我想为 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 笔记本中运行此代码。
提前致谢!