I have a 180295* 10 numpy array called lda_trans, row means words, and column means 10 topics.
array([[0.01841009, 0.01840699, 0.35798764, ..., 0.38443892, 0.01841072,
0.12870054],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
[0.1 , 0.1 , 0.1 , ..., 0.1 , 0.1 ,
0.1 ],
...,
[0.0416964 , 0.62473603, 0.0416964 , ..., 0.04169395, 0.04169796,
0.04169232],
[0.03772096, 0.03775132, 0.66048403, ..., 0.03771698, 0.03772411,
0.0377139 ],
[0.03754747, 0.03756587, 0.66206395, ..., 0.03754399, 0.037551 ,
0.03753927]])
Now I want to turn back each row's maximum value's column name, I only know how to extract the max value in each row, but I don't know how to get the column name. I know in pandas can use idxmax. But is there any similar function in Numpy? Thanks!
for i in range(180295):
lda_trans_max.append(np.max(lda_trans[i]))
There is argmin() and argmax() provided by numpy that returns the index of the min and max of a numpy array respectively.
index() functions to find out the index of the maximum value in a list. Use the enumerate() function to find out the index of the maximum value in a list. Use the numpy. argmax() function of the NumPy library to find out the index of the maximum value in a list.
You can use np. argmax() directly. The example is copied from the official documentation. axis = 0 is to find the max in each column while axis = 1 is to find the max in each row.
argmax. Returns the indices of the maximum values along an axis.
The following code shows how to get the index of the max value in a one-dimensional NumPy array: The argmax () function returns a value of 2. This tells us that the value in index position 2 of the array contains the maximum value.
But for the 2D array, you have to use Numpy module unravel_index. It will easily find the Index of the Max and Min value. from numpy import unravel_index result = unravel_index (np.max (array_2d),array_2d.shape) print ("Index for the Maximum Value in the 2D Array is:",result) Index for the Maximum Value in 2D Array
Finding the Minimum Value You can find the minimum value using the numpy.min () method. # Minimum Value min = np.min (array) print ("The minimum value in the array is :",min) Min Value in a 1D Numpy Array
The following code shows how to find the first index position of several values in a NumPy array: The value 4 first occurs in index position 0. The value 7 first occurs in index position 1. The value 8 first occurs in index position 4.
Use np.argmax
.
Demo:
>>> a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> np.argmax(a, axis=1)
array([4, 4])
You are getting [4, 4]
here because in both rows, the element with the maximum value is at position 4
.
Another demo:
>>> a
array([[5, 9, 7, 6, 8],
[8, 7, 7, 6, 9]])
>>> np.argmax(a, axis=1)
array([1, 4])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With