sklearn.metrics.classification_report?
sklearn.metrics.classification_report(y_true, y_pred, *, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False, zero_division='warn')
建立一個顯示主要分類指標的文本報告。
在用戶指南中閱讀更多內容。
參數 | 說明 |
---|---|
y_true | 1d array-like, or label indicator array / sparse matrix 真實的目標值。 |
y_pred | 1d array-like, or label indicator array / sparse matrix 分類器返回的估計目標。 |
labels | array, shape = [n_labels] 報告中要包含的標簽索引的可選列表。 |
target_names | list of strings 與標簽匹配的可選顯示名稱(相同順序)。 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重。 |
digits | int 用于格式化輸出浮點值的位數。當output_dict為True時,它將被忽略,并且返回的值將不會四舍五入。 |
output_dict | bool (default = False) 如果為True,則將輸出作為dict返回。 0.20版中的新功能。 |
zero_division | “warn”, 0 or 1, default=”warn” 設置零分頻時返回的值。 如果設置為“ warn”,則該值為0,但也會發出警告。 |
返回值 | 說明 |
---|---|
report | string / dict 準確性、召回率的文字摘要,每類的F1分數。如果output_dict為True,則返回字典。字典具有以下結構: |
{'label 1': {'precision':0.5,
'recall':1.0,
'f1-score':0.67,
'support':1},
'label 2': { ... },
...
}
報告的平均值包括宏觀平均值(每個標簽的未加權平均值)、加權平均值(每個標簽的支持度權重平均值)和樣本平均值(僅適用于多標簽分類)。微觀平均(對總的真正例、假負例和假正例的平均值)僅針對多標簽或帶有類別子集的多重類別顯示,否則與準確性相對應。有關平均值的更多詳細信息,另請參見precision_recall_fscore_support
。
請注意,在二元分類中,對正例類的召回也稱為“敏感性”。負例類的召回是“特殊性”。
另見
示例
>>> from sklearn.metrics import classification_report
>>> y_true = [0, 1, 2, 2, 2]
>>> y_pred = [0, 0, 2, 2, 1]
>>> target_names = ['class 0', 'class 1', 'class 2']
>>> print(classification_report(y_true, y_pred, target_names=target_names))
precision recall f1-score support
<BLANKLINE>
class 0 0.50 1.00 0.67 1
class 1 0.00 0.00 0.00 1
class 2 1.00 0.67 0.80 3
<BLANKLINE>
accuracy 0.60 5
macro avg 0.50 0.56 0.49 5
weighted avg 0.70 0.60 0.61 5
<BLANKLINE>
>>> y_pred = [1, 1, 0]
>>> y_true = [1, 1, 1]
>>> print(classification_report(y_true, y_pred, labels=[1, 2, 3]))
precision recall f1-score support
<BLANKLINE>
1 1.00 0.67 0.80 3
2 0.00 0.00 0.00 0
3 0.00 0.00 0.00 0
<BLANKLINE>
micro avg 1.00 0.67 0.80 3
macro avg 0.33 0.22 0.27 3
weighted avg 1.00 0.67 0.80 3
<BLANKLINE>