我在XGBoos中的优化代码上有一个错误

2024-04-26 12:16:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我只是在学习Python,并在网上发现各种不同质量的示例

总之,我复制了XGBoost优化器的代码如果我运行XGBClassifier的代码,它可以正常工作(如果我将标签设为1或-1输出),但是如果我将代码修改为XGBRegressor(用于尝试预测股价),如果失败了;知道是什么原因吗

我也有一个奇怪的错误为最终pyplot代码在底部当我运行它什么都没有发生,没有错误,只是什么都没有

我用的是Windows上的Jupyter笔记本

model = XGBRegressor()
n_estimators = [5, 10, 25, 50, 75, 100, 150, 200, 250, 300, 400, 500]
print(max_depth)
param_grid = dict(max_depth=max_depth, n_estimators=n_estimators)
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)
grid_search = GridSearchCV(model, param_grid, scoring="neg_log_loss", n_jobs=-1, cv=kfold, verbose=1)
grid_result = grid_search.fit(X, label_encoded_y)

scores = numpy.array(means).reshape(len(max_depth), len(n_estimators))
for i, value in enumerate(max_depth):
    pyplot.plot(n_estimators, scores[i], label='depth: ' + str(value))
pyplot.legend()
pyplot.xlabel('n_estimators')
pyplot.ylabel('Log Loss')
pyplot.savefig('n_estimators_vs_max_depth.png')

正如我所提到的,如果我改为XGBClassifier,这就可以了……除了底部的pyplot代码,它什么也不返回,甚至一个错误都不返回


Tags: 代码searchmodelparam错误labelmaxgrid