在Python中计算numpy ndarray中非NaN元素的数量 [英] Counting the number of non-NaN elements in a numpy ndarray in Python

查看:1549
本文介绍了在Python中计算numpy ndarray中非NaN元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算一个numpy ndarray矩阵中非NaN元素的数量.如何在Python中有效地做到这一点?这是实现此目的的简单代码:

I need to calculate the number of non-NaN elements in a numpy ndarray matrix. How would one efficiently do this in Python? Here is my simple code for achieving this:

import numpy as np

def numberOfNonNans(data):
    count = 0
    for i in data:
        if not np.isnan(i):
            count += 1
    return count 

在numpy中是否有为此内置的函数?效率很重要,因为我正在进行大数据分析.

Is there a built-in function for this in numpy? Efficiency is important because I'm doing Big Data analysis.

感谢任何帮助!

推荐答案

np.count_nonzero(~np.isnan(data))

~反转从np.isnan返回的布尔矩阵.

~ inverts the boolean matrix returned from np.isnan.

np.count_nonzero计算不为0 \ false的值. .sum应该给出相同的结果.但也许更清楚地使用count_nonzero

np.count_nonzero counts values that is not 0\false. .sum should give the same result. But maybe more clearly to use count_nonzero

测试速度:

In [23]: data = np.random.random((10000,10000))

In [24]: data[[np.random.random_integers(0,10000, 100)],:][:, [np.random.random_integers(0,99, 100)]] = np.nan

In [25]: %timeit data.size - np.count_nonzero(np.isnan(data))
1 loops, best of 3: 309 ms per loop

In [26]: %timeit np.count_nonzero(~np.isnan(data))
1 loops, best of 3: 345 ms per loop

In [27]: %timeit data.size - np.isnan(data).sum()
1 loops, best of 3: 339 ms per loop

data.size - np.count_nonzero(np.isnan(data))似乎在这里几乎不是最快的.其他数据可能会给出不同的相对速度结果.

data.size - np.count_nonzero(np.isnan(data)) seems to barely be the fastest here. other data might give different relative speed results.

这篇关于在Python中计算numpy ndarray中非NaN元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆