我正在做一個來自七個模型的預測概率的集合。每個模型輸出三個類別。我事先計算了七個模型預測的權重。這些預測的權重被存盤在變數 "prediction_weights "中。加權平均的代碼如下:
prediction_weights = np.array([3. 66963025e-01, 1.08053256e-01,1.14617370e-01, 4.10366349e-01,
6.16391075e-14, 4.37376684e-14, 9.26785075e-18]] )
weighted_predictions7 = np.zeros((nb_test_samples, num_classes),
dtype='float32')
for weight, prediction in zip(prediction_weights, preds)。
weighted_predictions7 = weight * prediction
yPred7 = np.argmax(weighted_predictions7, axis=1)
yTrue = Y_test.argmax(axis=-1)
accuracy = metrics.accuracy_score(yTrue, yPred7) * 100
np.savetxt('weighted_averaging_7_y_pred.csv',
weighted_predictions7,fmt='%f'/span>,
delimiter = ",")
我得到了以下錯誤:
檔案"<ipython-input-16-8f3a15c0fec1>"/span>,行2,in<module>。
weighted_predictions7 = weight * prediction
ValueError: 運算元不能不一起廣播與形狀(7,) (624,3)
下面是變數的形狀:
prediction_weights: (1,7) - Float 64陣列
nb_test_samples: 1 - int - nb_test_samples.
num_classes: 1 - int num_classes.
weighted_predictions7: (624,3) - float32的陣列。
Y_test: (624,3) - float32的陣列
yTrue: (624,) - 陣列的Int64
uj5u.com熱心網友回復:
簡單的替換
prediction_weights = np.array([3. 66963025e-01, 1.08053256e-01,1.14617370e-01, 4.10366349e-01,
6.16391075e-14, 4.37376684e-14, 9.26785075e-18] ])
by
prediction_weights = [3. 66963025e-01, 1.08053256e-01,1.14617370e-01, 4.10366349e-01,
6.16391075e-14, 4.37376684e-14, 9.26785075e-18]
解決了錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/316180.html
標籤:
下一篇:在Python中操作函式中的變數
