How to prune random forest regression in Optuna?

41 views Asked by At

I am working on machine learning model and trying to tune hyperparameters with Optuna. I want to try pruning, but I dont know how to implement this feature. I am using random forest regressor and everything works well.

def objective(trial):
    n_estimators = trial.suggest_int('n_estimators', 100, 1000)
    max_depth = trial.suggest_int('max_depth', 5, 50)
    min_samples_split = trial.suggest_int('min_samples_split', 2, 30)
    min_samples_leaf = trial.suggest_int('min_samples_leaf', 1, 10)
    max_samples = trial.suggest_float('max_samples', 0.5, 1.0)
    max_features = trial.suggest_int('max_features', 5, 30)
    max_leaf_nodes = trial.suggest_int('max_leaf_nodes', 100, 200)

    model = RandomForestRegressor(n_estimators=n_estimators,
                              max_depth=max_depth,
                              min_samples_split=min_samples_split,
                              min_samples_leaf=min_samples_leaf,
                              max_samples=max_samples,
                              max_features=max_features,
                              max_leaf_nodes=max_leaf_nodes)

    kFold = KFold(n_splits=5)
    scores = cross_val_score(model, X_train_transformed, y_train, cv=kFold, scoring='r2', n_jobs=-1)
    mean_score = np.mean(scores)

    return mean_score


study = optuna.create_study(direction = 'maximize',
                        sampler=optuna.samplers.TPESampler(multivariate=True))
study.optimize(objective, n_trials=300)

How can I implement pruning to my objective function?

0

There are 0 answers