【问题标题】:How to find elements loactions is numpy array [closed]如何查找元素位置是numpy数组[关闭]
【发布时间】:2017-01-09 08:14:24
【问题描述】:

我有一个二维数组,我想从中获取在其行和列中的前 2 个值中的每个元素的索引。例如,给定以下数组 -

r = np.random.rand(5,5)
>>> r
array([[ 0.89771084,  0.84415435,  0.81601728,  0.42322215,  0.78240944],
       [ 0.84490939,  0.53644975,  0.3506268 ,  0.98212093,  0.76426087],
       [ 0.254155  ,  0.12818165,  0.82656036,  0.97441244,  0.58597015],
       [ 0.50566688,  0.67774518,  0.58434845,  0.5204808 ,  0.9225643 ],
       [ 0.73930611,  0.31890204,  0.47691016,  0.28034347,  0.57832287]])

所需的输出是 -

[[0,0],
 [1,0],
 [0,1],
 [3,1],
 [2,2],
 [1,3],
 [2,3],
 [3,4]]

注意[0,2] 被省略了,因为虽然它是列中的第二大元素,但它是行中的第三大元素。

【问题讨论】:

  • 不够清楚。试试argsort
  • @Divakar 什么不清楚?
  • 切片rcols[3:5] 表示什么?对于给定的样本,预期的输出是什么?
  • @Divakar 是的,我已经编辑了
  • 编辑大大改善了这个问题。我会投票支持重新开放,但缺乏这样做的声誉。

标签: python arrays numpy


【解决方案1】:

正如 cmets 中所建议的,argsort 是关键。它为您提供已排序元素的索引。执行argsort 两次会给你排名。 (有一种更有效的方法来获得排名,我把它留作练习。)

然后使用沿行和列的排名来识别排名和列都在前 2 位的元素。

示例实现:

import numpy as np

r = np.array([[ 0.89771084,  0.84415435,  0.81601728,  0.42322215,  0.78240944],
              [ 0.84490939,  0.53644975,  0.3506268 ,  0.98212093,  0.76426087],
              [ 0.254155  ,  0.12818165,  0.82656036,  0.97441244,  0.58597015],
              [ 0.50566688,  0.67774518,  0.58434845,  0.5204808 ,  0.9225643 ],
              [ 0.73930611,  0.31890204,  0.47691016,  0.28034347,  0.57832287]])

# indices of elements in descending order
col_order = np.argsort(r, axis=0)[::-1, :]
row_order = np.argsort(r, axis=1)[:, ::-1]

# sorting the indices gives the rank (0=highest element, 4=lowest element)
col_rank = np.argsort(col_order, axis=0)
row_rank = np.argsort(row_order, axis=1)

# mark top n elements in each row and column
n = 2
col_top_n = col_rank < n
row_top_n = row_rank < n

# mark elements that are in the nop n of BOTH, a row and a column
both_top_n = np.logical_and(row_top_n, col_top_n)

# get indices of marked elements
row_indices, col_indices = np.nonzero(both_top_n)

print('The following elements are in the top {} of both their rows and columns:'.format(n))
for row, column in zip(row_indices, col_indices):
    print('row: {}, column: {}, value: {}'.format(row, column, r[row, column]))

【讨论】:

    猜你喜欢
    • 2021-03-17
    • 1970-01-01
    • 2015-01-31
    • 2018-10-20
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多