如何用numpy构造等级数组?(什么是排名数组?) [英] How to construct a rank array with numpy? (What is a rank array?)

查看:47
本文介绍了如何用numpy构造等级数组?(什么是排名数组?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望大家都过得愉快.在我的python课上,我们正在学习如何使用Numpy,因此我们得到了一个相关的作业.我的问题是:什么是等级数组,如何使用python构造等级数组?我的老师试图用这些内容来解释这一点,但实际上我什么都不懂:(这些是说明:

I hope all of you are having a great day. In my python class, we are learning how to use Numpy, so we got an assignment about that. My question is this: What is a rank array and how can I construct that with using python? My instructor tried to explain that with these lines but I did not understand anything actually :( These are the instructions:

rank_calculator(A)-5分

rank_calculator(A) - 5 pts

给出一个numpy ndarray A,返回其等级数组.

Given a numpy ndarray A, return its rank array.

Input: [[ 9 4 15 0 18]
        [16 19 8 10 1]]

Return value: [[4 2 6 0 8]
               [7 9 3 5 1]]

返回值应该是与原始数组A大小和形状相同的ndarray.

The return value should be an ndarray of the same size and shape as the original array A.

那么,有人可以解释一下吗?不幸的是,我不太擅长Python:(

So, can someone explain that? I am not so good at Python, unfortunately :(

推荐答案

您可以多次使用 numpy.argsort 处理矩阵,如

You can use numpy.argsort multiple times to handle a matrix, as suggested in this answer on SO.

import numpy as np

inp = np.array([[9,4,15,0,18],
                [16,19,8,10,1]])

inp.ravel().argsort().argsort().reshape(inp.shape)

array([[4, 2, 6, 0, 8],
       [7, 9, 3, 5, 1]])

什么是排名矩阵?

总而言之,如果我要提取矩阵中的所有整数,并将它们从最小到最大进行排序,然后为每个整数分配一个从0到9的 rank ,则将得出等级矩阵.请注意,最小的是0,排名为0,最大的是19,最后排名为9.

In summary, if I were to take all the integers in the matrix, and sort them smallest to largest, then assign each one a rank from 0 to 9, that would result in the rank matrix. Notice that the smallest is 0 which gets a rank of 0, while largest is 19, which gets the last rank of 9.

双重argsort的工作方式

#printing them so they align nicely
print('Array ->', end='')
for i in inp.ravel().astype('str'):
    print(i.center(4), end='')
print('\n')
print('Sort1 ->', end='')
for i in inp.ravel().argsort().astype('str'):
    print(i.center(4), end='')
print('\n')
print('Sort2 ->', end='')
for i in inp.ravel().argsort().argsort().astype('str'):
    print(i.center(4), end='')

Array -> 9   4   15  0   18  16  19  8   10  1  

Sort1 -> 3   9   1   7   0   8   2   5   4   6  

Sort2 -> 4   2   6   0   8   7   9   3   5   1  

让我们首先总结一下 argsort 的功能.它获取每个元素的位置,并在排序后将它们放在它们所属的位置.知道了这一点,我们可以编写一个本质上是三角形的后向逻辑.让我们从sort2开始,然后从sort1再到数组.

Let's first summarize what argsort does. It takes the position of each element and puts them where they belong after sorting. Knowing this, we can write a backward logic which is sort of triangular in nature. Lets start from sort2, then sort1 and then array.

  1. 第0th (在sort2中)是 4th (在sort1中), 4th (在sort1中)是 0th (在数组中).所以 thth (在数组中)是 0th (在sort2中)

  1. 0th (in sort2) is 4th (in sort1), 4th (in sort1) is 0th (in array). So 0th (in array) is 0th (in sort2)

9th (在sort2中)是 1st (在sort1中), 1st (在sort1中)是 9th (在数组中).因此, 9th (在数组中)是 9th (在sort2中)

9th (in sort2) is 1st (in sort1), 1st (in sort1) is 9th (in array). So, 9th (in array) is 9th (in sort2)

6th (在sort2中)是 9th (在sort1中), 9th (在sort1中)是 6th (在数组中).因此, 6th (在数组中)是 6th (在sort2中)

6th (in sort2) is 9th (in sort1), 9th (in sort1) is 6th (in array). So, 6th (in array) is 6th (in sort2)

缠住它有点困惑,但是一旦您了解了argsort()的工作原理,就不会有问题了.

Its a bit confusing to wrap your head around it, but once you can understand how argsort() works, you shouldn't have a problem.

这篇关于如何用numpy构造等级数组?(什么是排名数组?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆