python决策树预测模型_带决策树回归模型的负交叉值得分
TL、DR:
1)不,除非您顯式指定,或者它是估計(jì)器的默認(rèn).score方法。因?yàn)槟鷽]有,它默認(rèn)為DecisionTreeRegressor.score,它返回決定系數(shù),即R^2。可能是負(fù)數(shù)。在
2)是的,這是個(gè)問題。這也解釋了為什么你會(huì)得到一個(gè)負(fù)的決定系數(shù)。在
細(xì)節(jié):
您使用的函數(shù)如下:scores = cross_val_score(simple_tree, df.loc[:,'system':'gwno'], df['gdp_growth'], cv=cv)
所以你沒有顯式地傳遞一個(gè)“scoring”參數(shù)。讓我們看看docs:scoring : string, callable or None, optional, default: None
A string (see model evaluation documentation) or a scorer callable object / function with signature scorer(estimator, X, y).
所以它沒有明確說明,但這可能意味著它使用了估計(jì)器的默認(rèn).score方法。在
為了證實(shí)這個(gè)假設(shè),讓我們深入研究source code。我們看到最終使用的記分器如下:
^{pr2}$
has_scoring = scoring is not None
if not hasattr(estimator, 'fit'):
raise TypeError("estimator should be an estimator implementing "
"'fit' method, %r was passed" % estimator)
if isinstance(scoring, six.string_types):
return get_scorer(scoring)
elif has_scoring:
# Heuristic to ensure user has not passed a metric
module = getattr(scoring, '__module__', None)
if hasattr(module, 'startswith') and \
module.startswith('sklearn.metrics.') and \
not module.startswith('sklearn.metrics.scorer') and \
not module.startswith('sklearn.metrics.tests.'):
raise ValueError('scoring value %r looks like it is a metric '
'function rather than a scorer. A scorer should '
'require an estimator as its first parameter. '
'Please use `make_scorer` to convert a metric '
'to a scorer.' % scoring)
return get_scorer(scoring)
elif hasattr(estimator, 'score'):
return _passthrough_scorer
elif allow_none:
return None
else:
raise TypeError(
"If no scoring is specified, the estimator passed should "
"have a 'score' method. The estimator %r does not." % estimator)
所以請(qǐng)注意,scoring=None已經(jīng)完成,所以:has_scoring = scoring is not None
暗示has_scoring == False。另外,估計(jì)器有一個(gè).score屬性,所以我們要通過這個(gè)分支:elif hasattr(estimator, 'score'):
return _passthrough_scorer
這很簡單:def _passthrough_scorer(estimator, *args, **kwargs):
"""Function that wraps estimator.score"""
return estimator.score(*args, **kwargs)
最后,我們現(xiàn)在知道scorer就是你的估計(jì)器默認(rèn)的score。讓我們檢查一下docs for the estimator,它清楚地表明:Returns the coefficient of determination R^2 of the prediction.
The coefficient R^2 is defined as (1 - u/v), where u is the regression
sum of squares ((y_true - y_pred) ** 2).sum() and v is the residual
sum of squares ((y_true - y_true.mean()) ** 2).sum(). Best possible
score is 1.0 and it can be negative (because the model can be
arbitrarily worse). A constant model that always predicts the expected
value of y, disregarding the input features, would get a R^2 score of
0.0.
所以看起來你的分?jǐn)?shù)實(shí)際上就是決定系數(shù)。所以,基本上,R^2為負(fù)值,意味著你的模型表現(xiàn)得很差。比我們僅僅預(yù)測每個(gè)輸入的期望值(即平均值)更糟糕。這是有道理的,因?yàn)檎缒闼f:I have a small sample of ~40 observations and ~70 variables. Might
this be the problem?
這是個(gè)問題。當(dāng)你只有40個(gè)觀測值時(shí),對(duì)一個(gè)70維的問題空間進(jìn)行有意義的預(yù)測幾乎是沒有希望的。在
總結(jié)
以上是生活随笔為你收集整理的python决策树预测模型_带决策树回归模型的负交叉值得分的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Website for the intr
- 下一篇: python 下的数据结构与算法---6