我是靠谱客的博主 清爽大象,最近开发中收集的这篇文章主要介绍python nan的数量_在Python中计算一个numpy ndarray中非NaN元素的数量,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

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

Thnx for any help!

解决方案np.count_nonzero(~np.isnan(data))

~ inverts the boolean matrix returned from np.isnan.

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

Testing speed:

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)) seems to barely be the fastest here. other data might give different relative speed results.

最后

以上就是清爽大象为你收集整理的python nan的数量_在Python中计算一个numpy ndarray中非NaN元素的数量的全部内容,希望文章能够帮你解决python nan的数量_在Python中计算一个numpy ndarray中非NaN元素的数量所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部