string - 用于二维数组中非唯一元素的 numpy 过滤器
问题描述
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']]
解决方案
我不太明白这个问题,但我认为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')
推荐阅读
- sqlite - 获取关系上所有相关列的计数
- python - 包装一个已经存在的类
- amazon-athena - DDL 返回的行格式不正确
- sql-server - 带有条件 JOIN 的 EF Core LINQ
- node.js - 使用 Node.js 无法通过 socket.io 正确发送值的问题
- javascript - 所有 JavaScript 正则表达式都可以在 .NET 中使用吗?
- python - python multiprocessing child process cannot access to global variable
- java - 如何根据 MiniMax Alpha-Beta 的返回值在游戏板上移动?
- windows - 运行 minikube start 时 Oracle 虚拟机 (6.0.10) 退出并出错
- mysql - SQL 函数:获取行中的下一个值