【问题标题】:scikit learn decision tree model evaluationscikit学习决策树模型评估
【发布时间】:2016-12-29 20:59:30
【问题描述】:

这里是相关代码和文档,想知道默认cross_val_score没有明确指定score,输出数组是指精度、AUC还是其他一些指标?

使用带有 miniconda 解释器的 Python 2.7。

http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html

>>> from sklearn.datasets import load_iris
>>> from sklearn.cross_validation import cross_val_score
>>> from sklearn.tree import DecisionTreeClassifier
>>> clf = DecisionTreeClassifier(random_state=0)
>>> iris = load_iris()
>>> cross_val_score(clf, iris.data, iris.target, cv=10)
...                             
...
array([ 1.     ,  0.93...,  0.86...,  0.93...,  0.93...,
        0.93...,  0.93...,  1.     ,  0.93...,  1.      ])

问候, 林

【问题讨论】:

    标签: python python-2.7 machine-learning scikit-learn decision-tree


    【解决方案1】:

    如果没有给出评分参数,cross_val_score 将默认使用您正在使用的估算器的 .score 方法。对于DecisionTreeClassifier,它是平均准确度(如下面的文档字符串所示):

    In [11]: DecisionTreeClassifier.score?
    Signature: DecisionTreeClassifier.score(self, X, y, sample_weight=None)
    Docstring:
    Returns the mean accuracy on the given test data and labels.
    
    In multi-label classification, this is the subset accuracy
    which is a harsh metric since you require for each sample that
    each label set be correctly predicted.
    
    Parameters
    ----------
    X : array-like, shape = (n_samples, n_features)
        Test samples.
    
    y : array-like, shape = (n_samples) or (n_samples, n_outputs)
        True labels for X.
    
    sample_weight : array-like, shape = [n_samples], optional
        Sample weights.
    
    Returns
    -------
    score : float
        Mean accuracy of self.predict(X) wrt. y.
    

    【讨论】:

    • 感谢 Randy,如果是二分类问题,每个预测要么正确要么错误。对准确性意味着什么感到困惑?
    • 如果它是一个二分类问题,它只是正确分类的百分比,假设你预测 0/1。如果您要预测概率,它将是每个预测的(1-预测误差)的平均值。
    【解决方案2】:

    来自user guide

    默认情况下,每次 CV 迭代计算的分数是分数 估计器的方法。可以通过使用 评分参数:

    来自决策树分类器documentation

    返回给定测试数据和标签的平均准确度。在 多标签分类,这是子集准确度 苛刻的指标,因为您要求每个样本的每个标签集都是 正确预测。

    不要被“平均准确率”所迷惑,这只是计算准确率的常规方式。请点击source的链接:

        from .metrics import accuracy_score
        return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
    

    现在sourcemetrics.accuracy_score

    def accuracy_score(y_true, y_pred, normalize=True, sample_weight=None):
        ...
        # Compute accuracy for each possible representation
        y_type, y_true, y_pred = _check_targets(y_true, y_pred)
        if y_type.startswith('multilabel'):
            differing_labels = count_nonzero(y_true - y_pred, axis=1)
            score = differing_labels == 0
        else:
            score = y_true == y_pred
    
        return _weighted_sum(score, sample_weight, normalize)
    

    如果你still aren't convinced:

    def _weighted_sum(sample_score, sample_weight, normalize=False):
        if normalize:
            return np.average(sample_score, weights=sample_weight)
        elif sample_weight is not None:
            return np.dot(sample_score, sample_weight)
        else:
            return sample_score.sum()
    

    注意:accuracy_score normalize 参数默认为True,因此它只返回布尔numpy数组的np.average,因此它只是正确预测的平均数。

    【讨论】:

    • 感谢 juanpa.arrivillaga,如果是二分类问题,每个预测要么正确,要么错误。对准确性意味着什么感到困惑?
    • 感谢 juanpa 的耐心回答,将您的回复标记为答案。
    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 2019-02-28
    • 2015-06-15
    • 2017-04-17
    • 2018-08-26
    • 2011-05-06
    • 2013-04-21
    • 2013-10-24
    相关资源
    最近更新 更多