https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_score.html
sklearn.metrics.precision_score(y_true, y_pred, labels=None, pos_label=1, average=’binary’, sample_weight=None)
计算精度
精度 P r e c i s i o n = T P ( T P + F P ) Precision = \frac{TP}{(TP+FP)} Precision=(TP+FP)TP。其中TP是真正例,FP是假正例。精度直观地表示分类器标记正例的能力。
最佳值为1,最差值为0
参数名 | 含义 | 类型 |
---|---|---|
y_true | 正确值 | 1维矩阵 |
y_pred | 预测值 | 1维矩阵 |
average | 计算类型 | 字符串,‘binary’(默认)、‘micro’、‘macro’、‘weighted’、‘samples’ |
sample_weight | 样本比重 | n维矩阵(n=样本类数) |
参数average
选项 | 含义 |
---|---|
binary | 二分类 |
micro | 统计全局TP和FP来计算 |
macro | 计算每个标签的未加权均值(不考虑不平衡) |
weighted | 计算每个标签等等加权均值(考虑不平衡) |
samples | 计算每个实例找出其均值 |
precision,float或float矩阵
>>> from sklearn.metrics import precision_score
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> precision_score(y_true, y_pred, average='macro')
0.22...
>>> precision_score(y_true, y_pred, average='micro')
0.33...
>>> precision_score(y_true, y_pred, average='weighted')
...
0.22...
>>> precision_score(y_true, y_pred, average=None)
array([0.66..., 0. , 0. ])