2 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
混淆矩陣需要將標(biāo)簽和預(yù)測作為個(gè)位數(shù),而不是作為單熱編碼向量;盡管您已經(jīng)使用model.predict_classes(),即
rounded_predictions = model.predict_classes(test_images, batch_size=128, verbose=0)
rounded_predictions[1]
# 2
你test_labels仍然是one-hot編碼:
test_labels[1]
# array([0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
因此,您也應(yīng)該將它們轉(zhuǎn)換為個(gè)位數(shù),如下所示:
import numpy as np
rounded_labels=np.argmax(test_labels, axis=1)
rounded_labels[1]
# 2
之后,混淆矩陣應(yīng)該會出現(xiàn):
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(rounded_labels, rounded_predictions)
cm
# result:
array([[ 971, 0, 0, 2, 1, 0, 2, 1, 3, 0],
[ 0, 1121, 2, 1, 0, 1, 3, 0, 7, 0],
[ 5, 4, 990, 7, 5, 3, 2, 7, 9, 0],
[ 0, 0, 0, 992, 0, 2, 0, 7, 7, 2],
[ 2, 0, 2, 0, 956, 0, 3, 3, 2, 14],
[ 3, 0, 0, 10, 1, 872, 3, 0, 1, 2],
[ 5, 3, 1, 1, 9, 10, 926, 0, 3, 0],
[ 0, 7, 10, 1, 0, 2, 0, 997, 1, 10],
[ 5, 0, 3, 7, 5, 7, 3, 4, 937, 3],
[ 5, 5, 0, 9, 10, 3, 0, 8, 3, 966]])
添加回答
舉報(bào)