[Numpy-discussion] any better way to normalise a matrix
Thu Dec 27 23:14:10 CST 2007
On 27/12/2007, firstname.lastname@example.org <email@example.com> wrote:
> in my code i am trying to normalise a matrix as below
> mymatrix=matrix(..# items are of double type..can be negative
> for i in range(numrows):
> for j in range(numcols):
> # 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:
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:
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.
More information about the Numpy-discussion