簡體   English   中英

在不使用 sklearn 的 mnist 數據的情況下創建混淆矩陣

[英]Creating a confusion matrix without using sklearn for mnist data

需要在不使用 sklearn 的情況下為數據制作混淆矩陣。 訓練神經網絡並對其進行測試。 我想我需要計算每個可能結果(0-9)的結果總和並找到平均值,但我不確定如何從列表中提取每個結果的相關試驗。:這是我的代碼:

# go through all the records in the test data set
for record in test_data_list:
# split the record by the ',' commas
all_values = record.split(',')
# correct answer is first value
correct_label = int(all_values[0])
# scale and shift the inputs
inputs = (numpy.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01
# query the network
outputs = n.query(inputs)

  
# Note that this array, outputs, is the 10 output nodes of the NW for each trial
# This is wehre you need to chnage the code below to build the confusion matrix

label = numpy.argmax(outputs)
# append correct or incorrect to list 
if (label == correct_label):
    # network's answer matches correct answer, add 1 to scorecard
    scorecard.append(1)
else:
    # network's answer doesn't match correct answer, add 0 to scorecard
    scorecard.append(0)
    pass
pass

創建一個 (n_classes, n_classes) 矩陣並為每個conf_mat[gt_label, predicted_label] += 1遞增值

代碼:

import numpy as np


n_classes = 10
ground_truth_labels = np.random.randint(0, n_classes, size=1000)
predicted_labels = np.random.randint(0, n_classes, size=1000)

# Self-made confusion matrix
confusion_matrix = np.zeros((n_classes, n_classes))
for ground_truth_label, predicted_label in zip(ground_truth_labels, predicted_labels):
    confusion_matrix[ground_truth_label, predicted_label] += 1

# check the correctness of our matrix
from sklearn.metrics import confusion_matrix as sklearn_confusion_matrix
conf_matrix_sklearn = sklearn_confusion_matrix(ground_truth_labels, predicted_labels, labels=range(10))

assert np.allclose(conf_matrix_sklearn, confusion_matrix)

暫無
暫無

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

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