1.介绍

有三种不同的方法来评估一个模型的预测质量:

  • estimator的score方法:sklearn中的estimator都具有一个score方法,它提供了一个缺省的评估法则来解决问题。
  • Scoring参数:使用cross-validation的模型评估工具,依赖于内部的scoring策略。见下。
  • Metric函数:metrics模块实现了一些函数,用来评估预测误差。见下。

2. scoring参数

模型选择和评估工具,例如: grid_search.GridSearchCV 和 cross_validation.cross_val_score,使用scoring参数来控制你的estimator的好坏。

2.1 预定义的值

对于大多数case而说,你可以设计一个使用scoring参数的scorer对象;下面展示了所有可能的值。所有的scorer对象都遵循:高得分,更好效果。如果从mean_absolute_error 和mean_squared_error(它计算了模型与数据间的距离)返回的得分将被忽略。

python gmm模型评价bic和aic怎么判断 python评估模型_正例

2.2 从metric函数定义你的scoring策略

sklearn.metric提供了一些函数,用来计算真实值与预测值之间的预测误差:

  • 以_score结尾的函数,返回一个最大值,越高越好
  • 以_error结尾的函数,返回一个最小值,越小越好;如果使用make_scorer来创建scorer时,将greater_is_better设为False

接下去会讨论多种机器学习当中的metrics。

许多metrics并没有给出在scoring参数中可配置的字符名,因为有时你可能需要额外的参数,比如:fbeta_score。这种情况下,你需要生成一个合适的scorer对象。最简单的方法是调用make_scorer来生成scoring对象。该函数将metrics转换成在模型评估中可调用的对象。

第一个典型的用例是,将一个库中已经存在的metrics函数进行包装,使用定制参数,比如对fbeta_score函数中的beta参数进行设置:


>>> from sklearn.metrics import fbeta_score, make_scorer
>>> ftwo_scorer = make_scorer(fbeta_score, beta=2)
>>> from sklearn.grid_search import GridSearchCV
>>> from sklearn.svm import LinearSVC
>>> grid = GridSearchCV(LinearSVC(), param_grid={'C': [1, 10]}, scoring=ftwo_scorer)

第二个典型用例是,通过make_scorer构建一个完整的定制scorer函数,该函数可以带有多个参数:

  • 你可以使用python函数:下例中的my_custom_loss_func
  • python函数是否返回一个score(greater_is_better=True),还是返回一个loss(greater_is_better=False)。如果为loss,python函数的输出将被scorer对象忽略,根据交叉验证的原则,得分越高模型越好。
  • 对于分类问题的metrics:如果你提供的python函数是否需要对连续值进行决策判断,可以将参数设置为(needs_threshold=True)。缺省值为False。
  • 一些额外的参数:比如f1_score中的bata或labels。

下例使用定制的scorer,使用了greater_is_better参数:


>>> import numpy as np
>>> def my_custom_loss_func(ground_truth, predictions):
...     diff = np.abs(ground_truth - predictions).max()
...     return np.log(1 + diff)
...
>>> loss = make_scorer(my_custom_loss_func, greater_is_better=False)
>>> score = make_scorer(my_custom_loss_func, greater_is_better=True)
>>> ground_truth = [[1, 1]]
>>> predictions = [0, 1]
>>> from sklearn.dummy import DummyClassifier
>>> clf = DummyClassifier(strategy=‘most_frequent’, random_state=0)
>>> clf = clf.fit(ground_truth, predictions)
>>> loss(clf,ground_truth, predictions)
-0.69…
>>> score(clf,ground_truth, predictions)
0.69…

2.3 实现你自己的scoring对象

你可以生成更灵活的模型scorer,通过从头构建自己的scoring对象来完成,不需要使用make_scorer工厂函数。对于一个自己实现的scorer来说,它需要遵循两个原则:

  • 必须可以用(estimator, X, y)进行调用
  • 必须返回一个float的值

3. 分类metrics

sklearn.metrics模块实现了一些loss, score以及一些工具函数来计算分类性能。一些metrics可能需要正例、置信度、或二分决策值的的概率估计。大多数实现允许每个sample提供一个对整体score来说带权重的分布,通过sample_weight参数完成。

一些二分类(binary classification)使用的case:

  • matthews_corrcoef(y_true, y_pred)
  • precision_recall_curve(y_true, probas_pred)
  • roc_curve(y_true, y_score[, pos_label, …])

一些多分类(multiclass)使用的case:

  • confusion_matrix(y_true, y_pred[, labels])
  • hinge_loss(y_true, pred_decision[, labels, …])

一些多标签(multilabel)的case:

  • accuracy_score(y_true, y_pred[, normalize, …])
  • classification_report(y_true, y_pred[, …])
  • f1_score(y_true, y_pred[, labels, …])
  • fbeta_score(y_true, y_pred, beta[, labels, …])
  • hamming_loss(y_true, y_pred[, classes])
  • jaccard_similarity_score(y_true, y_pred[, …])
  • log_loss(y_true, y_pred[, eps, normalize, …])
  • precision_recall_fscore_support(y_true, y_pred)
  • precision_score(y_true, y_pred[, labels, …])
  • recall_score(y_true, y_pred[, labels, …])
  • zero_one_loss(y_true, y_pred[, normalize, …])

还有一些可以同时用于二标签和多标签(不是多分类)问题:

  • average_precision_score(y_true, y_score[, …])
  • roc_auc_score(y_true, y_score[, average, …])

在以下的部分,我们将讨论各个函数。

3.1 二分类/多分类/多标签

对于二分类来说,必须定义一些matrics(f1_score,roc_auc_score)。在这些case中,缺省只评估正例的label,缺省的正例label被标为1(可以通过配置pos_label参数来完成)

将一个二分类matrics拓展到多分类或多标签问题时,我们可以将数据看成多个二分类问题的集合,每个类都是一个二分类。接着,我们可以通过跨多个分类计算每个二分类metrics得分的均值,这在一些情况下很有用。你可以使用average参数来指定。

  • macro:计算二分类metrics的均值,为每个类给出相同权重的分值。当小类很重要时会出问题,因为该macro-averging方法是对性能的平均。另一方面,该方法假设所有分类都是一样重要的,因此macro-averaging方法会对小类的性能影响很大。
  • weighted: 对于不均衡数量的类来说,计算二分类metrics的平均,通过在每个类的score上进行加权实现。
  • micro: 给出了每个样本类以及它对整个metrics的贡献的pair(sample-weight),而非对整个类的metrics求和,它会每个类的metrics上的权重及因子进行求和,来计算整个份额。Micro-averaging方法在多标签(multilabel)问题中设置,包含多分类,此时,大类将被忽略。
  • samples:应用在 multilabel问题上。它不会计算每个类,相反,它会在评估数据中,通过计算真实类和预测类的差异的metrics,来求平均(sample_weight-weighted)
  • average:average=None将返回一个数组,它包含了每个类的得分.

多分类(multiclass)数据提供了metric,和二分类类似,是一个label的数组,而多标签(multilabel)数据则返回一个索引矩阵,当样本i具有label j时,元素[i,j]的值为1,否则为0.

3.2 accuracy_score

accuracy_score函数计算了准确率,不管是正确预测的fraction(default),还是count(normalize=False)。

在multilabel分类中,该函数会返回子集的准确率。如果对于一个样本来说,必须严格匹配真实数据集中的label,整个集合的预测标签返回1.0;否则返回0.0.

预测值与真实值的准确率,在n个样本下的计算公式如下:



accuracy(y,y^)=1nsamples∑i=0nsamples−1l(y^i=yi)accuracy(y,y^)=1nsamples∑i=0nsamples−1l(y^i=yi)


1(x)为指示函数。


>>> import numpy as np
>>> from sklearn.metrics import accuracy_score
>>> y_pred = [0, 2, 1, 3]
>>> y_true = [0, 1, 2, 3]
>>> accuracy_score(y_true, y_pred)
0.5
>>> accuracy_score(y_true, y_pred, normalize=False)
2

在多标签的case下,二分类label:


>>> accuracy_score(np.array([[0, 1], [1, 1]]), np.ones((2, 2)))
0.5

3.3 Cohen’s kappa

函数cohen_kappa_score计算了Cohen’s kappa估计。这意味着需要比较通过不同的人工标注(numan annotators)的标签,而非分类器中正确的类。

kappa score是一个介于(-1, 1)之间的数. score>0.8意味着好的分类;0或更低意味着不好(实际是随机标签)

Kappa score可以用在二分类或多分类问题上,但不适用于多标签问题,以及超过两种标注的问题。

3.4 混淆矩阵

confusion_matrix函数通过计算混淆矩阵,用来计算分类准确率。

缺省的,在混淆矩阵中的i,j指的是观察的数目i,预测为j,示例:


>>> 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]])

结果为:

示例:

3.5 分类报告

classification_report函数构建了一个文本报告,用于展示主要的分类metrics。 下例给出了一个小示例,它使用定制的target_names和对应的label:


>>> from sklearn.metrics import classification_report
>>> y_true = [0, 1, 2, 2, 0]
>>> y_pred = [0, 0, 2, 2, 0]
>>> target_names = ['class 0', 'class 1', 'class 2']
>>> print(classification_report(y_true, y_pred, target_names=target_names))
             precision    recall  f1-score   support

<span >class</span> <span class="err">0       0.67      1.00      0.80         2
</span><span >class</span> <span >1</span>       <span >0.00</span>      <span >0.00</span>      <span >0.00</span>         <span >1</span>
<span >class</span> <span class="err">2       1.00      1.00      1.00         2

avg / total 0.67 0.80 0.72 5

3.6 Hamming loss

hamming_loss计算了在两个样本集里的平均汉明距离或平均Hamming loss。

  • y^jy^j是类目数

那么两个样本间的Hamming loss为LHammingLHamming,定义如下:



LHamming(y,y^)=1nlabels∑j=0nlabels−11(y^j≠yj)LHamming(y,y^)=1nlabels∑j=0nlabels−11(y^j≠yj)


其中:1(x)1(x)为指示函数。


>>> from sklearn.metrics import hamming_loss
>>> y_pred = [1, 2, 3, 4]
>>> y_true = [2, 2, 3, 4]
>>> hamming_loss(y_true, y_pred)
0.25

在多标签(multilabel)的使用二元label指示器的情况:


>>> hamming_loss(np.array([[0, 1], [1, 1]]), np.zeros((2, 2)))
0.75

注意:在多分类问题上,Hamming loss与y_true 和 y_pred 间的Hamming距离相关,它与0-1 loss相类似。然而,0-1 loss会对不严格与真实数据集相匹配的预测集进行惩罚。因而,Hamming loss,作为0-1 loss的上界,也在0和1之间;预测一个合适的真实label的子集或超集将会给出一个介于0和1之间的Hamming loss.

3.7 Jaccard相似度系数score

jaccard_similarity_score函数会计算两对label集之间的Jaccard相似度系数的平均(缺省)或求和。它也被称为Jaccard index.

第i个样本的Jaccard相似度系数(Jaccard similarity coefficient),真实标签集为yiyi,其定义如下:



J(yi,y^i)=|yi∩y^i||yi∪y^i|.J(yi,y^i)=|yi∩y^i||yi∪y^i|.


在二分类和多分类问题上,Jaccard相似度系数score与分类的正确率(accuracy)相同:


>>> import numpy as np
>>> from sklearn.metrics import jaccard_similarity_score
>>> y_pred = [0, 2, 1, 3]
>>> y_true = [0, 1, 2, 3]
>>> jaccard_similarity_score(y_true, y_pred)
0.5
>>> jaccard_similarity_score(y_true, y_pred, normalize=False)
2

在多标签(multilabel)问题上,使用二元标签指示器:


>>> jaccard_similarity_score(np.array([[0, 1], [1, 1]]), np.ones((2, 2)))
0.75

3.8 准确率,召回率与F值

准确率(precision)可以衡量一个样本为负的标签被判成正,召回率(recall)用于衡量所有正例。

F-meature(包括:FβFβ相等,precision和recall的权重相等。

precision_recall_curve会根据预测值和真实值来计算一条precision-recall典线。

average_precision_score则会预测值的平均准确率(AP: average precision)。该分值对应于precision-recall曲线下的面积。

sklearn提供了一些函数来分析precision, recall and F-measures值:

  • average_precision_score:计算预测值的AP
  • f1_score: 计算F1值,也被称为平衡F-score或F-meature
  • fbeta_score: 计算F-beta score
  • precision_recall_curve:计算不同概率阀值的precision-recall对
  • precision_recall_fscore_support:为每个类计算precision, recall, F-measure 和 support
  • precision_score: 计算precision
  • recall_score: 计算recall

注意:precision_recall_curve只用于二分类中。而average_precision_score可用于二分类或multilabel指示器格式

3.8.1 二分类

在二元分类中,术语“positive”和“negative”指的是分类器的预测类别(expectation),术语“true”和“false”则指的是预测是否正确(有时也称为:观察observation)。给出如下的定义:

 

实际类目(observation)

 

预测类目(expectation)

TP(true positive)结果:Correct

FP(false postive)结果:Unexpected

 

FN(false negative)结果: Missing

TN(true negtive)结果:Correct

在这个上下文中,我们定义了precision, recall和F-measure:



precision=tptp+fpprecision=tptp+fp




recall=tptp+fnrecall=tptp+fn




Fβ=(1+β2)precision×recallβ2precision+recallFβ=(1+β2)precision×recallβ2precision+recall


这里是一个二元分类的示例:


>>> from sklearn import metrics
>>> y_pred = [0, 1, 0, 0]
>>> y_true = [0, 1, 0, 1]
>>> metrics.precision_score(y_true, y_pred)
1.0
>>> metrics.recall_score(y_true, y_pred)
0.5
>>> metrics.f1_score(y_true, y_pred)  
0.66...
>>> metrics.fbeta_score(y_true, y_pred, beta=0.5)  
0.83...
>>> metrics.fbeta_score(y_true, y_pred, beta=1)  
0.66...
>>> metrics.fbeta_score(y_true, y_pred, beta=2) 
0.55...
>>> metrics.precision_recall_fscore_support(y_true, y_pred, beta=0.5)  
(array([ 0.66...,  1.        ]), array([ 1. ,  0.5]), array([ 0.71...,  0.83...]), array([2, 2]...))
>>> import numpy as np
>>> from sklearn.metrics import precision_recall_curve
>>> from sklearn.metrics import average_precision_score
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> precision, recall, threshold = precision_recall_curve(y_true, y_scores)
>>> precision
array([ 0.66…, 0.5 , 1. , 1. ])
>>> recall
array([ 1. , 0.5, 0.5, 0. ])
>>> threshold
array([ 0.35, 0.4 , 0.8 ])
>>> average_precision_score(y_true, y_scores)
0.79…

3.8.2 多元分类和多标签分类

在多分类(Multiclass)和多标签(multilabel)分类问题上,precision, recall, 和 F-measure的概念可以独立应用到每个label上。有一些方法可以综合各标签上的结果,通过指定average_precision_score (只能用在multilabel上), f1_score, fbeta_score, precision_recall_fscore_support, precision_score 和 recall_score这些函数上的参数average可以做到。

注意:

  • “micro”选项:表示在多分类中的对所有label进行micro-averaging产生一个平均precision,recall和F值
  • “weighted”选项:表示会产生一个weighted-averaging的F值。

可以考虑下面的概念:

  • y是(sample, label)pairs的预测集
  • y^y^

metrics的定义如下:

python gmm模型评价bic和aic怎么判断 python评估模型_多分类_02

代码:


>>> from sklearn import metrics
>>> y_true = [0, 1, 2, 0, 1, 2]
>>> y_pred = [0, 2, 1, 0, 0, 1]
>>> metrics.precision_score(y_true, y_pred, average='macro')  
0.22...
>>> metrics.recall_score(y_true, y_pred, average='micro')
... 
0.33...
>>> metrics.f1_score(y_true, y_pred, average='weighted')  
0.26...
>>> metrics.fbeta_score(y_true, y_pred, average='macro', beta=0.5)  
0.23...
>>> metrics.precision_recall_fscore_support(y_true, y_pred, beta=0.5, average=None)
... 
(array([ 0.66...,  0.        ,  0.        ]), array([ 1.,  0.,  0.]), array([ 0.71...,  0.        ,  0.        ]), array([2, 2, 2]...))

对于多分类问题,对于一个“negative class”,有可能会排除一些标签:


>>> metrics.recall_score(y_true, y_pred, labels=[1, 2], average='micro')
... # excluding 0, no labels were correctly recalled
0.0

类似的,在数据集样本中没有出现的label不能用在macro-averaging中。


>>> metrics.precision_score(y_true, y_pred, labels=[0, 1, 2, 3], average='macro')
... 
0.166...

3.9 Hinge loss

hinge_loss函数会使用hinge loss计算模型与数据之间的平均距离。它是一个单边的metric,只在预测错误(prediction erros)时考虑。(Hinge loss被用于最大间隔分类器上:比如SVM)

如果label使用+1和-1进行编码。y为真实值,w为由decision_function结出的预测决策。 hinge loss的定义如下:



LHinge(y,w)=max{1−wy,0}=|1−wy|+LHinge(y,w)=max{1−wy,0}=|1−wy|+


如果超过两个label,由于Crammer & Singer所提到的问题 ,hinge_loss 会使用一个多元分类的变种。

如果ywyw则是对于其他label的预测判断的最大值,而predicted decisions由多个predicted decision输出,那么多分类的hinge loss定义如下:

L_\text{Hinge}(y_w, y_t) = \max\left{1 + y_t - y_w, 0\right}L_\text{Hinge}(y_w, y_t) = \max\left{1 + y_t - y_w, 0\right}

二分类问题示例:


>>> from sklearn import svm
>>> from sklearn.metrics import hinge_loss
>>> X = [[0], [1]]
>>> y = [-1, 1]
>>> est = svm.LinearSVC(random_state=0)
>>> est.fit(X, y)
LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
     intercept_scaling=1, loss='squared_hinge', max_iter=1000,
     multi_class='ovr', penalty='l2', random_state=0, tol=0.0001,
     verbose=0)
>>> pred_decision = est.decision_function([[-2], [3], [0.5]])
>>> pred_decision  
array([-2.18...,  2.36...,  0.09...])
>>> hinge_loss([-1, 1, 1], pred_decision)  
0.3...

多分类问题示例:


>>> X = np.array([[0], [1], [2], [3]])
>>> Y = np.array([0, 1, 2, 3])
>>> labels = np.array([0, 1, 2, 3])
>>> est = svm.LinearSVC()
>>> est.fit(X, Y)
LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
     intercept_scaling=1, loss='squared_hinge', max_iter=1000,
     multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
     verbose=0)
>>> pred_decision = est.decision_function([[-1], [2], [3]])
>>> y_true = [0, 2, 3]
>>> hinge_loss(y_true, pred_decision, labels)  
0.56...

3.10 Log loss

Log loss也被称为logistic回归loss,或者交叉熵loss(cross-entropy loss),用于概率估计。它通常用在(multinomial)的LR和神经网络上,以最大期望(EM:expectation-maximization)的变种的方式,用于评估一个分类器的概率输出,而非进行离散预测。

对于二元分类,true label为:y∈0,1y∈0,1,每个样本的log loss是对分类器给定true label的负值log似然估计(negative log-likelihood):

Llog(y,p)=−logPr(y|p)=−(ylog(p)+(1−y)log(1−p))Llog(y,p)=−log⁡Pr⁡(y|p)=−(ylog⁡(p)+(1−y)log⁡(1−p))

当扩展到多元分类(multiclass)上时。可以将样本的true label编码成1-of-K个二元指示器矩阵Y,如果从label K集合中取出的样本i,对应的label为k,则yi,k=1yi,k=1。整个集合的log loss表示如下:



Llog(Y,P)=−logPr(Y|P)=−1N∑i=0N−1∑k=0K−1yi,klogpi,kLlog(Y,P)=−log⁡Pr⁡(Y|P)=−1N∑i=0N−1∑k=0K−1yi,klog⁡pi,k


我们再看下如何对二分类的log loss进行泛化的,注意,在二分类问题上,pi,0=1−pi,1pi,0=1−pi,1扩展内部和来给出二分类的log loss。

log_loss函数,通过给定一列真实值label和一个概率矩阵来计算log loss,返回值通过estimator的predict_proba返回。


>>> from sklearn.metrics import log_loss
>>> y_true = [0, 0, 1, 1]
>>> y_pred = [[.9, .1], [.8, .2], [.3, .7], [.01, .99]]
>>> log_loss(y_true, y_pred)    
0.1738...

y_pred中的[.9, .1]指的是,第一个样本中90%的概率是label 0。另外,log loss是非负的。

3.11 Matthews相关系数


wikipedia是这么说的:

“The Matthews correlation coefficient is used in machine learning as a measure of the quality of binary (two-class) classifications. It takes into account true and false positives and negatives and is generally regarded as a balanced measure which can be used even if the classes are of very different sizes. The MCC is in essence a correlation coefficient value between -1 and +1. A coefficient of +1 represents a perfect prediction, 0 an average random prediction and -1 an inverse prediction. The statistic is also known as the phi coefficient.”

翻译如下:

机器学习中使用的Matthews相关系数,用于度量二分类的质量。它会考虑TP/FP/TN/FP的情况,通常被认为是一个balanced的度量 ,可以用于那些有着不同size的分类中。MCC本质上是一个介于[-1,+1]之间的相关系数值。相关系数为+1,表示是一个完美的预测,0表示是一个平均随机预测(average random prediction),而-1表示是一个逆预测(inverse prediction)。这种统计方法也被称为:phi coefficient。

MCC相应的定义如下:



MCC=tp×tn−fp×fn(tp+fp)(tp+fn)(tn+fp)(tn+fn)−−−−−−−−−−−−−−−−−−−−−−−−−−−−−√.MCC=tp×tn−fp×fn(tp+fp)(tp+fn)(tn+fp)(tn+fn).


这里的示例展示了matthews_corrcoef 函数的使用:


>>> from sklearn.metrics import matthews_corrcoef
>>> y_true = [+1, +1, +1, -1]
>>> y_pred = [+1, -1, +1, +1]
>>> matthews_corrcoef(y_true, y_pred)  
-0.33...

3.12 ROC

roc_curve计算了ROC曲线。Wikipedia如下:

“A receiver operating characteristic (ROC), or simply ROC curve, is a graphical plot which illustrates the performance of a binary classifier system as its discrimination threshold is varied. It is created by plotting the fraction of true positives out of the positives (TPR = true positive rate) vs. the fraction of false positives out of the negatives (FPR = false positive rate), at various threshold settings. TPR is also known as sensitivity, and FPR is one minus the specificity or true negative rate.”

该函数需要二分类的真实值和预测值,它可以是正例的概率估计,置信值,或二分决策值。下例展示了如何使用:


>>> import numpy as np
>>> from sklearn.metrics import roc_curve
>>> y = np.array([1, 1, 2, 2])
>>> scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> fpr, tpr, thresholds = roc_curve(y, scores, pos_label=2)
>>> fpr
array([ 0. ,  0.5,  0.5,  1. ])
>>> tpr
array([ 0.5,  0.5,  1. ,  1. ])
>>> thresholds
array([ 0.8 ,  0.4 ,  0.35,  0.1 ])

下图展下了上面的结果:

roc_auc_score函数计算了ROC曲线下面的面积,它也被称为AUC或AUROC。通过计算下面的面积,曲线信息被归一化到1内。


>>> import numpy as np
>>> from sklearn.metrics import roc_auc_score
>>> y_true = np.array([0, 0, 1, 1])
>>> y_scores = np.array([0.1, 0.4, 0.35, 0.8])
>>> roc_auc_score(y_true, y_scores)
0.75

在多标签(multi-label)分类上,roc_auc_score通过对上面的label进行平均。

对比于其它metrics: accuracy、 Hamming loss、 F1-score, ROC不需要为每个label优化一个阀值。roc_auc_score函数也可以用于多分类(multi-class)问题上。如果预测的输出已经被二值化。

python gmm模型评价bic和aic怎么判断 python评估模型_多分类_03