如何计算 Python 中 ndarray 中某个项目的出现次数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28663856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count the occurrence of certain item in an ndarray in Python?
提问by mflowww
In Python, I have an ndarray ythat is printed as array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
在 Python 中,我有一个 ndarrayy打印为array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
I'm trying to count how many 0s and how many 1s are there in this array.
我正在尝试计算这个数组中有多少个0s 和多少个1s。
But when I type y.count(0)or y.count(1), it says
但是当我输入y.count(0)or 时y.count(1),它说
numpy.ndarrayobject has no attributecount
numpy.ndarray对象没有属性count
What should I do?
我该怎么办?
采纳答案by ozgur
>>> a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
>>> unique, counts = numpy.unique(a, return_counts=True)
>>> dict(zip(unique, counts))
{0: 7, 1: 4, 2: 1, 3: 2, 4: 1}
Non-numpy way:
非麻木的方式:
Use collections.Counter;
>> import collections, numpy
>>> a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
>>> collections.Counter(a)
Counter({0: 7, 1: 4, 3: 2, 2: 1, 4: 1})
回答by Joel
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
If you know that they are just 0and 1:
如果你知道他们只是0和1:
np.sum(y)
gives you the number of ones. np.sum(1-y)gives the zeroes.
给你的数量。 np.sum(1-y)给出零。
For slight generality, if you want to count 0and not zero (but possibly 2 or 3):
稍微概括一下,如果您想计数0而不是零(但可能是 2 或 3):
np.count_nonzero(y)
gives the number of nonzero.
给出非零的数目。
But if you need something more complicated, I don't think numpy will provide a nice countoption. In that case, go to collections:
但是如果你需要更复杂的东西,我认为 numpy 不会提供一个不错的count选择。在这种情况下,请转到集合:
import collections
collections.Counter(y)
> Counter({0: 8, 1: 4})
This behaves like a dict
这就像一个字典
collections.Counter(y)[0]
> 8
回答by Milind Dumbare
Convert your array yto list land then do l.count(1)and l.count(0)
将您的数组转换y为列表l,然后执行l.count(1)和l.count(0)
>>> y = numpy.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
>>> l = list(y)
>>> l.count(1)
4
>>> l.count(0)
8
回答by Akavall
For your case you could also look into numpy.bincount
对于您的情况,您还可以查看numpy.bincount
In [56]: a = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
In [57]: np.bincount(a)
Out[57]: array([8, 4]) #count of zeros is at index 0 : 8
#count of ones is at index 1 : 4
回答by MaxG
I'd use np.where:
我会使用 np.where:
how_many_0 = len(np.where(a==0.)[0])
how_many_1 = len(np.where(a==1.)[0])
回答by Thomas
It involves one more step, but a more flexible solution which would also work for 2d arrays and more complicated filters is to create a boolean mask and then use .sum() on the mask.
它涉及更多的步骤,但也适用于二维数组和更复杂的过滤器的更灵活的解决方案是创建一个布尔掩码,然后在掩码上使用 .sum() 。
>>>>y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
>>>>mask = y == 0
>>>>mask.sum()
8
回答by Aziz Alto
What about using numpy.count_nonzero, something like
怎么样使用numpy.count_nonzero,像
>>> import numpy as np
>>> y = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>> np.count_nonzero(y == 1)
1
>>> np.count_nonzero(y == 2)
7
>>> np.count_nonzero(y == 3)
3
回答by Anas
What about len(y[y==0])and len(y[y==1])?
怎么样len(y[y==0])和len(y[y==1])?
回答by Gus Hecht
Personally, I'd go for:
(y == 0).sum()and (y == 1).sum()
就个人而言,我会选择:
(y == 0).sum()和(y == 1).sum()
E.g.
例如
import numpy as np
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
num_zeros = (y == 0).sum()
num_ones = (y == 1).sum()
回答by michael
y.tolist().count(val)
y.tolist().count(val)
with val 0 or 1
与 val 0 或 1
Since a python list has a native function count, converting to list before using that function is a simple solution.
由于python列表具有本机函数count,因此在使用该函数之前转换为列表是一个简单的解决方案。

