简体   繁体   English

numpy:FutureWarning:元素比较失败

[英]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.

相关问题 FutureWarning:元素比较失败; 返回标量,但将来将执行元素比较 - FutureWarning: elementwise comparison failed; returning scalar, but in the future will perform elementwise comparison 熊猫合并失败并显示“ FutureWarning:逐元素比较失败” - Pandas merge failing with “FutureWarning: elementwise comparison failed” FutureWarning:元素比较失败; 而是返回标量 - FutureWarning: elementwise comparison failed; returning scalar instead Python loc + isin 返回 FutureWarning(逐元素比较失败) - Python loc + isin returns FutureWarning (elementwise comparison failed) FutureWarning:元素比较失败; 而是返回标量,但将来将执行元素比较 - FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison 为什么我得到这个错误 result = method(y) FutureWarning: elementwise compare failed; 而是返回标量 - why i get this error result = method(y) FutureWarning: elementwise comparison failed; returning scalar instead 如何使用 OR 运算符对 Pandas Dataframe 进行子集化,同时避免“FutureWarning:元素比较失败;” - How to subset Pandas Dataframe using an OR operator whilst avoiding "FutureWarning: elementwise comparison failed;" 在 scikit learn 中准备和可视化分类报告时出现错误“FutureWarning: elementwise comparison failed” - Error “FutureWarning: elementwise comparison failed” while preparing and visualizing classification report in scikit learn FutureWarning:元素比较失败; 从 pandas dataframe 删除所有行时 - FutureWarning: elementwise comparison failed; when dropping all rows from pandas dataframe numpy:FutureWarning:与`in`语句的`None`比较 - numpy: FutureWarning: comparison to `None` with `in` statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM