Issue
I hope the title makes sense, I don't know how to put it exactly...
I have two 1-dimensional arrays (V and C) with an equal number of items inside. I need to obtain the maximum value of their product (Wmax), which I've achieved with this:
Wmax = numpy.amax(V*C)
However, I also need to extract the actual values of V and C that lead to Wmax. The idea then is to retrieve the array index of either V or C (they are the same anyway) and then read the value of both V and C at that index. One way that I've thought about to retrieve this index is to create a W array where each element is the product of each element of V and C, run numpy.amax on W instead of V*C and then find the index of Wmax inside W.
However, I wonder: is there a way to obtain this index without creating the new variable W? Would this different method be more or less efficient?
Solution
One way to do is to use np.argmax like that:
import numpy as np
C = np.array([0, 5, 2, 3, 4, 5])
V = np.array([0, 2, 4, 4, 2, 1])
indx = np.argmax(C*V)
print(f"index of the max value:{indx}")
print(f"C's value:{C[indx]}")
print(f"V's value:{V[indx]}")
# output index of the max value:3 C's value:3 V's value:4
Answered By - Phoenix
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.