Python-numpy逻辑报错:The truth value of an array with more than one element is ambiguous
来源:互联网 发布:彻地钉db数据 编辑:程序博客网 时间:2024/05/19 20: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()比较
本文转自:http://m.blog.csdn.net/article/details?id=50708302 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
- 排序的稳定性和时间复杂度
- STM32单片机程序启动
- 第十周—小学生数学考试系统2
- TextView使用SpannableString设置复合文本
- 定义几个常见的术语(tkinter,Python3.x)
- Python-numpy逻辑报错:The truth value of an array with more than one element is ambiguous
- Android Context完全解析,你所不知道的Context的各种细节
- 【leetcode】199. Binary Tree Right Side Viewti
- split命令分割大文件
- mvp下RecyclerView下拉刷新上拉加载Demo
- iOS多语言本地化(国际化)设置
- sklearn.model_selection的StratifiedKFold实例
- 排序——规则
- git add -A和 git add . git add -u区别