sklearn.metrics.multilabel_confusion_matrix?
sklearn.metrics.multilabel_confusion_matrix(y_true, y_pred, *, sample_weight=None, labels=None, samplewise=False)
[源碼]
計算每個類別或樣本的混淆矩陣
0.21版中的新功能。
計算逐類別(default)或逐樣本(samplewise = True)的多標簽混淆矩陣,以評估分類的準確性,并輸出每個類別或樣本的混淆矩陣。
在多標簽混淆矩陣MCM中,真負例計數為,假負例計數為,真正例計數為,假正例計數為。
多類數據將視為被一對多轉換為二值化。返回的混淆矩陣將按照(y_true,y_pred)的并集中唯一標簽的排序順序。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
y_true | 1d array-like, or label indicator array / sparse matrix (n_samples,n_outputs)或(n_samples,)形狀的真實目標值。 |
y_pred | 1d array-like, or label indicator array / sparse matrix (n_samples,n_outputs)或(n_samples,)形狀的分類器返回的估計目標。 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重。 |
labels | array-like 類或列索引的列表,用于選擇某些類(或強制包含數據中不存在的類) |
samplewise | bool, default=False 在多標簽的情況下,計算每個樣品的混淆矩陣 |
返回值 | 說明 |
---|---|
multi_confusion | array, shape (n_outputs, 2, 2) 一個2x2混淆矩陣,對應于輸入中的每個輸出。 在計算逐級multi_confusion(默認值)時,則n_outputs = n_labels; 在計算按樣本的multi_confusion(samplewise = True)時,n_outputs = n_samples。如果定義了labels,則結果將按labels中指定的順序返回,否則默認情況下將按排序順序返回結果。 |
另見:
注
multilabel_confusion_matrix計算逐類或逐樣例多標簽混淆矩陣,在多類任務中,標簽以“一對多”的方式進行二值化; 而confusion_matrix為每兩類之間的混淆計算一個混淆矩陣。
示例
多標簽指示器案例:
>>> import numpy as np
>>> from sklearn.metrics import multilabel_confusion_matrix
>>> y_true = np.array([[1, 0, 1],
... [0, 1, 0]])
>>> y_pred = np.array([[1, 0, 0],
... [0, 1, 1]])
>>> multilabel_confusion_matrix(y_true, y_pred)
array([[[1, 0],
[0, 1]],
<BLANKLINE>
[[1, 0],
[0, 1]],
<BLANKLINE>
[[0, 1],
[1, 0]]])
多類的例子:
>>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
>>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
>>> multilabel_confusion_matrix(y_true, y_pred,
... labels=["ant", "bird", "cat"])
array([[[3, 1],
[0, 2]],
<BLANKLINE>
[[5, 0],
[1, 0]],
<BLANKLINE>
[[2, 1],
[1, 2]]])