自定义评分功能GridsearchCV

时间:2019-01-05 06:21:08

标签: python scikit-learn scoring gridsearchcv

我想为GridsearchCV实现自定义评分功能。我试图看一下这个示例https://stats.stackexchange.com/questions/110599/how-to-get-both-mse-and-r2-from-a-sklearn-gridsearchcv,但是我的代码最终陷入了无限循环,它一直运行而没有给出任何结果...这是我正在使用的代码:

from sklearn.linear_model import SGDClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics.scorer import make_scorer

def my_custom_loss_func(y_true, y_pred):
    ps = precision_score(y_true, y_pred)    
    perc_bet =  np.count_nonzero(y_pred)/len(y_pred)

    result = (ps + perc_bet) / (1/ps)

    return result

scorer = make_scorer(my_custom_loss_func, greater_is_better=True)

model = SGDClassifier(random_state=0)
grid_values = {
    'tol': [0.0001, 0.005]
}

grid = GridSearchCV(model, param_grid = grid_values, scoring=scorer, n_jobs=-1, return_train_score=False)
grid.fit(X_train_scaled, y_train)

print('Grid best parameter: ', grid.best_params_)
print('Grid best score: ', grid.best_score_)

我正在Jupyter笔记本中运行此代码。

谢谢!

0 个答案:

没有答案