问题:如何计算Python中ndarray中某些项目的出现?
在Python中,我有一个ndarray y
打印为array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
我试图计算这个数组中有多少个0
和多少个1
。
但是当我输入y.count(0)
or时y.count(1)
,它说
numpy.ndarray
对象没有属性 count
我该怎么办?
In Python, I have an ndarray y
that is printed as array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
I’m trying to count how many 0
s and how many 1
s are there in this array.
But when I type y.count(0)
or y.count(1)
, it says
numpy.ndarray
object has no attribute count
What should I do?
回答 0
>>> 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}
非numpy方式:
使用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})
>>> 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})
回答 1
那使用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
What about using numpy.count_nonzero
, something like
>>> 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
回答 2
就个人而言,我会去:
(y == 0).sum()
和(y == 1).sum()
例如
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()
Personally, I’d go for:
(y == 0).sum()
and (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()
回答 3
对于您的情况,您还可以查看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
For your case you could also look into 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
回答 4
将数组转换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
Convert your array y
to list l
and then do l.count(1)
and 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
回答 5
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
如果你知道,他们只是0
和1
:
np.sum(y)
给你的数量。 np.sum(1-y)
给出零。
为了稍微概括起见,如果要计数0
而不是零(但可能是2或3):
np.count_nonzero(y)
给出非零的数量。
但是,如果您需要更复杂的东西,我认为numpy不会提供一个不错的count
选择。在这种情况下,请转到集合:
import collections
collections.Counter(y)
> Counter({0: 8, 1: 4})
这就像一个字典
collections.Counter(y)[0]
> 8
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
If you know that they are just 0
and 1
:
np.sum(y)
gives you the number of ones. np.sum(1-y)
gives the zeroes.
For slight generality, if you want to count 0
and not zero (but possibly 2 or 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 count
option. In that case, go to collections:
import collections
collections.Counter(y)
> Counter({0: 8, 1: 4})
This behaves like a dict
collections.Counter(y)[0]
> 8
回答 6
如果您确切知道要查找的号码,则可以使用以下代码;
lst = np.array([1,1,2,3,3,6,6,6,3,2,1])
(lst == 2).sum()
返回数组中发生2的次数。
If you know exactly which number you’re looking for, you can use the following;
lst = np.array([1,1,2,3,3,6,6,6,3,2,1])
(lst == 2).sum()
returns how many times 2 is occurred in your array.
回答 7
老实说,我发现将其转换为pandas系列或DataFrame最简单:
import pandas as pd
import numpy as np
df = pd.DataFrame({'data':np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])})
print df['data'].value_counts()
或罗伯特·穆伊(Robert Muil)提出的这一好话:
pd.Series([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]).value_counts()
Honestly I find it easiest to convert to a pandas Series or DataFrame:
import pandas as pd
import numpy as np
df = pd.DataFrame({'data':np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])})
print df['data'].value_counts()
Or this nice one-liner suggested by Robert Muil:
pd.Series([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]).value_counts()
回答 8
没有人建议使用带minlength = np.size(input)
,但它似乎是一个很好的解决方案,并且绝对是最快的:
In [1]: choices = np.random.randint(0, 100, 10000)
In [2]: %timeit [ np.sum(choices == k) for k in range(min(choices), max(choices)+1) ]
100 loops, best of 3: 2.67 ms per loop
In [3]: %timeit np.unique(choices, return_counts=True)
1000 loops, best of 3: 388 µs per loop
In [4]: %timeit np.bincount(choices, minlength=np.size(choices))
100000 loops, best of 3: 16.3 µs per loop
numpy.unique(x, return_counts=True)
和之间的疯狂加速numpy.bincount(x, minlength=np.max(x))
!
No one suggested to use with minlength = np.size(input)
, but it seems to be a good solution, and definitely the fastest:
In [1]: choices = np.random.randint(0, 100, 10000)
In [2]: %timeit [ np.sum(choices == k) for k in range(min(choices), max(choices)+1) ]
100 loops, best of 3: 2.67 ms per loop
In [3]: %timeit np.unique(choices, return_counts=True)
1000 loops, best of 3: 388 µs per loop
In [4]: %timeit np.bincount(choices, minlength=np.size(choices))
100000 loops, best of 3: 16.3 µs per loop
That’s a crazy speedup between numpy.unique(x, return_counts=True)
and numpy.bincount(x, minlength=np.max(x))
!
回答 9
怎么样len(y[y==0])
和len(y[y==1])
?
What about len(y[y==0])
and len(y[y==1])
?
回答 10
y.tolist().count(val)
使用val 0或1
由于python列表具有本机函数count
,因此在使用该函数之前将其转换为list是一个简单的解决方案。
y.tolist().count(val)
with val 0 or 1
Since a python list has a native function count
, converting to list before using that function is a simple solution.
回答 11
另一个简单的解决方案可能是使用numpy.count_nonzero():
import numpy as np
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
y_nonzero_num = np.count_nonzero(y==1)
y_zero_num = np.count_nonzero(y==0)
y_nonzero_num
4
y_zero_num
8
不要让名称误导您,如果您像示例中那样将其与布尔值一起使用,就可以解决问题。
Yet another simple solution might be to use numpy.count_nonzero():
import numpy as np
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
y_nonzero_num = np.count_nonzero(y==1)
y_zero_num = np.count_nonzero(y==0)
y_nonzero_num
4
y_zero_num
8
Don’t let the name mislead you, if you use it with the boolean just like in the example, it will do the trick.
回答 12
要计算出现次数,可以使用np.unique(array, return_counts=True)
:
In [75]: boo = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
# use bool value `True` or equivalently `1`
In [77]: uniq, cnts = np.unique(boo, return_counts=1)
In [81]: uniq
Out[81]: array([0, 1]) #unique elements in input array are: 0, 1
In [82]: cnts
Out[82]: array([8, 4]) # 0 occurs 8 times, 1 occurs 4 times
To count the number of occurrences, you can use np.unique(array, return_counts=True)
:
In [75]: boo = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
# use bool value `True` or equivalently `1`
In [77]: uniq, cnts = np.unique(boo, return_counts=1)
In [81]: uniq
Out[81]: array([0, 1]) #unique elements in input array are: 0, 1
In [82]: cnts
Out[82]: array([8, 4]) # 0 occurs 8 times, 1 occurs 4 times
回答 13
我会使用np.where:
how_many_0 = len(np.where(a==0.)[0])
how_many_1 = len(np.where(a==1.)[0])
I’d use np.where:
how_many_0 = len(np.where(a==0.)[0])
how_many_1 = len(np.where(a==1.)[0])
回答 14
利用系列提供的方法:
>>> import pandas as pd
>>> y = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]
>>> pd.Series(y).value_counts()
0 8
1 4
dtype: int64
take advantage of the methods offered by a Series:
>>> import pandas as pd
>>> y = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]
>>> pd.Series(y).value_counts()
0 8
1 4
dtype: int64
回答 15
一个简单的一般答案是:
numpy.sum(MyArray==x) # sum of a binary list of the occurence of x (=0 or 1) in MyArray
这将导致完整的代码作为示例
import numpy
MyArray=numpy.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) # array we want to search in
x=0 # the value I want to count (can be iterator, in a list, etc.)
numpy.sum(MyArray==0) # sum of a binary list of the occurence of x in MyArray
现在,如果MyArray具有多个维度,并且您要计算行中值分布的出现次数(此后为pattern)
MyArray=numpy.array([[6, 1],[4, 5],[0, 7],[5, 1],[2, 5],[1, 2],[3, 2],[0, 2],[2, 5],[5, 1],[3, 0]])
x=numpy.array([5,1]) # the value I want to count (can be iterator, in a list, etc.)
temp = numpy.ascontiguousarray(MyArray).view(numpy.dtype((numpy.void, MyArray.dtype.itemsize * MyArray.shape[1]))) # convert the 2d-array into an array of analyzable patterns
xt=numpy.ascontiguousarray(x).view(numpy.dtype((numpy.void, x.dtype.itemsize * x.shape[0]))) # convert what you search into one analyzable pattern
numpy.sum(temp==xt) # count of the searched pattern in the list of patterns
A general and simple answer would be:
numpy.sum(MyArray==x) # sum of a binary list of the occurence of x (=0 or 1) in MyArray
which would result into this full code as exemple
import numpy
MyArray=numpy.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) # array we want to search in
x=0 # the value I want to count (can be iterator, in a list, etc.)
numpy.sum(MyArray==0) # sum of a binary list of the occurence of x in MyArray
Now if MyArray is in multiple dimensions and you want to count the occurence of a distribution of values in line (= pattern hereafter)
MyArray=numpy.array([[6, 1],[4, 5],[0, 7],[5, 1],[2, 5],[1, 2],[3, 2],[0, 2],[2, 5],[5, 1],[3, 0]])
x=numpy.array([5,1]) # the value I want to count (can be iterator, in a list, etc.)
temp = numpy.ascontiguousarray(MyArray).view(numpy.dtype((numpy.void, MyArray.dtype.itemsize * MyArray.shape[1]))) # convert the 2d-array into an array of analyzable patterns
xt=numpy.ascontiguousarray(x).view(numpy.dtype((numpy.void, x.dtype.itemsize * x.shape[0]))) # convert what you search into one analyzable pattern
numpy.sum(temp==xt) # count of the searched pattern in the list of patterns
回答 16
您可以使用字典理解来创建整齐的单线。可以在这里找到有关字典理解的更多信息
>>>counts = {int(value): list(y).count(value) for value in set(y)}
>>>print(counts)
{0: 8, 1: 4}
这将创建一个字典,将ndarray中的值作为键,并将值的计数分别作为键的值。
每当您要计算此格式数组中某个值的出现次数时,此方法都将起作用。
You can use dictionary comprehension to create a neat one-liner. More about dictionary comprehension can be found here
>>>counts = {int(value): list(y).count(value) for value in set(y)}
>>>print(counts)
{0: 8, 1: 4}
This will create a dictionary with the values in your ndarray as keys, and the counts of the values as the values for the keys respectively.
This will work whenever you want to count occurences of a value in arrays of this format.
回答 17
尝试这个:
a = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
list(a).count(1)
Try this:
a = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
list(a).count(1)
回答 18
这可以通过以下方法轻松完成
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
y.tolist().count(1)
This can be done easily in the following method
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
y.tolist().count(1)
回答 19
由于您的ndarray仅包含0和1,因此您可以使用sum()获得1的出现,并使用len()-sum()获得0的出现。
num_of_ones = sum(array)
num_of_zeros = len(array)-sum(array)
Since your ndarray contains only 0 and 1, you can use sum() to get the occurrence of 1s and len()-sum() to get the occurrence of 0s.
num_of_ones = sum(array)
num_of_zeros = len(array)-sum(array)
回答 20
您有一个只有1和0的特殊数组。所以一个诀窍是使用
np.mean(x)
这将为您提供数组中1s的百分比。或者,使用
np.sum(x)
np.sum(1-x)
将为您提供数组中1和0的绝对数。
You have a special array with only 1 and 0 here. So a trick is to use
np.mean(x)
which gives you the percentage of 1s in your array. Alternatively, use
np.sum(x)
np.sum(1-x)
will give you the absolute number of 1 and 0 in your array.
回答 21
dict(zip(*numpy.unique(y, return_counts=True)))
刚刚在此处复制了Seppo Enarvi的评论,这应该是一个正确的答案
dict(zip(*numpy.unique(y, return_counts=True)))
Just copied Seppo Enarvi’s comment here which deserves to be a proper answer
回答 22
它涉及更多的步骤,但是对2d数组和更复杂的过滤器也适用的更灵活的解决方案是创建一个布尔掩码,然后在掩码上使用.sum()。
>>>>y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
>>>>mask = y == 0
>>>>mask.sum()
8
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.
>>>>y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
>>>>mask = y == 0
>>>>mask.sum()
8
回答 23
如果您不想使用numpy或collections模块,则可以使用字典:
d = dict()
a = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]
for item in a:
try:
d[item]+=1
except KeyError:
d[item]=1
结果:
>>>d
{0: 8, 1: 4}
当然,您也可以使用if / else语句。我认为Counter函数的功能几乎相同,但这更加透明。
If you don’t want to use numpy or a collections module you can use a dictionary:
d = dict()
a = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]
for item in a:
try:
d[item]+=1
except KeyError:
d[item]=1
result:
>>>d
{0: 8, 1: 4}
Of course you can also use an if/else statement. I think the Counter function does almost the same thing but this is more transparant.
回答 24
对于通用条目:
x = np.array([11, 2, 3, 5, 3, 2, 16, 10, 10, 3, 11, 4, 5, 16, 3, 11, 4])
n = {i:len([j for j in np.where(x==i)[0]]) for i in set(x)}
ix = {i:[j for j in np.where(x==i)[0]] for i in set(x)}
将输出一个计数:
{2: 2, 3: 4, 4: 2, 5: 2, 10: 2, 11: 3, 16: 2}
和索引:
{2: [1, 5],
3: [2, 4, 9, 14],
4: [11, 16],
5: [3, 12],
10: [7, 8],
11: [0, 10, 15],
16: [6, 13]}
For generic entries:
x = np.array([11, 2, 3, 5, 3, 2, 16, 10, 10, 3, 11, 4, 5, 16, 3, 11, 4])
n = {i:len([j for j in np.where(x==i)[0]]) for i in set(x)}
ix = {i:[j for j in np.where(x==i)[0]] for i in set(x)}
Will output a count:
{2: 2, 3: 4, 4: 2, 5: 2, 10: 2, 11: 3, 16: 2}
And indices:
{2: [1, 5],
3: [2, 4, 9, 14],
4: [11, 16],
5: [3, 12],
10: [7, 8],
11: [0, 10, 15],
16: [6, 13]}
回答 25
这里有一些东西,通过它您可以计算出特定数字的出现次数:根据您的代码
count_of_zero = list(y [y == 0])。count(0)
打印(count_of_zero)
//根据匹配项,将有布尔值,根据True值,将返回数字0
here I have something, through which you can count the number of occurrence of a particular number: according to your code
count_of_zero=list(y[y==0]).count(0)
print(count_of_zero)
// according to the match there will be boolean values and according to True value the number 0 will be return
回答 26
如果您对最快的执行感兴趣,那么您会事先知道要查找的值,并且您的数组是一维的,否则您对展平数组上的结果感兴趣(在这种情况下,函数的输入应是np.flatten(arr)
不是只arr
),然后Numba是你的朋友:
import numba as nb
@nb.jit
def count_nb(arr, value):
result = 0
for x in arr:
if x == value:
result += 1
return result
或者,对于超大型阵列,并行化可能会有所帮助:
@nb.jit(parallel=True)
def count_nbp(arr, value):
result = 0
for i in nb.prange(arr.size):
if arr[i] == value:
result += 1
return result
对这些基准进行基准测试np.count_nonzero()
(也存在创建可以避免的临时数组的问题)和np.unique()
基于-的解决方案
import numpy as np
def count_np(arr, value):
return np.count_nonzero(arr == value)
import numpy as np
def count_np2(arr, value):
uniques, counts = np.unique(a, return_counts=True)
counter = dict(zip(uniques, counts))
return counter[value] if value in counter else 0
用于使用以下命令生成的输入:
def gen_input(n, a=0, b=100):
return np.random.randint(a, b, n)
获得以下图(图的第二行是对更快方法的放大):
表明基于Numba的解决方案比NumPy的解决方案明显更快,并且对于非常大的输入,并行方法比朴素的方法要快。
完整的代码在这里。
If you are interested in the fastest execution, you know in advance which value(s) to look for, and your array is 1D, or you are otherwise interested in the result on the flattened array (in which case the input of the function should be np.flatten(arr)
rather than just arr
), then Numba is your friend:
import numba as nb
@nb.jit
def count_nb(arr, value):
result = 0
for x in arr:
if x == value:
result += 1
return result
or, for very large arrays where parallelization may be beneficial:
@nb.jit(parallel=True)
def count_nbp(arr, value):
result = 0
for i in nb.prange(arr.size):
if arr[i] == value:
result += 1
return result
Benchmarking these against np.count_nonzero()
(which also has a problem of creating a temporary array which may be avoided) and np.unique()
-based solution
import numpy as np
def count_np(arr, value):
return np.count_nonzero(arr == value)
import numpy as np
def count_np2(arr, value):
uniques, counts = np.unique(a, return_counts=True)
counter = dict(zip(uniques, counts))
return counter[value] if value in counter else 0
for input generated with:
def gen_input(n, a=0, b=100):
return np.random.randint(a, b, n)
the following plots are obtained (the second row of plots is a zoom on the faster approach):
Showing that Numba-based solution are noticeably faster than the NumPy counterparts, and, for very large inputs, the parallel approach is faster than the naive one.
Full code available here.
回答 27
如果使用生成器处理非常大的数组,则可以选择。令人高兴的是,这种方法对数组和列表都适用,并且您不需要任何其他程序包。此外,您没有使用太多的内存。
my_array = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
sum(1 for val in my_array if val==0)
Out: 8
if you are dealing with very large arrays using generators could be an option. The nice thing here it that this approach works fine for both arrays and lists and you dont need any additional package. Additionally, you are not using that much memory.
my_array = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
sum(1 for val in my_array if val==0)
Out: 8
回答 28
Numpy为此提供了一个模块。只是一个小技巧。将您的输入数组作为垃圾箱。
numpy.histogram(y, bins=y)
输出是2个数组。一个带有值本身,另一个带有相应的频率。
Numpy has a module for this. Just a small hack. Put your input array as bins.
numpy.histogram(y, bins=y)
The output are 2 arrays. One with the values itself, other with the corresponding frequencies.
回答 29
using numpy.count
$ a = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]
$ np.count(a, 1)
using numpy.count
$ a = [0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]
$ np.count(a, 1)
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。