| import xgboost as xgb |
| from xgboost import plot_importance |
| from xgboost.sklearn import XGBClassifier,XGBRegressor |
| xgb_reg = xgb.XGBRegressor(n_estimators=750 |
| ,max_depth=9 |
| ,colsample_bytree=0.5 |
| ,reg_alpha=0.05 |
| ,subsample=0.6 |
| ) |
| xgb_reg.fit(X_train, y_train) |
| y_pred=xgb_reg.predict(X_test) |
| print(y_pred) |
| |
| xgb_reg.score(X_test,y_test) |
| |
| metrics.mean_squared_error(y_test, y_pred) |
| xgb_reg.feature_importances_ |
| plt.figure(figsize=(15, 10)) |
| |
| t = np.arange(len(X_test)) |
| |
| plt.plot(t, y_test, 'r', linewidth=2, label='真实值') |
| |
| plt.plot(t, y_pred, 'g', linewidth=2, label='预测值') |
| |
| plt.legend() |
| plt.show() |

粗调
| |
| xgb_params = {'max_depth':[6,9], |
| 'subsample':[0.6,0.9,1], |
| 'colsample_bytree':[0.5,0.6], |
| 'reg_alpha':[0,0.05,0.1], |
| 'n_estimators':[750,1000]} |
| |
| xgb_gridsearch = GridSearchCV(xgb_reg,xgb_params,cv=3,n_jobs=-1, |
| verbose=10,refit=True) |
| xgb_gridsearch.fit(X_train, y_train) |
| y_pred=xgb_gridsearch.predict(X_test) |
| print(y_pred) |
| |
| print("r2:", xgb_gridsearch.score(X_test, y_test)) |
| |
| print("MSE:", metrics.mean_squared_error(y_test, y_pred)) |
| print(xgb_gridsearch.best_params_) |
将得到的参数,代入模型,重跑。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?