简体   繁体   English

如何找到二维数组中每一行的最大值?

[英]How to find the max of each row in 2d arrays?

For example, I have this 2d array:例如,我有这个二维数组:

[
    [
     0.0,
     0.24320757858085434,
     0.14893361727523413,
     0.29786723455046826,
     0.18838778030301612,
     0.12160378929042717
    ],
    [
     0.23717478210768014,
     0.0,
     0.16770789675478251,
     0.20539938644228997,
     0.25981195646349819,
     0.1299059782317491
    ],
    [
     0.21681956134183847,
     0.250361664212574,
     0.0,
     0.23178986094050727,
     0.16390018248131957,
     0.13712873102376066
    ],
    [
     0.2933749527592357,
     0.20744741852633861,
     0.15681550844086434,
     0.0,
     0.18554661183269694,
     0.15681550844086434
    ],
    [
     0.20305810393286577,
     0.28716752453162431,
     0.12135042758887897,
     0.20305810393286577,
     0.0,
     0.18536584001376513
    ],
    [
     0.17877693623386351,
     0.19584032147389943,
     0.13848001934394774,
     0.23407395508684939,
     0.25282876786143976,
     0.0
    ]
]

which gives sets of probabilities.这给出了一组概率。 How can I find the best probability of each row?如何找到每一行的最佳概率? And also is there any way to find for example the 2nd, 3rd best probability without changing the elements' positions?还有什么方法可以在不改变元素位置的情况下找到例如第二、第三个最佳概率?

You can do this easily with 3rd party library numpy .您可以使用 3rd 方库numpy轻松完成此操作。 First create a numpy array:首先创建一个numpy数组:

A = np.array([[0.0, 0.24320757858085434, 0.14893361727523413, 0.29786723455046826, 0.18838778030301612, 0.12160378929042717], [0.23717478210768014, 0.0, 0.16770789675478251, 0.20539938644228997, 0.25981195646349819, 0.1299059782317491], [0.21681956134183847, 0.250361664212574, 0.0, 0.23178986094050727, 0.16390018248131957, 0.13712873102376066], [0.2933749527592357, 0.20744741852633861, 0.15681550844086434, 0.0, 0.18554661183269694, 0.15681550844086434], [0.20305810393286577, 0.28716752453162431, 0.12135042758887897, 0.20305810393286577, 0.0, 0.18536584001376513], [0.17877693623386351, 0.19584032147389943, 0.13848001934394774, 0.23407395508684939, 0.25282876786143976, 0.0]])

To return the maximum of each row:返回每行的最大值:

res = A.max(axis=1)

For the second largest in each row, you can use numpy.sort .对于每行中的第二大,您可以使用numpy.sort This sorts along an axis (not in place) and then extracts the 2nd largest (via -2).这沿着轴(不在位)排序,然后提取第二大(通过 -2)。

res = np.sort(A, axis=1)[:, -2]

These are both vectorised calculations .这些都是矢量化计算 You can perform these calculations using lists of lists, but this is inadvisable.可以使用列表列表执行这些计算,但这是不可取的。

@jpp's numpy solution is probably the way to go, for the reasons they gave, but if you wanted to do it from pure python, you could do the following: @jpp 的numpy解决方案可能是可行的方法,出于他们给出的原因,但如果您想从纯 python 中执行此操作,您可以执行以下操作:

#Get the maximum value for each list

[[max(i)] for i in my_list]

# [[0.29786723455046826], [0.2598119564634982], [0.250361664212574], 
# [0.2933749527592357], [0.2871675245316243], [0.25282876786143976]]

# Get the maximum 2 values for each list:

[sorted(i)[-2:] for i in my_list]

# Get the maximum 3 values for each list:

[sorted(i)[-3:] for i in my_list]

And so on.等等。 Note that this will not reorder the original list, as the sorting is occurring in the sublists being created in the list comprehension请注意,这不会对原始列表重新排序,因为排序是在列表理解中创建的子列表中进行的

You can first sort each row in descending order, then select first or second largest elements depending on your need.您可以先按降序对每一行进行排序,然后根据需要选择第一大或第二大的元素。

a = [
    [
     0.0,
     0.24320757858085434,
     0.14893361727523413,
     0.29786723455046826,
     0.18838778030301612,
     0.12160378929042717
    ],
    [
     0.23717478210768014,
     0.0,
     0.16770789675478251,
     0.20539938644228997,
     0.25981195646349819,
     0.1299059782317491
    ],
    [
     0.21681956134183847,
     0.250361664212574,
     0.0,
     0.23178986094050727,
     0.16390018248131957,
     0.13712873102376066
    ]
]

for i in range(0, len(a)):
    a[i].sort(reverse=True)

print "1st Largests:"
for row in a:
    print "\t" + str(row[0])

print "2nd Largests:"
for row in a:
    print "\t" + str(row[1])

PS: if you are worrying about efficiency, then what you need to look for is partitioning. PS:如果你担心效率,那么你需要寻找的是分区。 Lomuto and Hoare partition schemes are two famous ones. Lomuto 和 Hoare 分区方案是两个著名的方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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