如何在sklearn中使用自定义评分函数 cross_val_score

mer*_*kle 6 python machine-learning python-3.x scikit-learn sklearn-pandas

我想在函数中使用调整后的 Rsquarecross_val_score。我尝试使用make_scorer功能但它不起作用。

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))
Run Code Online (Sandbox Code Playgroud)

它抛出一个错误:

IndexError: positional indexers are out-of-bounds
Run Code Online (Sandbox Code Playgroud)

有什么方法可以使用我的自定义功能,即;adjusted_rsquarecross_val_score

Mat*_*her 7

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

my_scorer = make_scorer(adjusted_rsquare, greater_is_better=True)
Run Code Online (Sandbox Code Playgroud)

您还需要更改得分函数

def adjusted_rsquare(y_true, y_pred, **kwargs):
Run Code Online (Sandbox Code Playgroud)

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