sklearn.metrics.precision_recall_fscore_support?
sklearn.metrics.precision_recall_fscore_support(y_true, y_pred, *, beta=1.0, labels=None, pos_label=1, average=None, warn_for=('precision', 'recall', 'f-score'), sample_weight=None, zero_division='warn')
計算每個類的精度,召回率,F量度和支持度
精度是tp /(tp + fp)的比率,其中tp是真正例的數目,fp是假正例的數目。從直覺上講,精度是分類器不將負例樣本標記為正例的能力。
召回率是tp /(tp + fn)的比率,其中tp是真正例的數目,fn是假負例的數目。直觀上,召回是分類器找到所有正例樣本的能力。
F-beta得分可以解釋為精度和召回率的加權調和平均值,其中F-beta得分在1時達到最佳值,在0時達到最差值。
beta參數對于召回率的F-beta評分權重比對于精確度的權重高。 beta == 1.0表示召回率和精確度同等重要。
支持度是y_true中每個類的出現次數。
如果pos_label為None且采用二進制分類,則該函數將返回平均精度、召回率和F度量(如果average是'micro','macro','weighted'或'samples'之一)。
在用戶指南中閱讀更多內容10
參數 | 說明 |
---|---|
y_true | 1d array-like, or label indicator array / sparse matrix 真實目標值。 |
y_pred | 1d array-like, or label indicator array / sparse matrix 分類器返回的估計目標。 |
beta | float, 1.0 by default F-score召回與精度的強度對比。 |
labels | list, optional 當average!='binary'時要包括的一組標簽,如果average是None,則是標簽的順序。可以排除數據中存在的標簽,例如,以忽略多數否定類的方式計算多類平均值,而數據中不存在的標簽將導致宏平均值中的0成分。對于多標簽目標,標簽是列索引。默認情況下,y_true和y_pred中的所有標簽均按排序順序使用。 |
pos_label | str or int, 1 by default 如果average ='binary'且數據為二進制的報告類。如果數據是多類或多標簽的,則將被忽略;設置labels=[pos_label]和average!='binary'將僅報告該標簽的分數。 |
average | string, [None (default)| ‘binary’| ‘micro’| ‘macro’| ‘samples’|‘weighted’] 如果為None,則返回每個類的分數。否則,將根據數據的平均表現確定類型: - 'binary' :僅報告由pos_label指定的類的結果。僅當目標(y_ {true,pred})為二進制時才適用。 - 'micro' :通過計算真正例、假負例和假正例的總數來全局計算指標。 - 'macro' :計算每個標簽的指標,并找到其未加權平均值。 沒有考慮標簽不平衡。 - 'weighted' :計算每個標簽的指標,并找到它們受支持的平均權重(每個標簽的真實實例數)。這會更改‘macro’以解決標簽不平衡的問題;這可能導致F-score不在精確度和召回率之間。 - 'samples' :計算每個實例的指標,并找到它們的平均值(僅對不同于 accuracy_score 的多標簽分類有意義)。 |
warn_for | tuple or set, for internal use 這確定了在使用此函數僅返回其指標之一的情況下將發出哪些警告。 |
sample_weight | array-like of shape (n_samples,), default=None 樣本權重。 |
zero_division | “warn”, 0 or 1, default=”warn” 設置零分頻時的返回值: - 召回:沒有正例標簽時 - 精度:沒有正例預測時 f-score:發生以上兩種情況時 如果設置為“ warn”,則該值為0,但也會發出警告。 |
返回值 | 說明 |
---|---|
precision | float (if average is not None) or array of float, shape = [n_unique_labels] |
recall | float (if average is not None) or array of float, , shape = [n_unique_labels] |
fbeta_score | float (if average is not None) or array of float, shape = [n_unique_labels] |
support | None (if average is not None) or array of int, shape = [n_unique_labels] y_true中每個標簽的出現次數。 |
注
當真正例+假正例== 0時,精度是不確定的;當真正例+假負例== 0時,召回率是不確定的。在這種情況下,默認情況下,度量將設置為0,f-score也將設置為0,并且將引發UndefinedMetricWarning。可以使用zero_division修改此行為。
參考
1 Wikipedia entry for the Precision and recall
2 Wikipedia entry for the F1-score
示例
>>> import numpy as np
>>> from sklearn.metrics import precision_recall_fscore_support
>>> y_true = np.array(['cat', 'dog', 'pig', 'cat', 'dog', 'pig'])
>>> y_pred = np.array(['cat', 'pig', 'dog', 'cat', 'cat', 'dog'])
>>> precision_recall_fscore_support(y_true, y_pred, average='macro')
(0.22..., 0.33..., 0.26..., None)
>>> precision_recall_fscore_support(y_true, y_pred, average='micro')
(0.33..., 0.33..., 0.33..., None)
>>> precision_recall_fscore_support(y_true, y_pred, average='weighted')
(0.22..., 0.33..., 0.26..., None)
可以計算每個標簽的精度,召回率,F1得分和支持度而不是取平均值:
>>> precision_recall_fscore_support(y_true, y_pred, average=None,
... labels=['pig', 'dog', 'cat'])
(array([0. , 0. , 0.66...]),
array([0., 0., 1.]), array([0. , 0. , 0.8]),
array([2, 2, 2]))