【4.3.1】量化预测结果

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

  • Estimator评分方法:Estimators有一个评分方法,为他们设计要解决的问题提供默认评估标准。 本页未对此进行讨论,但在每个估算工具的文档中都有讨论。
  • 评分参数:使用交叉验证的模型评估工具(例如 model_selection.cross_val_score和model_selection.GridSearchCV)依赖于内部评分策略。 这将在评分参数:定义模型评估规则一节中讨论。
  • 度量功能(Metric functions):度量模块实现了为特定目的评估预测误差的功能。 这些指标详细介绍了 Classification metrics, Multilabel ranking metrics, Regression metrics and Clustering metrics.。

最后,Dummy estimators对于获得随机预测的这些指标的基准值是有用的。

使用工具进行模型选择和评估,例如 model_selection.GridSearchCV和 model_selection.cross_val_score,采用一个scoring参数来控制它们对评估的估计量应用的指标。

一、常见情况:预定义值

对于最常见的用例,您可以使用scoring参数指定一个记分对象 ; 下表显示了所有可能的值。所有得分对象遵循惯例:较高的返回值优于较低的返回值。因此,衡量模型和数据之间距离的度量(metrics.mean_squared_error如)可用作返回度量值的否定值的neg_mean_squared_error

Scoring Function Comment
分类
‘accuracy’ metrics.accuracy_score
‘average_precision’ metrics.average_precision_score
‘f1’ metrics.f1_score for binary targets
‘f1_micro’ metrics.f1_score micro-averaged
‘f1_macro’ metrics.f1_score macro-averaged
‘f1_weighted’ metrics.f1_score weighted average
‘f1_samples’ metrics.f1_score by multilabel sample
‘neg_log_loss’ metrics.log_loss requires predict_proba support
‘precision’ etc. metrics.precision_score suffixes apply as with ‘f1’
‘recall’ etc. metrics.recall_score suffixes apply as with ‘f1’
‘roc_auc’ metrics.roc_auc_score
聚类
‘adjusted_mutual_info_score’ metrics.adjusted_mutual_info_score
‘adjusted_rand_score’ metrics.adjusted_rand_score
‘completeness_score’ metrics.completeness_score
‘fowlkes_mallows_score’ metrics.fowlkes_mallows_score
‘homogeneity_score’ metrics.homogeneity_score
‘mutual_info_score’ metrics.mutual_info_score
‘normalized_mutual_info_score’ metrics.normalized_mutual_info_score
‘v_measure_score’ metrics.v_measure_score
回归
‘explained_variance’ metrics.explained_variance_score
‘neg_mean_absolute_error’ metrics.mean_absolute_error
‘neg_mean_squared_error’ metrics.mean_squared_error
‘neg_mean_squared_log_error’ metrics.mean_squared_log_error
‘neg_median_absolute_error’ metrics.median_absolute_error
‘r2’ metrics.r2_score

用法示例:

>>> from sklearn import svm, datasets
>>> from sklearn.model_selection import cross_val_score
>>> iris = datasets.load_iris()
>>> X, y = iris.data, iris.target
>>> clf = svm.SVC(probability=True, random_state=0)
>>> cross_val_score(clf, X, y, scoring='neg_log_loss')
array([-0.07..., -0.16..., -0.06...])
>>> model = svm.SVC()
>>> cross_val_score(model, X, y, scoring='wrong_choice')
Traceback (most recent call last):
ValueError: 'wrong_choice' is not a valid scoring value. Valid options are ['accuracy', 'adjusted_rand_score', 'average_precision', 'f1', 'f1_macro', 'f1_micro', 'f1_samples', 'f1_weighted', 'neg_log_loss', 'neg_mean_absolute_error', 'neg_mean_squared_error', 'neg_median_absolute_error', 'precision', 'precision_macro', 'precision_micro', 'precision_samples', 'precision_weighted', 'r2', 'recall', 'recall_macro', 'recall_micro', 'recall_samples', 'recall_weighted', 'roc_auc']

二、从公制函数定义你的评分策略

该模块sklearn.metric还暴露了一组测量预测误差的简单函数,给出了地面真实和预测:

  • 函数以_score返回值最大化结束,越高越好。
  • 功能结束_error或_loss返回值最小化,越低越好。当使用转换为记分对象时make_scorer,将greater_is_better参数设置为False(默认为True);请参阅下面的参数说明。

可用于各种机器学习任务的指标在下面详细介绍。

许多指标没有被赋予用作scoring值的名称,有时是因为它们需要额外的参数,例如 fbeta_score。在这种情况下,您需要生成一个适当的评分对象。生成可评估对象进行评分的最简单的方法是使用make_scorer。该函数将度量转换为可用于模型评估的可调用。

一个典型的用例是将库中的现有度量函数包含在其参数的非默认值中,例如函数的beta参数fbeta_score:

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

第二个用例是使用简单的python函数来构建一个完全自定义的scorer对象make_scorer,它可以使用几个参数:

  • 你想要使用的python函数(my_custom_loss_func 在下面的例子中)
  • python函数是否返回一个分数(greater_is_better=True,默认值)还是一个loss(greater_is_better=False)。如果一个损失,得分器对象的python函数的输出被否定,符合交叉验证约定,得分者为更好的模型返回更高的值。
  • 仅用于分类度量:您提供的python函数是否需要连续的判断确定性(needs_threshold=True)。默认值为False。
  • 任何额外的参数,例如beta或labels在f1_score。

以下是建立自定义记分员以及使用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_func will negate the return value of my_custom_loss_func,
>>> #  which will be np.log(2), 0.693, given the values for ground_truth
>>> #  and predictions defined below.
>>> 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...

三、实现自己的评分对象

您可以通过从头开始构建自己的评分对象,而不使用make_scorer工厂,从而生成更灵活的模型得分手。要求成为记分员,需要符合以下两个规则所指定的协议:

  • 它可以用参数调用 ,应该评估的模型在哪里,是验证数据,是(在监督的情况下)或(在无监督的情况下)的基本真实目标。(estimator, X, y)estimatorXyXNone
  • 它返回量化浮点数 estimator上预测质量X,参考y。再次,按照惯例,更高的数字更好,所以如果你的得分手返回损失,该值应该被否定。

参考资料

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn