XGBoost调参
XGBoost回归
1、先调迭代次数n_estimators
1 from xgboost import XGBRegressor
2 from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error # 评价标准
3 from sklearn.model_selection import train_test_split,GridSearchCV,cross_val_score
4
5 # 调 n_estimators 参数(迭代次数)
6 ScoreAll = []
7 for i in range(60,100,1): # 这里自己慢慢调整
8 model = XGBRegressor(n_estimators = i)
9 score = cross_val_score(model, X_train,Y_train,cv=10).mean()
10 ScoreAll.append([i,score])
11 ScoreAll = np.array(ScoreAll)
12
13 max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0] # 找出最高得分对应的索引
14 print("最优参数以及最高得分:",ScoreAll[max_score])
15 plt.figure()
16 plt.xlabel('n_estimators',size=13)
17 plt.ylabel('score',size=13)
18 plt.plot(ScoreAll[:,0],ScoreAll[:,1])
19 plt.grid() # 生成网格
20 plt.show()
2、学习率/迭代步长learning_rate
1 # 学习率/迭代步长
2 ScoreAll = []
3 # for i in [0.05, 0.07, 0.1, 0.2, 0.25]:
4 for i in [0.05, 0.07, 0.1, 0.15]:
5 model = XGBRegressor(n_estimators=90, # 这里的90是上面测出最好的结果
6 learning_rate=i)
7 score = cross_val_score(model, X_train,Y_train,cv=10).mean()
8 ScoreAll.append([i,score])
9 ScoreAll = np.array(ScoreAll)
10
11 max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0]
12 print("最优参数以及最高得分:",ScoreAll[max_score])
13 plt.figure()
14 plt.xlabel('learning_rate',size=13)
15 plt.ylabel('score',size=13)
16 plt.grid() # 生成网格
17 plt.plot(ScoreAll[:,0],ScoreAll[:,1])
18 plt.show()
3、树的最大深度max_depth
1 # max_depth的最佳参数(树的最大深度)
2 ScoreAll = []
3 for i in [1,2,3, 4, 5, 6, 7, 8, 9, 10]:
4 model = XGBRegressor(n_estimators=90,
5 learning_rate=0.1,
6 max_depth=i)
7 score = cross_val_score(model, X_train,Y_train,cv=10).mean()
8 ScoreAll.append([i,score])
9 ScoreAll = np.array(ScoreAll)
10
11 max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0]
12 print("最优参数以及最高得分:",ScoreAll[max_score])
13 plt.figure()
14 plt.xlabel('max_depth',size=13)
15 plt.ylabel('score',size=13)
16 plt.grid() # 生成网格
17 plt.plot(ScoreAll[:,0],ScoreAll[:,1])
18 plt.show()
4、subsample
1 ScoreAll = []
2 for i in range(6,11):
3 model = XGBRegressor(n_estimators=90,
4 learning_rate=0.1,
5 max_depth=3,
6 subsample=i/10.0)
7 score = cross_val_score(model, X_train,Y_train,cv=10).mean()
8 ScoreAll.append([i,score])
9 ScoreAll = np.array(ScoreAll)
10
11 max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0]
12 print("最优参数以及最高得分:",ScoreAll[max_score])
13 plt.figure()
14 plt.xlabel('subsample',size=13)
15 plt.ylabel('score',size=13)
16 plt.grid() # 生成网格
17 plt.plot(ScoreAll[:,0],ScoreAll[:,1])
18 plt.show()
tips!:这个参数调整很神奇,感觉根本不需要上面麻烦的代码,出来的结果也不是最优的,还不如直接拟合看结果,从0.2-1.0慢慢试,就会找到最好的值。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律