簡體   English   中英

值錯誤:具有多個元素的數組的真值不明確

[英]Value error: The truth value of an array with more than one element is ambiguous

我有一個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] 如果那不起作用,那么您需要檢查您的邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM