Reputation: 1152
I have a rather large matrix (500000 * 24) as an ndarray
and I want to multiply its cell with the corresponding column min
. I have already done this with for
loops but I keep reading that this is not the NumPy
way of doing things.
Is there a proper way of doing such an operation (I might also want to substract a constant later)?
Thanks in advance
Upvotes: 1
Views: 3039
Reputation: 4532
Would normal multiply not do?
import numpy
a = numpy.random.random((4,2))
b = a * numpy.min(a,axis=0)
Upvotes: 1
Reputation: 53678
Yes you can simply multiply your array with the minimum vector directly, an example is shown below.
import numpy as np
data = np.random.random((500000, 24))
# This returns an array of size 500,000 that is the row of 24 values
minimum = data.min(axis=1)
data = data * minimum
If you wish to create a minimum
array of size 24 (where the minimum of the 500,000 values is taken) then you would choose axis=0
.
This set of slides discusses how such operations can work.
Upvotes: 2