Python 在NumPy数组中查找频率较低的数字

Python 在NumPy数组中查找频率较低的数字,python,numpy,Python,Numpy,假设我有以下NumPy数组: a=np.数组([1,2,3,1,2,1,1,1,3,2,2,1]) 如何在此数组中找到频率较低的数字?使用- 使用- 试试这个: import numpy as np a = np.array([1,2,3,1,2,1,1,1,3,2,2,1]) b = np.unique(a, return_counts=True) c = b[0][np.argmin(b[1], axis=0)] print(c) 试试这个: import numpy as np a

假设我有以下NumPy数组:

a=np.数组([1,2,3,1,2,1,1,1,3,2,2,1])
如何在此数组中找到频率较低的数字?

使用-

使用-

试试这个:

import numpy as np
a = np.array([1,2,3,1,2,1,1,1,3,2,2,1])
b = np.unique(a, return_counts=True)
c = b[0][np.argmin(b[1], axis=0)]

print(c)
试试这个:

import numpy as np
a = np.array([1,2,3,1,2,1,1,1,3,2,2,1])
b = np.unique(a, return_counts=True)
c = b[0][np.argmin(b[1], axis=0)]

print(c)

下面是另一个使用numpy的解决方案:

cnt = np.bincount(a)
np.nonzero(cnt==cnt[np.nonzero(cnt)].min())[0][0]
输出:

3

下面是另一个使用numpy的解决方案:

cnt = np.bincount(a)
np.nonzero(cnt==cnt[np.nonzero(cnt)].min())[0][0]
输出:

3

试试这个
a.min()
试试这个
a.min()
3不是频率最低的数字,但它返回0?我遗漏了什么吗?bincount从0开始,因此由于我的列表中没有0,它返回0更新了我的答案。3不是最不常见的数字,但它返回0?我遗漏了什么吗?bincount从0开始,因此由于我的列表中没有0,它将返回0更新我的答案。