Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 决策树回归模型的负交叉值_Python_Pandas_Machine Learning_Scikit Learn - Fatal编程技术网

Python 决策树回归模型的负交叉值

Python 决策树回归模型的负交叉值,python,pandas,machine-learning,scikit-learn,Python,Pandas,Machine Learning,Scikit Learn,我正在用交叉评分法评估决策树回归预测模型。问题是,分数似乎是负数,我真的不明白为什么 这是我的代码: all_depths = [] all_mean_scores = [] for max_depth in range(1, 11): all_depths.append(max_depth) simple_tree = DecisionTreeRegressor(max_depth=max_depth) cv = KFold(n_splits=2, shuffle=Tr

我正在用交叉评分法评估决策树回归预测模型。问题是,分数似乎是负数,我真的不明白为什么

这是我的代码:

all_depths = []
all_mean_scores = []
for max_depth in range(1, 11):
    all_depths.append(max_depth)
    simple_tree = DecisionTreeRegressor(max_depth=max_depth)
    cv = KFold(n_splits=2, shuffle=True, random_state=13)
    scores = cross_val_score(simple_tree, df.loc[:,'system':'gwno'], df['gdp_growth'], cv=cv)
    mean_score = np.mean(scores)
    all_mean_scores.append(np.mean(scores))
    print("max_depth = ", max_depth, scores, mean_score, sem(scores))
结果是:

max_depth =  1 [-0.45596988 -0.10215719] -0.2790635315340 0.176906344162 
max_depth =  2 [-0.5532268 -0.0186984] -0.285962600541 0.267264196259 
max_depth =  3 [-0.50359311  0.31992411] -0.0918345038141 0.411758610421 max_depth =  4 [-0.57305355  0.21154193] -0.180755811466 0.392297741456 max_depth =  5 [-0.58994928  0.21180425] -0.189072515181 0.400876761509 max_depth =  6 [-0.71730634  0.22139877] -0.247953784441 0.469352551213 max_depth =  7 [-0.60118621  0.22139877] -0.189893720551 0.411292487323 max_depth =  8 [-0.69635044  0.13976584] -0.278292298411 0.418058142228 max_depth =  9 [-0.78917478  0.30970763] -0.239733577455 0.549441204178 max_depth =  10 [-0.76098227  0.34512503] -0.207928623044 0.553053649792
我的问题如下:

1) 分数返回MSE对吗?如果是的话,为什么是负面的

2) 我有一个约40个观测值和约70个变量的小样本。这可能是问题所在吗


提前感谢。

这是可能的。已经回答了

实际的MSE只是你得到的数字的正版本

统一计分API总是将分数最大化,因此需要最小化的分数将被否定,以便统一计分API正常工作。因此,如果返回的分数是应最小化的分数,则返回的分数为负值,如果返回的分数是应最大化的分数,则返回的分数为正值。

TL,DR: 1) 不,除非您明确指定,或者它是估计器的默认
.score
方法。由于没有,它默认为
DecisionTreeRegressor.score
,返回确定系数,即R^2。这可能是负面的

2) 是的,这是个问题。这解释了为什么你会得到一个负的决定系数

详情如下: 您使用的函数如下所示:

scores = cross_val_score(simple_tree, df.loc[:,'system':'gwno'], df['gdp_growth'], cv=cv)
因此,您没有明确传递“评分”参数。让我们看一下:

评分:字符串,可调用或无,可选,默认值:无

一个字符串(参见模型评估文档)或一个记分器可调用的对象/函数,带有签名记分器(估计器,X,y)

因此,它没有明确说明这一点,但这可能意味着它使用了估计器的默认
.score
方法

为了证实这个假设,让我们深入研究一下。我们看到最终使用的记分员如下:

scorer = check_scoring(estimator, scoring=scoring)
那么,让我们看看

因此请注意,
scoring=None
已执行,因此:

has_scoring = scoring is not None
意味着
的评分=False
。另外,估计器有一个
.score
属性,因此我们通过这个分支:

elif hasattr(estimator, 'score'):
    return _passthrough_scorer
这很简单:

def _passthrough_scorer(estimator, *args, **kwargs):
    """Function that wraps estimator.score"""
    return estimator.score(*args, **kwargs)
最后,我们现在知道,
score
是您的估计器的默认
score
。让我们检查一下,它清楚地表明:

返回预测的确定系数R^2

系数R^2定义为(1-u/v),其中u是回归 平方和((y_真-y_pred)**2)。和()和v是残差 平方和((y_真-y_真.mean())**2.sum()。尽可能 分数为1.0,可以为负值(因为模型可以为 (更糟)。总是预测预期结果的常数模型 不管输入特征如何,y的值将得到R^2分 0.0

看来你的分数实际上是决定系数。所以,基本上,R^2为负值,这意味着你的模型表现非常差。比我们只预测每个输入的预期值(即平均值)更糟糕。这是有道理的,因为正如你所说:

我有一个约40个观测值和约70个变量的小样本。可以 这就是问题所在吗


这是个问题。当你只有40次观察时,对70维问题空间进行有意义的预测几乎是没有希望的

非常感谢你详细的回答。我会尽量减少维度,也会使用评分参数。@Toutsos不用担心。熊猫的
pandas
文档通常非常有用,如果您仍然无法理解,它会提供指向必要源代码的非常方便的链接。如果您觉得这有帮助,您可以接受/upvote.trusted,但声誉不足15,因此不会公开显示。熊猫文档真的很好。我已经发现了评分参数,只是没有足够的统计解释,我只是从机器学习开始。因此,这不是python的问题,而是理解结果是什么以及如何解释它的问题。谢谢。我真的找了,但没找到那篇文章。
def _passthrough_scorer(estimator, *args, **kwargs):
    """Function that wraps estimator.score"""
    return estimator.score(*args, **kwargs)