【问题标题】:numpy filter for not unique elements in 2d array用于二维数组中非唯一元素的 numpy 过滤器
【发布时间】:2021-05-01 12:27:33
【问题描述】:
import numpy as np

data = np.array(
    [
        ['a' 'a'],
        ['a' 'b'],
        ['d' 'c'],
        ['a' 'b'],
        ['d' 'c'],
        ['a' 'a'],
        ['b' 'a'],
        ['c' nan]
    ]
)

如何过滤最频繁的子数组? 预期结果:[['a' 'a'], ['d' 'c']]

【问题讨论】:

  • 输出不应该是:[['a', 'a'], ['a', 'b'], ['d', 'c']] ?
  • 或者正在测量列的频率?
  • 我想要列的频率:['a', 'b'] != ['b', 'a']
  • data 仅包含 1 列。创建数组时将字符串连接起来。
  • 你是对的。一列八行。我的错。但问题还是一样。

标签: string numpy filter 2d unique


【解决方案1】:

我不太明白这个问题,但我认为np.unqiue 可能有用。

data = np.array(
     [
         ['a', 'a'],
         ['a', 'b'],
         ['d', 'c'],
         ['a', 'b'],
         ['d', 'c'],
         ['a', 'a'],
         ['b', 'a'],
         ['c', np.nan]
     ]
 )

unique, idx, counts = np.unique(data[:,0], return_counts=True, return_index=True)
threshold = 1
data[idx[counts > threshold]]

输出:

array([['a', 'a'],
       ['d', 'c']], dtype='<U32')

【讨论】:

    猜你喜欢
    • 2013-10-11
    • 2019-01-18
    • 2023-02-14
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 2017-06-10
    相关资源
    最近更新 更多