Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy find the max value in a row and return back to it's column index

Tags:

python

numpy

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]))
like image 833
Shin Yu Wu Avatar asked Nov 12 '18 11:11

Shin Yu Wu


People also ask

How do you find the index of a maximum value in a Numpy array?

There is argmin() and argmax() provided by numpy that returns the index of the min and max of a numpy array respectively.

How do you return the index of max value in a list?

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.

How do you find the maximum value of a row in an array in Python?

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.

What is Numpy argmax return?

argmax. Returns the indices of the maximum values along an axis.

How to get the index of the max value in NumPy?

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.

How to find the maximum value in 2D array in Python?

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

How do you find the minimum value of a NumPy 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

How to find the index position of several values in NumPy?

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.


1 Answers

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])
like image 112
timgeb Avatar answered Oct 18 '22 20:10

timgeb