Python-numpy逻辑报错:The truth value of an array with more than one element is ambiguous. Us
来源:互联网 发布:via浏览器 知乎 编辑:程序博客网 时间:2024/05/17 03:42
报错代码:
import numpy as numpya=np.zeros(3)a[0]=0; a[1]=1; a[2]=2if a==[1,2,3]:print "OK"else:print "NOT OK"Traceback (most recent call last): File "<pyshell#45>", line 1, in <module> if a==[1,2,3]:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()报错原因:
Numpy对逻辑表达式判别不清楚,它可以返回False如果等号两边两个式子是数值相等,也可以返回True因为等号两边两个式子是逻辑相等。它觉得这是模棱两可的,因此放弃做判断,统一用a.any()进行或比较,或a.all()进行与比较。可以从下面例子体会一下。
纠正演示:
import numpy as numpya=np.zeros(3)a[0]=0; a[1]=1; a[2]=2print (a-[0,1,2]).any()#[0,0,0] Falseprint (a-[0,1,2]).all()#[0,0,0] Falseprint (a-[1,2,3]).any() #[-1,-1,-1] Trueprint (a-[1,2,3]).all() #[-1,-1,-1] Trueprint (a-[0,2,3]).any() #[0,-1,-1] Trueprint (a-[0,2,3]).all() #[0,-1,-1] False
自己的记法为:
numpy-array数组进行(a-b)比较时,True表示不同,False表示相同
部分元素相等,.all()返False(一帮情况下不希望出现),.any()返回True; 所有元素都相等,二者均返回False
因此最好使用.any()比较
0 0
- Python-numpy逻辑报错:The truth value of an array with more than one element is ambiguous. Us
- Python-numpy逻辑报错:The truth value of an array with more than one element is ambiguous
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- Hibernate 级联查询: More than one row with the given identifier was found 报错
- Mysql报错:Result consisted of more than one row
- An AVPlayerItem cannot be associated with more than one instance of AVPlayer
- An AVPlayerItem cannot be associated with more than one instance of AVPlayer
- An AVPlayerItem cannot be associated with more than one instance of AVPlayer'
- An AVPlayerItem cannot be associated with more than one instance of AVPlayer
- The server timezone value 'UTC' is unrecognized or represents more than one timezone
- The server time zone value 'EDT' is unrecognized or represents more than one time zone.
- Mysql错误:The server time zone value is unrecognized or represents more than one time zone
- The server time zone value 'PDT' is unrecognized or represents more than one time zone
- The server timezone value 'xxx' is unrecognized or represents more than one time
- The server time zone value '?????' more than one time zone
- tensorflow报错:setting an array element with a sequence
- 让人哭笑不得的 The value of attribute "value" associated with an element type "prope
- mysql出现错误:The server time zone value is unrecognized or represents more than one time zone
- 问题集(一)之“未将对象引用设置到对象的实例”
- 学习Discuz! X3.2记录:改变幻灯片播放形式
- 阮一峰 - 博客十年
- POJ NO.3984 迷宫问题(BFS,保存路径)
- 线程同步和线程安全
- Python-numpy逻辑报错:The truth value of an array with more than one element is ambiguous. Us
- 1026. 程序运行时间(15)
- Diksam代码阅读(2)——Diksam编译运行的主要流程
- 微信开发基础教程
- CSS3新增颜色属性
- iPhone应用打包和上传APPstore
- 获取LayoutInflater实例
- Struts2的标签库(二)之数据标签
- vim+python+OpenCV学习三 : 对图像的像素处理