gpt4 book ai didi

python - 用于网格搜索分类的自定义评分函数

转载 作者:行者123 更新时间:2023-11-28 22:18:37 26 4
gpt4 key购买 nike

我想在 scikit-learn 中为 RandomForestClassifier 执行 GridSearchCV,并且我有一个我想使用的自定义评分函数。

评分函数只有在提供概率时才会起作用(例如,必须调用 rfc.predict_proba(...) 而不是 rfc.predict(...) )

如何指示 GridSearchCV 使用 predict_proba() 而不是 predict()

from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier

def my_custom_loss_func(ground_truth, predictions):
# predictions must be probabilities - e.g. model.predict_proba()
# example code here:
diff = np.abs(ground_truth - predictions).max()
return np.log(1 + diff)

param_grid = {'min_samples_leaf': [1, 2, 5, 10, 20, 50, 100], 'n_estimators': [100, 200, 300]}
grid = GridSearchCV(RandomForestClassifier(), param_grid=param_grid,
scoring=my_custom_loss_func)

最佳答案

参见文档 here : 可调用的应该有参数 (estimator, X, y)

然后你可以在你的定义中使用,estimator.predict_proba(X)

或者,您可以使用 make_scorer使用 needs_proba=True

一个完整的代码示例:

from sklearn.datasets import make_classification
from sklearn.model_selection import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import make_scorer
import pandas as pd
import numpy as np

X, y = make_classification()
def my_custom_loss_func_est(estimator, X, y):
# predictions must be probabilities - e.g. model.predict_proba()
# example code here:
diff = np.abs(y - estimator.predict_proba(X)[:, 1]).max()
return -np.log(1 + diff)

def my_custom_loss_func(ground_truth, predictions):
# predictions must be probabilities - e.g. model.predict_proba()
# example code here:
diff = np.abs(ground_truth - predictions[:, 1]).max()
return np.log(1 + diff)

custom_scorer = make_scorer(my_custom_loss_func,
greater_is_better=False,
needs_proba=True)

使用记分对象:

param_grid = {'min_samples_leaf': [10, 50], 'n_estimators': [100, 200]}
grid = GridSearchCV(RandomForestClassifier(), param_grid=param_grid,
scoring=custom_scorer, return_train_score=True)
grid.fit(X, y)
pd.DataFrame(grid.cv_results_)[['mean_test_score',
'mean_train_score',
'param_min_samples_leaf',
'param_n_estimators']]

mean_test_score mean_train_score param_min_samples_leaf param_n_estimators
0 -0.505201 -0.495011 10 100
1 -0.509190 -0.498283 10 200
2 -0.406279 -0.406292 50 100
3 -0.406826 -0.406862 50 200

直接使用损失函数也很简单

grid = GridSearchCV(RandomForestClassifier(), param_grid=param_grid,
scoring=my_custom_loss_func_est, return_train_score=True)
grid.fit(X, y)
pd.DataFrame(grid.cv_results_)[['mean_test_score',
'mean_train_score',
'param_min_samples_leaf',
'param_n_estimators']]

mean_test_score mean_train_score param_min_samples_leaf param_n_estimators
0 -0.509098 -0.491462 10 100
1 -0.497693 -0.490936 10 200
2 -0.409025 -0.408957 50 100
3 -0.409525 -0.409500 50 200

结果因 cv 折叠不同而不同(我假设,但我现在懒得设置种子并再次编辑(或者是否有更好的方法来粘贴代码而无需手动缩进所有内容?))

关于python - 用于网格搜索分类的自定义评分函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50379374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com