|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +''' |
| 4 | + This python code calculates the Min-Max Normalization |
| 5 | +
|
| 6 | + Min-max normalization is one of the most common ways to normalize data. |
| 7 | + For every feature, the minimum value of that feature gets transformed into a 0, |
| 8 | + the maximum value gets transformed into a 1, and every other value |
| 9 | + gets transformed into a decimal between 0 and 1. |
| 10 | +
|
| 11 | + norm = [(x - X.min)/(X.max - X.min)]*(new_max - new_min) + new_min |
| 12 | +
|
| 13 | +''' |
| 14 | + |
| 15 | +def minmax_norm(X, npmin, diff, nmax, nmin): |
| 16 | + return ( (X - npmin) / (diff) ) * (nmax-nmin) + nmin |
| 17 | + |
| 18 | +# Input the data array |
| 19 | +data = [ 13, 15, 16, 16, 19, 20, 20, 21, 22, 22, |
| 20 | + 25, 25, 25, 25, 30, 33, 33, 35, 35, 35, |
| 21 | + 35, 36, 40, 45, 46, 52, 70] |
| 22 | + |
| 23 | +# Setting the new min and new max |
| 24 | +nmin = 0 |
| 25 | +nmax = 1 |
| 26 | + |
| 27 | +# Putting the data in new numpy array |
| 28 | +nparray = np.array(data) |
| 29 | + |
| 30 | +#------------- Normalizing the data --------------------------# |
| 31 | +# Difference between max nparray value and min nparray value |
| 32 | +diff = nparray.max() - nparray.min() |
| 33 | +npmin = nparray.min() |
| 34 | + |
| 35 | +ndata = minmax_norm(nparray, npmin, diff, nmax, nmin) |
| 36 | + |
| 37 | +print(ndata) |
| 38 | + |
| 39 | +# Getting the norm of 35 |
| 40 | +nvalue = minmax_norm(35, npmin, diff, nmax, nmin) |
| 41 | +print(np.round(nvalue, 2)) |
0 commit comments