sklearn - 对类的子集进行精确评分的交叉验证

时间:2013-12-25 12:03:55

标签: python machine-learning cross-validation scikit-learn

我有一个用于分类的数据集,包含3个类标签[0,1,2]

我想运行交叉验证并尝试几个估算器,但我对只有1级和2级精度的评分感兴趣。我不关心0级的精确度,我也不知道希望它的得分能够摒弃CV优化。我也不关心任何课程的召回。换句话说,我想确保每当预测1或2时,它就会非常自信。

所以问题是,如何运行cross_val_score并告诉其评分函数忽略0级的精度?

更新:根据接受的答案,这是一个示例答案代码:

def custom_precision_score(y_true,y_pred):
  precision_tuple, recall_tuple, fscore_tuple, support_tuple = metrics.precision_recall_fscore_support(y_true, y_pred)
  precision_tuple = precision_tuple[1:]
  support_tuple = support_tuple[1:]
  weighted_precision = np.average(precision_tuple, weights=support_tuple)
  return weighted_precision

custom_scorer = metrics.make_scorer(custom_precision_score)

scores = cross_validation.cross_val_score(clf, featuresArray, targetArray, cv=10, scoring=custom_scorer)

1 个答案:

答案 0 :(得分:4)

cross_val_score包含使用make_scorer的{​​{3}} scorer callable object。您可以在score_func(y, y_pred, **kwargs)调用的自定义分数函数make_scorer中设置要测试的组。