[英]Counting non zeros in only 1 column of a numpy array
I have a Numpy array that is created as follows 我有一个创建如下的Numpy数组
data=np.zeros(500,dtype='float32, (50000,2)float32')
This array is filled with values that I acquire from some measurements, and is supposed to reflect that during each time point (room for 500 time points) we can acquire 50.000 x- and y- coords. 这个数组充满了我从一些测量中获得的值,并且应该反映出在每个时间点(500个时间点的空间)内,我们可以获取50.000 x和y坐标。
Later in my code is use a bisect
-like search for which I need to know howmany X-coords (measurement points) are actually in my array which I originally did with np.count_nonzero(data)
, this yielded the following problem: 稍后在我的代码中使用类似
bisect
的搜索,为此我需要知道实际上我最初使用np.count_nonzero(data)
数组中实际上有多少X坐标(测量点np.count_nonzero(data)
,这产生了以下问题:
Fake data:
1 1
2 2
3 0
4 4
5 0
6 6
7 7
8 8
9 9
10 10
the non zero count returns 18 values here, the code then goes into the bisect
-like search using data[time][1][0][0]
as min X-coord and data[time][1][(np.count_nonzero(data)][0]
as max x-coord which results in the array stopping at 9 instead of 10. 非零计数这里返回18倍的值,代码然后进入
bisect
样使用搜索data[time][1][0][0]
为min X-坐标和data[time][1][(np.count_nonzero(data)][0]
作为最大x坐标,导致数组从9而不是10停止。
I could use a while loop to manually count non-zero values (in the X-coord column) in the array but that would be silly, I assume that there is some builtin numpy functionality for this. 我可以使用while循环来手动计算数组中的非零值(在X坐标列中),但这很愚蠢,我假设有一些内置的numpy功能。 My question is then what builtin functionality or modification of my
np.count_nonzero(data)
I need since the documentation doesn't offer much information in that regards ( link to numpy doc). 然后我的问题是我需要什么内置功能或对我的
np.count_nonzero(data)
进行修改,因为文档在这方面没有提供太多信息( 链接到numpy doc)。
-- Simplified question -- -简化的问题-
Can I use Numpy functionality to count the non-zero values for a singular column only? 我可以使用Numpy功能为单个列计算非零值吗? (ie between
data[time][1][0][0]
and data[time][1][max][0]
) (即在
data[time][1][0][0]
和data[time][1][max][0]
)
If I understand you correctly, to select elements from data[time][1][0][0]
to data[time][1][max][0]
: 如果我理解正确,请从
data[time][1][0][0]
到data[time][1][max][0]
选择元素:
data[time][1][:max+1,0]
EDIT : 编辑 :
To count all non-zero for every time: 要每次都计算所有非零值:
(data["f1"][:,:,0] != 0).sum(1)
Why not consider using data != 0
to get the bool
matrix? 为什么不考虑使用
data != 0
来获取bool
矩阵?
You can use: 您可以使用:
stat = sum(data != 0)
to count the non-zero entries. stat = sum(data != 0)
计算非零条目。
I am not sure what shape your data
array has but hope you can see what I mean. 我不确定
data
数组的形状,但希望您能明白我的意思。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.