【发布时间】:2021-03-04 21:14:41
【问题描述】:
对于此 SO 帖子 FutureWarning: elementwise comparison failed; returning scalar, but in the future will perform elementwise comparison 中描述的问题,numpy 中是否有任何解决方案。我的 numpy 版本是 1.19.1 并使用 python 3.8.5。
a = np.array(['aug', False, False, False])
a == 'aug'
array([ True, False, False, False])
但是:
a == False
<ipython-input-88-f9ff25cfe387>:1: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
a == False
与 np.nan 相同:
a = array(['aug', np.nan, np.nan, np.nan])
a == 'aug'
array([ True, False, False, False])
但是:
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
【问题讨论】:
-
试试
a[:,None,None] == 'False' -
a是字符串 dtype。你为什么要测试 bool 对象,False或 float 对象nan反对呢?字符串比较返回一个匹配形状的布尔数组(为什么没有给问题增加任何东西的额外维度?)。失败的比较返回带有警告的标量 False。您的a数组不等于False或nan。 -
@hpaulj 我清理了我原来的问题。目前尚不清楚为什么 numpy 无法区分具有字符串元素的数组中的布尔值或 np.nan。
-
您还没有费心查看您的
a数组,是吗?