Custom min-max, z-score, MAD z-score and decimal scaling normalization in Python

● Problem Statement

Use the methods provided below to normalize the following group of data:
200, 300, 400, 600, 1000
(a) min-max normalization by setting min = 0 and max = 1
(b) z-score normalization
(c) z-score normalization using the mean absolute deviation instead of standard deviation
(d) normalization by decimal scaling

● Algorithm

Input : Data set of elements as data and a number to normalize from the data set
Output: Displaying min-max normalization, z-score normalization, MAD z-score
normalization and normalization by decimal scaling.
Data Structure: data is a list where we store user-inputted data/predefined data.
Description: Using statistics and pandas package’s functions to calculate numerical
things with less complexity.
Step 1 : Start
Step 2 : data ← 200, 300, 400, 600, 1000
Step 3 : Display “Enter an item from data:”
Read num
Step 4 : Display “Calculating min-max normalization”
Display “After doing min-max normalization :” call minMaxNor with num, data
Step 5 : Display “Calculating z-score normalization”
Display “After doing z-score normalization”
Call zNor with num, mean value of data list, standard division value of data list
Step 6 : Display “Calculating Modified z-score normalization”
Step 7 : df ← call DataFrame with data list from pandas package
Step 8 : Display “After doing Modified z-score normalization : ”
Call zNorMAD with num, mean value of data list and df.mad
Step 9 : Display “Calculating decimal scaling normalization”
Display “After doing decimal scaling normalization: ”
call decNor with num, the maximum value of data list
Step 10 : Stop

Algorithm for minMaxNor

Input: a list called list from where we will fetch the data
An integer called num
Output : Returns the answer
Data Structure : ans an integer where the answer to be stored
minNum to store the minimum settings
MaxNum to store the maximum settings
Step 1: Start
Step 2 : Display “Enter Minimun Setting:”
Read minNum
Step 3 : Display “Enter Maximum Setting:”
Read maxNum
Step 4 : ans ← round with 3 decimal places
((num-min(list))/(max(list)-min(list))*(maxNum-minNum))+minNum
Step 5 : Return ans

Algorithm for zNor

Input: mean value of the list as mean
Stander Division as stdDv
An integer called num
Output : Returns the answer
Step 1: Start
Step 2 : Return round with 2 decimal places (num-mean)/stdDv

Algorithm for zNorMAD

Input: mean value of the list as mean
Absolute mean division as abMeanDiv
An integer called num
Output : Returns the answer
Step 1: Start
Step 2 : Return round with 2 decimal places (num-mean)/abMeanDiv

Algorithm for decNor

Input: Maximum value of the list as maxNum
An integer called num
Output : Returns the answer
Data Structure : div an integer where the answer to be stored
Digit where the size of the maxNum to be stored
Step 1: Start
Step 2 : digit ← len(str(maxNum))
Step 3 : div ← 10 to the power digit
Step 4 : returns num/div

● Source Code

'''

Problem Statement
Use the methods provided below to normalize the following group of data:
200, 300, 400, 600, 1000
(a) min-max normalization by setting min = 0 and max = 1
(b) z-score normalization
(c) z-score normalization using the mean absolute deviation instead of standard deviation
(d) normalization by decimal scaling

'''

import statistics
import pandas as pd


def minMaxNor(num,list):
    minNum=int(input("Enter Minimun Setting:\t"))
    maxNum = int(input("Enter Maximum Setting:\t"))
    ans=round(((num-min(list))/(max(list)-min(list))*(maxNum-minNum))+minNum,2)
    return ans

def zNor (num,mean,stdDv):
    return round((num-mean)/stdDv,2)

def zNorMAD (num,mean,abMeanDiv):
    return round((num-mean)/abMeanDiv,2)

def decNor(num,maxNum):
    digit=len(str(maxNum))
    div=pow(10,digit)
    return num/div

data=[200, 300, 400, 600, 1000]

num=int(input("Enter an item from data : \t"))
print("Calculating  min-max normalization")
print("After doing min-max normalization :",minMaxNor(num,data))

print("\nCalculating z-score normalization")
print("After doing z-score normalization : \t", zNor(num,statistics.mean(data),statistics.stdev(data)))

print("\nCalculating Modified z-score normalization")
df = pd.DataFrame(data)
print("After doing Modified z-score normalization : \t", zNorMAD(num,statistics.mean(data),df.mad()))

print("\nCalculating decimal scaling normalization")
print("After doing decimal scaling normalization : \t", decNor(num,max(data)))

Output:

Enter an item from data : 200
Calculating min-max normalization
Enter Minimun Setting: 0
Enter Maximum Setting: 1
After doing min-max normalization : 0.0
Calculating z-score normalization
After doing z-score normalization : -0.95
Calculating Modified z-score normalization
After doing Modified z-score normalization : 0 -1.25
dtype: float64
Calculating decimal scaling normalization
After doing decimal scaling normalization : 0.02
Process finished with exit code 0
Enter an item from data : 600
Calculating min-max normalization
Enter Minimun Setting: 0
Enter Maximum Setting: 1
After doing min-max normalization : 0.5
Calculating z-score normalization
After doing z-score normalization : 0.32
Calculating Modified z-score normalization
After doing Modified z-score normalization : 0 0.42
dtype: float64
Calculating decimal scaling normalization
After doing decimal scaling normalization : 0.06
Process finished with exit code 0

● Discussion:

Here I have used a packages called Statistics and pandas to uses it’s functions. Using
those function the programme became shorter and saved a lot of time.

Leave a Comment

Your email address will not be published. Required fields are marked *