[英]numpy: FutureWarning: elementwise comparison failed
Is there any resolution in numpy with regards to the issue described in this SO post FutureWarning: elementwise comparison failed;关于此 SO post FutureWarning: elementwise comparison failed 中描述的问题,numpy 中是否有任何解决方案; returning scalar, but in the future will perform elementwise comparison . 返回标量,但将来会执行元素比较。 My numpy version is 1.19.1 and using python 3.8.5 .我的 numpy 版本是1.19.1并使用 python 3.8.5 。
a = np.array(['aug', False, False, False])
a == 'aug'
array([ True, False, False, False])
But:但:
a == False
<ipython-input-88-f9ff25cfe387>:1: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
a == False
Same with np.nan:与 np.nan 相同:
a = array(['aug', np.nan, np.nan, np.nan])
a == 'aug'
array([ True, False, False, False])
But:但:
a == np.nan
<ipython-input-1236-9224919e9367>:1: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
a == np.nan
False
Look at the arrays after you create them:创建数组后查看数组:
In [58]: a = np.array(['aug', False, False, False])
...:
In [59]: a
Out[59]: array(['aug', 'False', 'False', 'False'], dtype='<U5')
In [60]: a == 'aug'
Out[60]: array([ True, False, False, False])
In [61]: a == False
<ipython-input-61-f9ff25cfe387>:1: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
a == False
Out[61]: False
In [62]: a == 'False'
Out[62]: array([False, True, True, True])
It's a string dtype array.这是一个字符串数据类型数组。 Testing for matching strings works.测试匹配字符串有效。 Testing for a nonstring value is wrong.测试非字符串值是错误的。
Same for nan
: nan
:
In [64]: a = np.array(['aug', np.nan, np.nan, np.nan])
In [65]: a
Out[65]: array(['aug', 'nan', 'nan', 'nan'], dtype='<U3')
In [66]: a == 'nan'
Out[66]: array([False, True, True, True])
If you must mix types - string, boolean, float, you can specify object
dtype.如果您必须混合类型 - 字符串、布尔值、浮点数,您可以指定object
dtype。 That makes the array more list-like.这使得数组更像列表。
In [67]: a = np.array(['aug', False, False, False], object)
In [68]: a
Out[68]: array(['aug', False, False, False], dtype=object)
In [69]: a == 'aug'
Out[69]: array([ True, False, False, False])
In [70]: a == False
Out[70]: array([False, True, True, True])
In [71]: a == True
Out[71]: array([False, False, False, False])
But it doesn't help with np.nan
.但这对np.nan
没有帮助。 nan
is a special kind of float that isn't equal to anything, not even itself. nan
是一种特殊的浮点数,它不等于任何东西,甚至不等于它本身。
In [72]: a = np.array(['aug', np.nan, np.nan, np.nan], object)
In [73]: a
Out[73]: array(['aug', nan, nan, nan], dtype=object)
In [74]: a=='aug'
Out[74]: array([ True, False, False, False])
In [75]: a == np.nan
Out[75]: array([False, False, False, False])
isnan
is the correct way to test for nan
, but it only works with float dtype arrays: isnan
是测试nan
的正确方法,但它仅适用于 float dtype 数组:
In [76]: np.isnan(a)
Traceback (most recent call last):
File "<ipython-input-76-da86cb21b8a4>", line 1, in <module>
np.isnan(a)
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.