ID : 259

viewed : 126

Tags : PythonPython List

100

Normalization means converting a given data into another scale. We rescale data in such a way that it falls between two values. Most of the time data is rescaled between 0 and 1. We rescale the data for a different purpose. For example, machine learning algorithms perform better when the dataset values are small.

Let’s say we have a list {1,2,3} and after the normalization of the scale of 0 and 1, the list will be `{0, 0.5, 1}`

. We can also normalize our data to some other scale as well. Let’s say between 2 and 6. So, again if we have the list {1,2,3} after normalization it will be {2,4,6}.

To understand how normalization works. We will have a look at its formula. We subtract the `minimum value`

from every number and divide it by the `range`

i-e: max-min. So, in output, we get the normalized value of that specific number.

$$ X*{norm} = {x-x*{min}\over x*{max}-x*{min}} $$

We can use two methods to normalize a list. Either we can use the built-in function, which is available in the preprocessing module of the `sklearn`

package. Or we can make our logic for it which works on the same formula as discussed above.

`MinMaxScaler`

Function in Python `sklearn`

A built-in method named `MinMaxScaler()`

is available in `preprocessing`

module of the `sklearn`

package. We will create a 1D `NumPy`

array and pass it to the function. We must have `NumPy`

and `sklearn`

packages installed to use this function.

Example Code:

`# python 3.x import numpy as np from sklearn import preprocessing list = np.array([6,1,0,2,7,3,8,1,5]).reshape(-1,1) print('Original List:',list) scaler = preprocessing.MinMaxScaler() normalizedlist=scaler.fit_transform(list) print('Normalized List:',normalizedlist) `

Output:

`Original List: [[6] [1] [0] [2] [7] [3] [8] [1] [5]] Normalized List: [[0.75 ] [0.125] [0. ] [0.25 ] [0.875] [0.375] [1. ] [0.125] [0.625]] `

Now let’s say we want to specify our range for normalization. For this purpose, we will specify our `range`

in `MinMaxScaler()`

. By default, if we don’t specify the range, it will be 0 and 1. But we can define our range for scaling. In this example, we specify the range 0 and 3.

Example Code:

`# python 3.x import numpy as np from sklearn import preprocessing list = np.array([6,1,0,2,7,3,8,1,5]).reshape(-1,1) print('Original List:',list) scaler = preprocessing.MinMaxScaler(feature_range=(0, 3)) normalizedlist=scaler.fit_transform(list) print('Normalized List:',normalizedlist) `

Output:

`Original List: [[6] [1] [0] [2] [7] [3] [8] [1] [5]] Normalized List: [[2.25 ] [0.375] [0. ] [0.75 ] [2.625] [1.125] [3. ] [0.375] [1.875]] `

We can also normalize the list of numbers manually by writing the complete code for it, which works on the same formula as discussed above for normalization.

Example Code:

`list = [6,1,0,2,7,3,8,1,5] print('Original List:',list) xmin = min(list) xmax=max(list) for i, x in enumerate(list): list[i] = (x-xmin) / (xmax-xmin) print('Normalized List:',list) `

Output:

`Original List: [6, 1, 0, 2, 7, 3, 8, 1, 5] Normalized List: [0.75, 0.125, 0.0, 0.25, 0.875, 0.375, 1.0, 0.125, 0.625] `