简体   繁体   中英

How to keep the largest value for each row in a list of lists python

I have a list of lists as follows

array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02],
       [3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
       [9.97873928e-01, 1.10315885e-12, 2.12607185e-03],
       [5.13391890e-07, 9.84294588e-01, 1.57048982e-02]])

I would like to keep the largest value in each row, like so

array([[9.88945457e-01],
       [9.60482604e-01],
       [9.97873928e-01],
       [9.84294588e-01]])

Does this require a loop to go through each row? Or is this something enumerate() or iterrows() can handle?

Inseted of looping over the array, vectorizing the operation is much more efficient.

To do so, you have to play with the axis argument of the max function. In this case you should use the axis=1.

import numpy as np


a = np.array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02],
              [3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
              [9.97873928e-01, 1.10315885e-12, 2.12607185e-03],
              [5.13391890e-07, 9.84294588e-01, 1.57048982e-02]])
print(np.max(a, axis=1))

如果它是一个numpy数组

myarray.max(axis=1)

to find out the largest number you can do it like this:

def largest(arr,n): 

# Initialize maximum element 
max = arr[0] 

# Traverse array elements from second 
# and compare every element with  
# current max 
for i in range(1, n): 
    if arr[i] > max: 
        max = arr[i] 
return max


# Driver Code 
arr = [10, 324, 45, 90, 9808] 
n = len(arr) 
Ans = largest(arr,n) 
print ("Largest in given array is",Ans) 

Having this array:

array = np.array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02],
       ...        [3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
       ...        [9.97873928e-01, 1.10315885e-12, 2.12607185e-03],
       ...        [5.13391890e-07, 9.84294588e-01, 1.57048982e-02]])

If your array it's quite large it's better to use (as suggested by @Guillem or @Muhammad) the max numpy function.

Please note that the max numpy function will return a numpy array

np.max(a, axis=1)
>>> array([0.98894546, 0.9604826 , 0.99787393, 0.98429459])

However, I think that you can reach the same goal in python using a loop on the array and then use the max() function.

Somthing like this:

array = np.array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02],
      ...        [3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
      ...        [9.97873928e-01, 1.10315885e-12, 2.12607185e-03],
      ...        [5.13391890e-07, 9.84294588e-01, 1.57048982e-02]])

res = []
for sub_list in array:
    max_value = max(sub_list)
    res.append([max_value])

This method will return a standard python list

You are using numpy, but in plain python it is simply:

maxes = [max(x) for x in numlists]  

or

maxes = list(map(max,numlists))

numpy.argmax gets the index of the maximum value of each row
reshape raises one-dimensional data to two-dimensional

getArrayMax = lambda array: array[range(array.shape[0]), numpy.argmax(array, axis=1)].reshape(array.shape[0], 1)

>>> getArrayMax(f)
array([[0.98894546],
       [0.9604826 ],
       [0.99787393],
       [0.98429459]])


use a list comprehension with a max

 arr=np.array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02],
   [3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
   [9.97873928e-01, 1.10315885e-12, 2.12607185e-03],
   [5.13391890e-07, 9.84294588e-01, 1.57048982e-02]])

 result=[max(item)  for item in arr]
 print(result)

output:

 [0.988945457, 0.960482604, 0.997873928, 0.984294588]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM