How to Normalize a NumPy Matrix (With Examples)

Normalizing a NumPy matrix means transforming the data so that the values of each row or column sum to 1. This is done to ensure that the values of each element are within a certain range and don’t unduly affect one another. To normalize a matrix, each element can be divided by the sum of all elements in the matrix, or by the Euclidean norm of the matrix. Examples of how to normalize a NumPy matrix using these two methods are provided below.


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 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

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

x