Write a function to normalize a given list of numbers The in
Write a function to normalize a given list of numbers. The input is a list of real values; the return value is a list of normalized values. The normalization is done by: X_i = x_i - min x/maxx- minx where X_i is ith element of the input list, minx is the smallest value of the list and maxx is the max value of the input list. def normalize(list):
Solution
Python program
# Function definition is here
def normalize( lst ):
newlst = [] # the variable to store normalized values
minx = lst[0] # variable to store minx
maxx = lst[0] # variable for maxx
for k in lst: # loop to find min and max in input list
if k > maxx:
maxx = k # finding the maxx
if k < minx:
minx = k # finding the minx
for k in lst:
newlst = newlst + [float((k-minx))/(maxx-minx)] # computing the normalized value
return newlst
# Function call
v = normalize( [0,2.5,5,10] ) # function call
print v # printing the result
Output
[0.0, 0.25, 0.5, 1.0]
