How to Normalize a NumPy Matrix (With Examples)


To normalize a matrix means to scale the values such that that the range of the row or column values is between 0 and 1.

The easiest way to normalize the values of a NumPy matrix is to use the normalize() function from the sklearn package, which uses the following basic syntax:

from sklearn.preprocessing import normalize

#normalize rows of matrix
normalize(x, axis=1, norm='l1')

#normalize columns of matrix
normalize(x, axis=0, norm='l1')

The following examples show how to use this syntax in practice.

Example 1: Normalize Rows of NumPy Matrix

Suppose we have the following NumPy matrix:

import numpy as np

#create matrix
x = np.arange(0, 36, 4).reshape(3,3)

#view matrix
print(x)

[[ 0  4  8]
 [12 16 20]
 [24 28 32]]

The following code shows how to normalize the rows of the NumPy matrix:

from sklearn.preprocessing import normalize

#normalize matrix by rows
x_normed = normalize(x, axis=1, norm='l1')

#view normalized matrix
print(x_normed)

[[0.         0.33333333 0.66666667]
 [0.25       0.33333333 0.41666667]
 [0.28571429 0.33333333 0.38095238]]

Notice that the values in each row now sum to one.

  • Sum of first row: 0 + 0.33 + 0.67 = 1
  • Sum of second row: 0.25 + 0.33 + 0.417 = 1
  • Sum of third row: 0.2857 + 0.3333 + 0.3809 = 1

Example 2: Normalize Columns of NumPy Matrix

Suppose we have the following NumPy matrix:

import numpy as np

#create matrix
x = np.arange(0, 36, 4).reshape(3,3)

#view matrix
print(x)

[[ 0  4  8]
 [12 16 20]
 [24 28 32]]

The following code shows how to normalize the rows of the NumPy matrix:

from sklearn.preprocessing import normalize

#normalize matrix by columns
x_normed = normalize(x, axis=0, norm='l1')

#view normalized matrix
print(x_normed)

[[0.         0.08333333 0.13333333]
 [0.33333333 0.33333333 0.33333333]
 [0.66666667 0.58333333 0.53333333]]

Notice that the values in each column now sum to one.

  • Sum of first column: 0 + 0.33 + 0.67 = 1
  • Sum of second column: 0.083 + 0.333 + 0.583 = 1
  • Sum of third column: 0.133 + 0.333 + 0.5333 = 1

Additional Resources

The following tutorials explain how to perform other common operations in Python:

How to Normalize Arrays in Python
How to Normalize Columns in a Pandas DataFrame

Leave a Reply

Your email address will not be published.