在此處輸入圖片說明
你好,有誰知道如何加快這個程序。僅使用 numpy 或 pandas 從 3x3 混淆矩陣中提取常見度量值(例如準確度、精確度、召回率、f 分數)的簡單方法?有什么建議?我給你附上一張我的結果的圖片。
cm =confusion_matrix.to_numpy()
acuracy_0 = np.round_(cm[0][0]/cm[3][3], 2)
acuracy_1 = np.round_(cm[1][1]/cm[3][3], 2)
acuracy_2 = np.round_(cm[2][2]/cm[3][3], 2)
acuracy = (acuracy_0, acuracy_1, acuracy_2)
col1 = np.array(acuracy)
precision_0 = np.round_(cm[0][0]/cm[3][0], 2)
precision_1 = np.round_(cm[1][1]/cm[3][1], 2)
precision_2 = np.round_(cm[2][2]/cm[3][2], 2)
precision = (precision_0, precision_1, precision_2)
col2 = np.array(precision)
recall_0 = np.round_(cm[0][0]/cm[0][3], 2)
recall_1 = np.round_(cm[1][1]/cm[1][3], 2)
recall_2 = np.round_(cm[2][2]/cm[2][3], 2)
recall = (recall_0, recall_1, recall_2)
col3 = np.array(recall)
f_score_0 = np.round_((2*precision_0*recall_0)/(precision_0 recall_0), 2)
f_score_1 = np.round_((2*precision_1*recall_1)/(precision_1 recall_1), 2)
f_score_2 = np.round_((2*precision_2*recall_2)/(precision_2 recall_2), 2)
f_score = (f_score_0, f_score_1, f_score_2)
col4 = np.array(f_score)
d = {'Acuracy': col1, 'Precision':col2, 'Recall':col3, 'F1_score':col4}
measurs = pd.DataFrame(data=d)
measurs.index.name = 'Class_Label'
measurs
uj5u.com熱心網友回復:
由于您正在使用 Pandas 資料框,您可以執行以下操作:
# df is your confusion_matrix
# these are your classes
classes = df.columns[:-1]
# the true positives
true_positives = np.diagonal(df.loc[classes, classes])
accuracy = true_positives / df.loc['All','All']
precisions = true_positives / df.loc['All', classes]
recalls = true_positives / df.loc[classes, 'All']
fscores = 2 * precisions * recalls / (precisions recalls)
out = pd.DataFrame({'accuracy': accuracy,
'precisions': precisions,
'recall': recalls,
'fscore': fscores
}, index=classes # in case your classes are different from 0,1,2,...
).round(2) # round all at once
輸出:
accuracy precisions recall fscore
0 0.17 0.38 0.67 0.48
1 0.14 0.38 0.31 0.34
2 0.08 0.43 0.27 0.33
uj5u.com熱心網友回復:
對于它的價值,代碼如下
accuracy_0 = cm[0][0]/cm[3][3]
accuracy_1 = cm[1][1]/cm[3][3]
accuracy_2 = cm[2][2]/cm[3][3]
accuracy = (accuracy_0, accuracy_1, accuracy_2)
可以換成更簡潔的
accuracy = cm.diagonal()[:-1]/cm[-1,-1]
所以你可能會重寫你的代碼
cm = confusion_matrix.to_numpy()
diag = cm.diagonal()[:-1]
accuracy = diag / cm[ -1, -1]
precision = diag / cm[ 3,:-1]
recall = diag / cm[:-1, 3]
f_score = 2 * precision * recall / (precision recall)
out = pd.DataFrame({'Accuracy': accuracy,
'Precision': precision,
'Recall': recall,
'F-score': f_score}).round(2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/355848.html
下一篇:獲得更干凈的斑點以進行計數
