[Numpy-discussion] any better way to normalise a matrix

Anne Archibald peridot.faceted at gmail.com
Fri Dec 28 00:14:10 EST 2007


On 27/12/2007, devnew at gmail.com <devnew at gmail.com> wrote:
> in my code i am trying to normalise a matrix as below
>
> mymatrix=matrix(..# items are of double type..can be negative
> values....)
> numrows,numcols=mymatrix.shape
>
> for i in range(numrows):
>         temp=mymatrix[i].max()
>         for j in range(numcols):
>                 mymatrix[i,j]=abs(mymatrix[i,j]/temp)
>
> # i am using abs() to make neg vals positive
>
> this way the code takes too much time to run for a case of
> numrows=25, and numcols=8100 etc..
> being a beginner in numpy and python ,i would like to know if this can
> be done more efficiently..can anyone advise?

Yes. A major advantage - really the reason for existence - of numpy is
that you can do operations to whole matrices at once in a single
instruction, without loops. This is convenient from a coding point of
view, and it also allows numpy to greatly accelerate calculations.
Writing code the way you have above takes no advantage of numpy's
machinery, and it would probably run faster using python lists than
numpy arrays. I strongly recommend you read some numpy documentation;
try starting with the tutorial:
http://www.scipy.org/Tentative_NumPy_Tutorial

For example, to extract an array containing the maxima of each row of
mymatrix, you can use the amax() function:

temp = numpy.amax(mymatrix, axis=1)

Multiplying a whole matrix by a constant can be done simply as well:

7*mymatrix

A final comment is that numpy is designed for computations with
multidimensional data. Its basic data type is the array. It has a
specialized data type called "matrix" which provides slightly more
convenient matrix multiplication (in the sense of linear algebra). I
recommend you do not use numpy's matrix class until you are more
accustomed to the software.

Read the documentation. Start with the tutorial. Good luck.
Anne



More information about the NumPy-Discussion mailing list