【问题标题】:How to construct a rank array with numpy? (What is a rank array?)如何用 numpy 构造一个秩数组? (什么是秩数组?)
【发布时间】:2020-12-30 09:55:16
【问题描述】:

我希望你们所有人都有美好的一天。在我的 Python 课上,我们正在学习如何使用 Numpy,所以我们得到了一个关于它的作业。我的问题是:什么是秩数组,如何使用 python 构造它?我的导师试图用这些话来解释这一点,但我实际上什么都不懂:( 这些是说明:

rank_calculator(A) - 5 分

给定一个 numpy ndarray A,返回它的秩数组。

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。

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

【问题讨论】:

  • 如果你的输入数组是arr,那么arr.flatten().argsort().argsort().reshape(arr.shape)

标签: python arrays numpy python-requests


【解决方案1】:

您可以按照this answer on SO 中的建议多次使用numpy.argsort 来处理矩阵。

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。

双 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,然后是数组。

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

  2. 9th(在 sort2 中)是 1st(在 sort1 中),1st(在 sort1 中)是 9th(在数组中)。所以,9th(在数组中)是9th(在 sort2 中)

  3. 6th(在 sort2 中)是 9th(在 sort1 中),9th(在 sort1 中)是 6th(在数组中)。所以,6th(在数组中)是6th(在 sort2 中)

把你的头绕进去有点令人困惑,但是一旦你理解了 argsort() 的工作原理,你就不应该有问题。

【讨论】:

  • 啊,我现在明白了,非常感谢 :)) 现在我将查看代码。
  • 为什么我们使用 .argsort 两次?当我们首先使用该函数时,它会给出一些与答案无关的数字。如果你有时间,你也能澄清一下吗:)
  • 请务必让我在我的答案中添加解释。
  • 很高兴为您提供帮助,如果它解决了您的问题,请标记答案,因为它鼓励我将来再次帮助您:)
【解决方案2】:

问)什么是秩数组?

Ans:基本上是按排序顺序排列的元素。

基本上,您的老师要求您返回每个元素的位置(如果它们按升序排序)。

代码:

import numpy as np

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

flatA = A.flatten()
sorted_flatA = sorted(flatA)  # will become -> [0, 1, 4, 8, 9, 10, 15, 16, 18, 19]

# Using a 'MAP' to map the values of sorted_faltA to the index of sorted_faltA.
MAP = {}
for i in range(len(sorted_flatA)):
    MAP[sorted_flatA[i]] = i

# Then simply going through the 2D array snd replacing the with their ranks.
res = np.zeros(A.shape)
for i in range(A.shape[0]):
    for j in range(A.shape[1]):
        res[i][j] = MAP[A[i][j]]

print(res)

【讨论】:

  • 好吧,我会试着理解代码,但我不明白这背后的逻辑。例如,[ 9 4 15 0 18] 数组如何变成 [4 2 6 0 8]?流程是什么?
  • 所以如果你注意到 9 是二维数组中第四小的数字。 4 是第 2 个,15 是第 6 个,0 是第 0 个......就像这样。 (我们从 0 开始计数)
  • 非常感谢,我知道了。现在,我将查看代码
猜你喜欢
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
相关资源
最近更新 更多