我可以采用 optuna 函数的最佳参数和最佳模型,并直接在我的笔记本中应用该模型吗?

can i take best parameters and best model of optuna function and apply this model directly in my notebook?

我建立了 optuna 的功能来为我的数据找出 gbm 和 xgboost 的最佳模型,但我想知道我是否可以采用最佳模型并将其直接应用到我的笔记本中(提取最佳模型作为对象以重用稍后) 这是我的 objective 函数:

import lightgbm as lgb 
import optuna
import sklearn.metrics
from xgboost import XGBRegressor
from optuna.integration import XGBoostPruningCallback
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
best_booster = None
gbm = None
def objective(trial,random_state=22,n_jobs=1,early_stopping_rounds=50):
    
    regrosser_name = trial.suggest_categorical("regressor", ["XGBoost", "lightgbm"])
    train_x, valid_x, train_y, valid_y = train_test_split(X_train, y_train, test_size=0.25)
    dtrain = lgb.Dataset(train_x, label=train_y)
    # Step 2. Setup values for the hyperparameters:
    if regrosser_name == 'XGBoost':
        params = {
        "verbosity": 0,  # 0 (silent) - 3 (debug)
        "objective": "reg:squarederror",
        "n_estimators": 10000,
        "max_depth": trial.suggest_int("max_depth", 4, 12),
        "learning_rate": trial.suggest_loguniform("learning_rate", 0.005, 0.05),
        "colsample_bytree": trial.suggest_loguniform("colsample_bytree", 0.2, 0.6),
        "subsample": trial.suggest_loguniform("subsample", 0.4, 0.8),
        "alpha": trial.suggest_loguniform("alpha", 0.01, 10.0),
        "lambda": trial.suggest_loguniform("lambda", 1e-8, 10.0),
        "gamma": trial.suggest_loguniform("lambda", 1e-8, 10.0),
        "min_child_weight": trial.suggest_loguniform("min_child_weight", 10, 1000),
        "seed": random_state,
        "n_jobs": n_jobs,
        }
        model = XGBRegressor(**params)
        model.fit(train_x, train_y)
        y_pred = model.predict(X_val)
        accuracy_rf = sklearn.metrics.mean_absolute_error(valid_y, y_pred)
        return accuracy_rf
    
        print(rf_max_depth)
        print(rf_n_estimators)
        
    else:
        param = {
        "objective": "binary",
        "metric": "binary_logloss",
        "verbosity": -1,
        "boosting_type": "gbdt",
        "lambda_l1": trial.suggest_float("lambda_l1", 1e-8, 10.0, log=True),
        "lambda_l2": trial.suggest_float("lambda_l2", 1e-8, 10.0, log=True),
        "num_leaves": trial.suggest_int("num_leaves", 2, 256),
        "feature_fraction": trial.suggest_float("feature_fraction", 0.4, 1.0),
        "bagging_fraction": trial.suggest_float("bagging_fraction", 0.4, 1.0),
        "bagging_freq": trial.suggest_int("bagging_freq", 1, 7),
        "min_child_samples": trial.suggest_int("min_child_samples", 5, 100),
        }
        gbm = lgb.train(param, dtrain)
        preds_gbm = gbm.predict(valid_x)
        pred_labels_gbm = np.rint(preds_gbm)
        accuracy_gbm = sklearn.metrics.mean_absolute_error(valid_y, pred_labels_gbm)
        return accuracy_gbm

这是我试图解决这个问题的方法:

def callback(study, trial):
    global best_booster
    if study.best_trial == trial:
        best_booster = gbm
if __name__ == "__main__":
    study = optuna.create_study(direction="maximize")
    study.optimize(objective, n_trials=100, callbacks=[callback])

我认为它是关于导入的东西,如果对我的 optuna 功能有任何提示,请说明

如果我没有正确理解你的问题,那么是的,这就是模型的用途。

比如将您保存的模型带到您的笔记本中,为其提供与您用于训练它的结构相同的数据,它应该可以达到目的。或者在管道中使用它。

即使是1行与np数组相同的结构也可以使用。例如,我的模型预测是否应批准贷款。

例如,一位银行客户想要贷款并提交他的信息。银行职员将此信息输入系统。系统将此信息转换为单个 np 数组,其结构与用于训练模型的数据集相同。

然后系统使用该模型来预测贷款是否应该被批准。

我将我的 optuna xgb 模型保存为 json,例如

my_model.get_booster().save_model(f'{保存路径}my_model.json')