gpt4 book ai didi

使用 RandomizedSearchCV 对 XGBClassifier 进行 Python 超参数优化

转载 作者:太空宇宙 更新时间:2023-11-04 08:42:14 24 4
gpt4 key购买 nike

我正在尝试为 XGBClassifier 获取最佳超参数,这将导致获得最具预测性的属性。我正在尝试使用 RandomizedSearchCV 通过 KFold 进行迭代和验证。

当我总共运行此过程 5 次 (numFolds=5) 时,我希望将最好的结果保存在名为 collector 的数据框中(在下面指定)。因此,每次迭代,我都希望将最佳结果和分数附加到收集器数据框。

 from scipy import stats
from scipy.stats import randint
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import
precision_score,recall_score,accuracy_score,f1_score,roc_auc_score

clf_xgb = xgb.XGBClassifier(objective = 'binary:logistic')
param_dist = {'n_estimators': stats.randint(150, 1000),
'learning_rate': stats.uniform(0.01, 0.6),
'subsample': stats.uniform(0.3, 0.9),
'max_depth': [3, 4, 5, 6, 7, 8, 9],
'colsample_bytree': stats.uniform(0.5, 0.9),
'min_child_weight': [1, 2, 3, 4]
}
clf = RandomizedSearchCV(clf_xgb, param_distributions = param_dist, n_iter = 25, scoring = 'roc_auc', error_score = 0, verbose = 3, n_jobs = -1)

numFolds = 5
folds = cross_validation.KFold(n = len(X), shuffle = True, n_folds = numFolds)

collector = pd.DataFrame()
estimators = []
results = np.zeros(len(X))
score = 0.0

for train_index, test_index in folds:
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
clf.fit(X_train, y_train)
estimators.append(clf.best_estimator_)
estcoll = pd.DataFrame(estimators)


estcoll['score'] = score
pd.concat([collector,estcoll])
print "\n", len(collector), "\n"
score /= numFolds

由于某种原因,没有任何内容被保存到数据框中,请帮忙。

另外,我有大约 350 个属性要循环,其中 3.5K 行在训练中,2K 行在测试中。通过贝叶斯超参数优化过程运行它是否可能会改善我的结果?还是只会节省处理时间?

最佳答案

RandomizedSearchCV() 将为您做的比您意识到的要多。探索适合的 CV 对象的 cv_results 属性 at the documentation page

这是您的代码,几乎没有变化。我添加的两个更改:

  1. 我将 n_iter=5 从 25 改为了。这将执行 5 组参数,通过 5 折交叉验证意味着总共 25 次拟合。
  2. 我在 RandomizedSearchCV 之前定义了你的 kfold 对象,然后在构建 RandomizedSearchCV 时将其作为 cv 参数引用

_

clf_xgb = xgb.XGBClassifier(objective = 'binary:logistic')
param_dist = {'n_estimators': stats.randint(150, 1000),
'learning_rate': stats.uniform(0.01, 0.59),
'subsample': stats.uniform(0.3, 0.6),
'max_depth': [3, 4, 5, 6, 7, 8, 9],
'colsample_bytree': stats.uniform(0.5, 0.4),
'min_child_weight': [1, 2, 3, 4]
}

numFolds = 5
kfold_5 = cross_validation.KFold(n = len(X), shuffle = True, n_folds = numFolds)

clf = RandomizedSearchCV(clf_xgb,
param_distributions = param_dist,
cv = kfold_5,
n_iter = 5, # you want 5 here not 25 if I understand you correctly
scoring = 'roc_auc',
error_score = 0,
verbose = 3,
n_jobs = -1)

这是我的答案与您的代码明显不同的地方。只适合randomizedsearchcv对象一次,无需循环。它使用它的 cv 参数处理 CV 循环。

clf.fit(X_train, y_train)

您所有的交叉验证结果现在都在 clf.cv_results_ 中。例如,您可以通过以下方式获得交叉验证(平均 5 次)训练分数:clf.cv_results_['mean_train_score'] 或使用 clf.cv_results_['mean_test_score'] 进行交叉验证的测试集(保留数据)得分。您还可以获得其他有用的东西,例如 mean_fit_timeparamsclf,一旦拟合,将自动记住您的 best_estimator_ 作为一个属性。

这些与确定模型拟合的最佳超参数集相关。对于来自 n_iter 的单次迭代中使用的 5 折中的每一个,一组超参数是恒定的,因此您不必查看迭代中折之间的不同分数。

关于使用 RandomizedSearchCV 对 XGBClassifier 进行 Python 超参数优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43927725/

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