您现在的位置是:首页 > 电脑 > 

如何使用numpy返回数组中某些值的数量(how to return number of certain values in an array using numpy)

2025-07-18 00:12:36
如何使用numpy返回数组中某些值的数量(how to return number of certain values in an array using numpy) 我需要返回第列的非合理(超范围或超出范围)值的数量,其中0为空白。 我必须在一个真正的问题中处理一个csv文件,但我刚刚创建了一个ndarray。 data = np.array([[
如何使用numpy返回数组中某些值的数量(how to return number of certain values in an array using numpy)

我需要返回第列的非合理(超范围或超出范围)值的数量,其中0为空白。 我必须在一个真正的问题中处理一个csv文件,但我刚刚创建了一个ndarray。

data = np.array([[ 1, 2000, 14, 4546], [ 2, 1999, 246, 0], [ , 2008, 190, ], [ 4, 2000, 100, 0]])

我甚至不敢想我应该从哪里开始。

如果有人可以提供帮助,那将是很棒的。

I need to return the number of non-reasonable (nan or out of range) values for the rd column where has 0s an a blank in it. I have to deal with a csv file in a real problem but I just created a ndarray for now.

data = np.array([[ 1, 2000, 14, 4546], [ 2, 1999, 246, 0], [ , 2008, 190, ], [ 4, 2000, 100, 0]])

I cant even think where I should start.

It will be awesome if someone can help.

最满意答案

首先,您需要能够只访问您感兴趣的列。使用切片执行此操作:

data[:,2] # grab all rows, and just the column with index 2

现在您想要计算a的出现次数:

_nonzero(np.isnan(data[:,2]))

我们想要计算零元素的数量:

data[:,2].size - _nonzero(data[:,2])

如果我们将它们加在一起:

data[:,2].size - _nonzero(data[:,2]) + _nonzero(np.isnan(data[:,2]))

但这很无聊,因为第列中没有任何0或a 。 让我们尝试使用最后一列:

>>> slice = data[:,] >>> slice.size - _nonzero(slice) + _nonzero(np.isnan(slice))

编辑我应该解释为什么这有效:

np.isnan(data[:,2])根据它是否为a给出一个True和False数组。 如果为数字,则将其转换为1 ,将False is converted to 0, so the _nonzero call counts the number of which represent the a`值call counts the number of 1 which represent the数字。

_nonzero(data[:,2])直接计算非零数。 如果我们从元素总数中减去非零元素的数量,我们将得到0的数量。

First, you need to be able to access just the column that you're interested in. Do this with a slice:

data[:,2] # grab all rows, and just the column with index 2

ow you want to count the occurrences that are a:

_nonzero(np.isnan(data[:,2]))

And we want to count the number of zero elements:

data[:,2].size - _nonzero(data[:,2])

And if we add those together:

data[:,2].size - _nonzero(data[:,2]) + _nonzero(np.isnan(data[:,2]))

This is boring, though, since the rd column doesn't have any 0 or a in it. Lets try with the last column:

>>> slice = data[:,] >>> slice.size - _nonzero(slice) + _nonzero(np.isnan(slice))

edit I should explain why this works:

np.isnan(data[:,2]) gives an array of True and False based on if it's a a or not. True, when treated as a number, is converted to 1 and False is converted to0so the_nonzerocall counts the number of1which represent thea` values.

_nonzero(data[:,2]) counts the number of non-zero directly. If we subtract the number of non-zero elements from the total number of elements, we'll get the number of 0s.

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/50296.html

相关标签:无
上传时间: 2023-04-14 14:39:40
留言与评论(共有 7 条评论)
本站网友 梦到找不到回家的路
8分钟前 发表
本站网友 要命的小方
8分钟前 发表
_nonzero(data[
本站网友 ume安贞
17分钟前 发表
2] # grab all rows
本站网友 银行贷款担保人
28分钟前 发表
1999
本站网友 人体营养素
12分钟前 发表
np.isnan(data[
本站网友 21454小游戏
13分钟前 发表
2]) + _nonzero(np.isnan(data[