我有一个python代码:
...
bestProb=-1;
bestIndex=-1;
prob=np.zeros(numClasses) #numClasses=2
for i in range(0,numClasses):
prob[i]=findProb(x,weights,b,i)
for i in range(0,numClasses):
if prob[i]>bestProb:
bestProb=prob
bestIndex=i
...
课程数量为2。当我运行此代码时,我得到错误:
File "regression.py", line 19, in predict
if prob[i]>bestProb:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我知道,如果我将整个数组与单个数字进行比较,会出现此错误,但我在写入prob[i]
时不是只使用一个数字吗?如果我错了,请告诉我该怎么修。谢谢!你知道吗
另外,请注意findProb()给出的是一个数字,而不是序列。所以这也不是什么问题。你知道吗
编辑:问题出在bestProb=prob
。应该是bestProb=prob[i]
。抱歉给你添麻烦了。谢谢!你知道吗
您需要调试
Prob[i]
变量。尝试在if
语句之前添加print(Prob[i])
,以观察Prob[i]
的内容。有时,它只是一个元素的数组,您只需将其Prob[i][0]
。如果这不起作用,那么你需要检查你的逻辑。你知道吗相关问题 更多 >
编程相关推荐