ValueError:在进行加权预测时,操作数无法与形状(7,)(624,3)一起广播

2024-09-30 12:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做七个模型的预测概率的集合。每个模型输出三个类。我计算了七个模型预测的权重,这些预测的权重存储在变量“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',
            delimiter = ",")

我得到以下错误:

  File "<ipython-input-16-8f3a15c0fec1>", line 2, in <module>
    weighted_predictions7 += weight * prediction

ValueError: operands could not be broadcast together with shapes (7,) (624,3) 

以下是变量的形状:

    prediction_weights: (1,7) - Array of Float 64
    nb_test_samples: 1 - int
    num_classes: 1 - int
    weighted_predictions7: (624,3) - Array of float32
    Y_test: (624,3) - Array of float32
    yTrue: (624,) - Array of Int64

Tags: of模型testnparray权重samplesprediction
1条回答
网友
1楼 · 发布于 2024-09-30 12:29:36

简单替换

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]]) 

prediction_weights = [3.66963025e-01, 1.08053256e-01,1.14617370e-01, 4.10366349e-01,
 6.16391075e-14, 4.37376684e-14, 9.26785075e-18] 

解决了错误

相关问题 更多 >

    热门问题