如何在sklearn cross_val_score中使用自定义评分功能 [英] How to use custom scoring function in sklearn cross_val_score

查看:191
本文介绍了如何在sklearn cross_val_score中使用自定义评分功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 cross_val_score 函数中使用 Adjusted Rsquare .我尝试使用 make_scorer 函数,但无法正常工作.

I want to use Adjusted Rsquare in the cross_val_score function. I tried with make_scorer function but it is not working.

from sklearn.cross_validation import train_test_split
X_tr, X_test, y_tr, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

regression = LinearRegression(normalize=True)
from sklearn.metrics.scorer import make_scorer
from sklearn.metrics import r2_score
def adjusted_rsquare(y_true,y_pred):
    adjusted_r_squared = 1 - (1-r2_score(y_true, y_pred))*(len(y_pred)-1)/(len(y_pred)-X_test.shape[1]-1)
    return adjusted_r_squared

my_scorer = make_scorer(adjusted_rsquare, greater_is_better=True)
score = np.mean(cross_val_score(regression, X_tr, y_tr, scoring=my_scorer,cv=crossvalidation, n_jobs=1))

正在处理错误:

IndexError: positional indexers are out-of-bounds

有什么方法可以使用我的自定义函数,即; adjusted_rsquare cross_val_score ?

Is there any way to use my custom function i.e; adjusted_rsquare with cross_val_score?

推荐答案

adjusted_rsquare(X,Y)是一个数字,它不是一个函数,只需像这样创建计分器即可:

adjusted_rsquare(X,Y) is a number, it's not a function, just create the scorer like this:

my_scorer = make_scorer(adjusted_rsquare, greater_is_better=True)

您还需要更改得分功能:

def adjusted_rsquare(y_true, y_pred, **kwargs):

这是您应该使用的原型.您将实际结果与应有的结果进行比较.

That's the prototype that you should use. You compare the actual result to the result it should have been.

这篇关于如何在sklearn cross_val_score中使用自定义评分功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆