XGBoost的保存模型、加载模型、继续训练
假设有这样一个模型:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# coding: utf-8 import numpy as np import pandas as pd import xgboost as xgb # 构造模型 model = xgb.XGBRegressor( learning_rate=0.2, n_estimators=2000, max_depth=10, min_child_weight=1, gamma=8, subsample=0.8, colsample_bytree=0.8, objective="reg:linear", booster="gbtree", n_jobs=4, scale_pos_weight=1, random_state=27 ) # 训练 model.fit(X_train, Y_train) # 预测 Y_pred = model.predict(X_test) |
保存模型:
|
1 2 3 4 5 6 7 |
# coding: utf-8 # 模型路径 model_path = "./model.mdl" # 保存模型 model.get_booster().save_model(model_path) |
加载模型:
|
1 2 3 4 5 6 7 8 9 10 |
# coding: utf-8 # 模型路径 model_path = "./model.mdl" # 加载模型 model = xgb.Booster(model_file=model_path) # 要先将数据转换为矩阵,才能进行预测 Y_pred = model.predict(xgb.DMatrix(X_test)) |
继续训练:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# coding: utf-8 # 模型路径 model_path = "./model.mdl" # 构造模型 model = xgb.XGBRegressor( learning_rate = 0.01, n_estimators = 2000, max_depth = 4, min_samples_split = 2300, min_samples_leaf = 50, min_child_weight = 1, gamma = 8, subsample = 0.8, colsample_bytree = 0.8, objective = "reg:linear", booster = "gbtree", n_jobs = 4, max_features = "sqrt", scale_pos_weight = 1, random_state = 27 ) # 加载目标路径的模型做继续训练 model.fit(X_train, Y_train, xgb_model=model_path) # 保存模型 model.get_booster().save_model(model_path) |