# How to normalize a 2-dimensional numpy array in python less verbose?

91

Given a 3 times 3 numpy array

``````a = numpy.arange(0,27,3).reshape(3,3)

# array([[ 0,  3,  6],
#        [ 9, 12, 15],
#        [18, 21, 24]])
``````

To normalize the rows of the 2-dimensional array I thought of

``````row_sums = a.sum(axis=1) # array([ 9, 36, 63])
new_matrix = numpy.zeros((3,3))
for i, (row, row_sum) in enumerate(zip(a, row_sums)):
new_matrix[i,:] = row / row_sum
``````

There must be a better way, isn't there?

Perhaps to clearify: By normalizing I mean, the sum of the entrys per row must be one. But I think that will be clear to most people.

This question is tagged with `python` `arrays` `syntax` `numpy` `normalization`

~ Asked on 2012-01-18 03:12:41

### The Best Answer is

140

Broadcasting is really good for this:

``````row_sums = a.sum(axis=1)
new_matrix = a / row_sums[:, numpy.newaxis]
``````

`row_sums[:, numpy.newaxis]` reshapes row_sums from being `(3,)` to being `(3, 1)`. When you do `a / b`, `a` and `b` are broadcast against each other.

~ Answered on 2012-01-18 03:21:58

110

Scikit-learn offers a function `normalize()` that lets you apply various normalizations. The "make it sum to 1" is called L1-norm. Therefore:

``````from sklearn.preprocessing import normalize

matrix = numpy.arange(0,27,3).reshape(3,3).astype(numpy.float64)
# array([[  0.,   3.,   6.],
#        [  9.,  12.,  15.],
#        [ 18.,  21.,  24.]])

normed_matrix = normalize(matrix, axis=1, norm='l1')
# [[ 0.          0.33333333  0.66666667]
#  [ 0.25        0.33333333  0.41666667]
#  [ 0.28571429  0.33333333  0.38095238]]
``````

Now your rows will sum to 1.

~ Answered on 2014-03-20 22:54:35