python - 当我使用 XGBOOST 回归时,max_depth 的不同值只能得到负分
问题描述
我的代码运行良好,但问题是所有的 train_scores 和 test_scores 都是负值。如果您能帮助我找出代码中的问题,我会很高兴。我希望评估模型在不同 max_depth 值下的性能:1、3、5、6、7、9、11、13、15、17、19。
这是我得到的输出:
train_scores = [-1.5330488137183185, -1.253742668302357, -1.212924006705376, -1.209022226685474, -1.2087753262347243, -1.2087753262347243, -1.2087753262347243, -1.2087753262347243, -1.2087753262347243, -1.2087753262347243]
test_scores = [-1.7155087611718076, -1.4551497092616446, -1.427034811918214, -1.4260848404210638, -1.42582407694383, -1.42582407694383, -1.42582407694383, -1.42582407694383, -1.42582407694383, -1.42582407694383]
这是我的代码:
mse_train1, mse_test1,max_depth1, RMSE_Train1, RMSE_Test1, train_scores, test_scores = list(), list(), list(), list(),list(), list(),list()
model = XGBRegressor(learning_rate= 0.04,max_depth= 1,n_estimators= 40,subsample= 0.7)
# Training and Evaluate the model for the dataframe contains important features
# -----------------------------------------------------------------------------
for iter in range(1, 20, 2):
max_depth1.append(iter)
model.fit(train_set, train_set_RET)
y_train_predicted = model.predict(train_set)
train_score = model.score(train_set, train_set_RET)
train_scores.append(train_score)
y_test_predicted = model.predict(test_set)
test_score = model.score(test_set, test_set_RET)
test_scores.append(test_score)
mse_train = mean_squared_error(train_set_RET, y_train_predicted)
mse_train1.append(mse_train)
RMSE_Train = np.sqrt(mse_train)
RMSE_Train1.append (RMSE_Train)
mse_test = mean_squared_error(test_set_RET, y_test_predicted)
mse_test1.append(mse_test)
RMSE_Test = np.sqrt(mse_test)
RMSE_Test1.append (RMSE_Test)
#print('>%d, train: %.3f, test: %.3f' % (i, train_score, test_score))
#print("Iteration: {} Train mse: {} Test mse: {}".format(iter, mse_train, mse_test))
model.max_depth += 2
def plot1_fun (num_trees,mse_train1, mse_test1):
pyplot.plot(num_trees, mse_train1, marker='.', label= 'MSE on Train Data')
pyplot.plot(num_trees, mse_test1, marker='.', label= 'MSE on Test Data')
# axis labels
pyplot.xlabel('max depth')
pyplot.ylabel('Mean Squared Error')
# show the legend
pyplot.legend()
# show the plot
pyplot.show()
plot1_fun(num_trees,mse_train1, mse_test1)
print ('train_scores =', train_scores)
print ('test_scores =', test_scores)
解决方案
推荐阅读
- python - Python 的 lambda 列表只取最后一个值
- html - 如何在 Vuejs 中为选项卡使用路由器视图?
- ios - 如何使用 Swift iOS 向 ST17H26 发送自定义蓝牙命令?
- java - 解析后如何获取多个数组元素并将其传递给数据类
- r - 如何通过 purrr::pmap 正确地将参数从 tibble 传递到函数中
- jquery - Jquery 输入值返回 null(未定义)
- orm - 驱动查询模式不会导致 N+1 问题吗?
- ios - 如何在 Swift 中更改 CDMarkdownKit 中的超链接颜色?
- javascript - 将按钮链接到我也创建的另一个页面
- laravel - Undefined index with Laravel