“不是” numpy 中的功能,这可以更 Pythonic 吗?
我需要一个从数组返回非 NaN 值的函数。目前我正在这样做:
>>> a = np.array([np.nan, 1, 2])
>>> a
array([ NaN, 1., 2.])
>>> np.invert(np.isnan(a))
array([False, True, True], dtype=bool)
>>> a[np.invert(np.isnan(a))]
array([ 1., 2.])
Python:2.6.4 numpy:1.3.0
如果您知道更好的方法,请分享, 谢谢
I need a function that returns non-NaN values from an array. Currently I am doing it this way:
>>> a = np.array([np.nan, 1, 2])
>>> a
array([ NaN, 1., 2.])
>>> np.invert(np.isnan(a))
array([False, True, True], dtype=bool)
>>> a[np.invert(np.isnan(a))]
array([ 1., 2.])
Python: 2.6.4
numpy: 1.3.0
Please share if you know a better way,
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您当前正在测试任何非 NaN 的内容,并且 mtrw 有正确的方法来执行此操作。如果您有兴趣测试有限数(不是 NaN 也不是 INF),那么您不需要反转,可以使用:
更多 Pythonic 和本机,易于阅读,并且通常当您想避免 NaN 时,您也想要根据我的经验,避免 INF。
只是想我会把它扔给人们。
You are currently testing for anything that is not NaN and mtrw has the right way to do this. If you are interested in testing for finite numbers (is not NaN and is not INF) then you don't need an inversion and can use:
More pythonic and native, an easy read, and often when you want to avoid NaN you also want to avoid INF in my experience.
Just thought I'd toss that out there for folks.
从数组获取
array([ 1., 2.])
arr = np.array([np.nan, 1, 2])
你可以这样做:
OR
(而:np.nan == np.nan是False)
To get
array([ 1., 2.])
from an arrayarr = np.array([np.nan, 1, 2])
You can do :
OR
(While : np.nan == np.nan is False)
我不确定这是否或多或少是Pythonic的......
I'm not sure whether this is more or less pythonic...