目的:評估分類器準確性
函數:sklearn.metrics.confusion_matrix(y_true, y_pred, labels=None, sample_weight=None)
輸入:
- y_true:實際的目標結果
- y_pred:預測的結果
- labels: 標簽,對結果中的string進行排序, 順序對應0、1、2
- sample_weight:樣本的權重?
輸出:
- 一個矩陣,shape=[y中的類型數,y中的類型數]
- 矩陣中每個值表征分類的準確性
- 第0行第0列的數表示y_true中值為0,y_pred中值也為0的個數
- 第0行第1列的數表示y_true中值為0,y_pred中值為1的個數
示例:
>>> from sklearn.metrics import confusion_matrix
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> confusion_matrix(y_true, y_pred)
array([[2, 0, 0], [0, 0, 1], [1, 0, 2]])
>>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
>>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
>>> confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])
array([[2, 0, 0], [0, 0, 1], [1, 0, 2]])