简体   繁体   English

Numpy 按降序对二维数组进行排序并从每行中取前 N

[英]Numpy sorting 2d array by descending and take first N from each row

here have an original 2D array这里有一个原始的二维数组

in_arr = np.array([[20,0,10,40,30], [50,40,60,90,80]])

# original array
# [[20,  0, 10, 40, 30],
#  [50, 40, 60, 90, 80]]

I need to sort the array by descending and by row, therefore, I use numpy.argsort(axis=1), and output sorted indices I got我需要按降序和按行对数组进行排序,因此,我使用 numpy.argsort(axis=1) 和 output 排序索引

out_arr1 = np.argsort(in_arr, axis = 1)[:,::-1]
>>> array([[3, 4, 0, 2, 1],
          [3, 4, 2, 0, 1]])

Then, I need to extract the first 3 largerst number from each array row, the sample desired output as follows:然后,我需要从每个数组行中提取前 3 个最大的数字,样本所需的 output 如下:

# first 3 largest number from each row
# [[40,30,20],
#  [90,80,60]]

I have struggling few hours to try to come out correct solution, but still have no idea how should I do, here would like to ask for help.我努力了几个小时试图找出正确的解决方案,但仍然不知道我该怎么做,在这里想寻求帮助。 Your valuable time and advice will be much appreciated.您的宝贵时间和建议将不胜感激。 Thank you!谢谢!

Using numpy.argsort() returns an array of indices for the sorted array.使用numpy.argsort()返回排序数组的索引数组。 As such, what your out_arr1 lets you know is where on each row to find the highest values.因此,您的out_arr1让您知道的是每一行的哪个位置可以找到最高值。

If you are to continue this way, what you would need to do is for each row in in_arr (hereby written as in_arr[i] ) take values found at the first 3 indices in out_arr1[i] .如果您要继续这种方式,您需要对 in_arr 中的每一行(在此写为in_arr[i] )取 out_arr1[ out_arr1[i] ] 中前 3 个索引处的值。

What that means is that out_arr1[i, 0] tells you where the highest value in in_arr on row i is located.这意味着out_arr1[i, 0]告诉您第i行的 in_arr 中的最大值所在的位置。 In our case, out_arr1[0, 0] = 3 , which means the highest value in row 0 is 40 (on index 3)在我们的例子中, out_arr1[0, 0] = 3 ,这意味着第 0 行的最大值是 40(在索引 3 上)

Doing this, the 3 largest numbers on each row are represented by out_arr1[0, 0] , out_arr1[0, 1] , out_arr1[0, 2] and out_arr1[1, 0] , out_arr1[1, 1] , out_arr1[1, 2] .这样做,每行上最大的 3 个数字由out_arr1[0, 0]out_arr1[0, 1]out_arr1[0, 2]out_arr1[1, 0]out_arr1[1, 1]out_arr1[1, 2]

to get the desired output, we would need something along the lines of:要获得所需的 output,我们需要以下内容:

final_arr = numpy.array([in_arr[0, out_arr1[0, 0], in_arr[0, out_arr1[0, 1], in_arr[0, out_arr1[0, 2], in_arr[1, out_arr1[1, 0], in_arr[1, out_arr1[1, 1], in_arr[1, out_arr1[1, 2]])

This however, is less than elegant, and there is another, easier solution to your problem.然而,这并不优雅,还有另一种更简单的解决方案来解决您的问题。

Using numpy.sort() instead of numpy.argsort() we can return the exact values of in_arr sorted along an axis.使用numpy.sort()而不是numpy.argsort()我们可以返回沿轴排序的in_arr的确切值。 By doing that, we no longer need to use an output index to find our 3 highest values, as they are the first 3 in our new output.通过这样做,我们不再需要使用 output 索引来查找我们的 3 个最高值,因为它们是我们新 output 中的前 3 个。

Considering out_arr2 as the output from numpy.sort() , the final array would look like:out_arr2视为numpy.sort()中的 output ,最终数组将如下所示:

final_arr = numpy.array([[out_arr[0, 0], out_arr[0, 1], out_arr[0, 2]], [out_arr[1, 0], out_arr[1, 1], out_arr[1, 2]]])

Based on this this answer you can do something like this基于这个this answer你可以做这样的事情

np.array(list(map(lambda x, y: y[x], np.argsort(in_arr), in_arr)))[:,::-1][:,:3]

which gives这使

array([[40, 30, 20],
       [90, 80, 60]])

You can first sort all rows in the input array with a list comprehension using sorted .您可以首先使用 sorted 使用列表推导对输入数组中的所有行进行sorted Then you extract the last 3 numbers of the rows.然后提取行的最后 3 个数字。

in_arr = np.array([[20,0,10,40,30], [50,40,60,90,80]])

output = []
for i in [sorted(row) for row in in_arr]:
    output.append(i[-3:][::-1])
    
print(output)

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

相关问题 Numpy 二维数组 - 从指定索引中获取 N 个元素 - Numpy 2d array - take N elements from specified index 按前 n 行对二维数组进行排序 - Sorting 2D array by the first n rows Numpy索引:第一个(变化的)2d数组中每行的元素数 - Numpy indexing: first (varying) number of elements from each row in 2d array 从 `2D` numpy 的每一行中删除 `n` 元素,其中 `n` 逐行变化 - Removing `n` elements from each row in `2D` numpy where `n` varies row by row Numpy 2d Array 从每一行切片不同的元素 - Numpy 2d Array slicing different elements from each row 二维 Numpy 数组的前 N 个元素,其中 N 是一个列表 - First N elements of a 2D Numpy array where N is a list 在 2d numpy 数组的每一行中选择前“n”个非重复元素的有效方法 - Efficient way to pick first 'n' non-repeating elements in every row of a 2d numpy array numpy:创建一个二维numpy数组,其中的每一行都填充有常规数组中的字符串 - numpy: create a 2d numpy array where each row is filled with strings from a regular array 将 2D numpy 数组排序到每个元素与某个点的接近程度 - Sorting a 2D numpy array on to the proximity of each element to a certain point 在 numpy 二维数组上 - 当 N 行更改时如何将每行中的最后 N 个数组元素设置为零 - on a numpy 2D array - how to set last N array elements in each row to zero when N changes over rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM