*44*

To normalize the values in a NumPy array to be between 0 and 1, you can use one of the following methods:

**Method 1: Use NumPy**

import numpy as np x_norm = (x-np.min(x))/(np.max(x)-np.min(x))

**Method 2: Use Sklearn**

from sklearn import preprocessing as pre x = x.reshape(-1, 1) x_norm = pre.MinMaxScaler().fit_transform(x)

Both methods assume **x** is the name of the NumPy array you would like to normalize.

The following examples show how to use each method in practice.

**Example 1: Normalize Values Using NumPy**

Suppose we have the following NumPy array:

**import numpy as np
#create NumPy array
x = np.array([13, 16, 19, 22, 23, 38, 47, 56, 58, 63, 65, 70, 71])
**

We can use the following code to normalize each value in the array to be between 0 and 1:

**#normalize all values to be between 0 and 1
x_norm = (x-np.min(x))/(np.max(x)-np.min(x))
#view normalized array
print(x_norm)
[0. 0.05172414 0.10344828 0.15517241 0.17241379 0.43103448
0.5862069 0.74137931 0.77586207 0.86206897 0.89655172 0.98275862
1. ]
**

Each value in the NumPy array has been normalized to be between 0 and 1.

Hereâ€™s how it worked:

The minimum value in the dataset is 13 and the maximum value is 71.

To normalize the first value ofÂ **13**, we would apply the formula shared earlier:

**z**Â = (13 â€“ 13) / (71 â€“ 13) =_{i}Â = (x_{i}â€“ min(x)) / (max(x) â€“ min(x))**0**

To normalize the second value ofÂ **16**, we would use the same formula:

**z**= (16 â€“ 13) / (71 â€“ 13) =_{i}Â = (x_{i}â€“ min(x)) / (max(x) â€“ min(x))**.0517**

To normalize the third value ofÂ **19**, we would use the same formula:

**z**= (19 â€“ 13) / (71 â€“ 13) =_{i}Â = (x_{i}â€“ min(x)) / (max(x) â€“ min(x))**.1034**

We use this same formula to normalize each value in the original NumPy array to be between 0 and 1.

**Example 2: Normalize Values Using sklearn**

Once again, suppose we have the following NumPy array:

**import numpy as np
#create NumPy array
x = np.array([13, 16, 19, 22, 23, 38, 47, 56, 58, 63, 65, 70, 71])
**

We can use the **MinMaxScaler()** function from **sklearn** to normalize each value in the array to be between 0 and 1:

**from sklearn import preprocessing as pre
#reshape array so that it works with sklearn
x = x.reshape(-1, 1)
#normalize all values to be between 0 and 1
x_norm = pre.MinMaxScaler().fit_transform(x)
#view normalized array
print(x_norm)
[[0. ]
[0.05172414]
[0.10344828]
[0.15517241]
[0.17241379]
[0.43103448]
[0.5862069 ]
[0.74137931]
[0.77586207]
[0.86206897]
[0.89655172]
[0.98275862]
[1. ]]**

Each value in the NumPy array has been normalized to be between 0 and 1.

Notice that these normalized values match the ones calculated using the previous method.

**Additional Resources**

The following tutorials explain how to perform other common tasks in NumPy:

How to Rank Items in NumPy Array

How to Remove Duplicate Elements from NumPy Array

How to Find Most Frequent Value in NumPy Array