Skip to content

Instantly share code, notes, and snippets.

@himkt
Last active May 19, 2020 13:53
Show Gist options
  • Select an option

  • Save himkt/ecaef9794703de075642496962065405 to your computer and use it in GitHub Desktop.

Select an option

Save himkt/ecaef9794703de075642496962065405 to your computer and use it in GitHub Desktop.
import sklearn
import sklearn.datasets
import sklearn.ensemble
import sklearn.model_selection
import sklearn.svm
import optuna
# Define an objective function to be minimized.
def objective(trial):
# Invoke suggest methods of a Trial object to generate hyperparameters.
regressor_name = trial.suggest_categorical("classifier", ["SVR", "RandomForest"])
if regressor_name == "SVR":
svr_c = trial.suggest_loguniform("svr_c", 1e-10, 1e10)
regressor_obj = sklearn.svm.SVR(C=svr_c)
else:
rf_max_depth = trial.suggest_int("rf_max_depth", 2, 32)
regressor_obj = sklearn.ensemble.RandomForestRegressor(max_depth=rf_max_depth)
X, y = sklearn.datasets.load_boston(return_X_y=True)
X_train, X_valid, y_train, y_valid = sklearn.model_selection.train_test_split(X, y, random_state=0)
regressor_obj.fit(X_train, y_train)
y_pred = regressor_obj.predict(X_valid)
error = sklearn.metrics.mean_squared_error(y_valid, y_pred)
return error # A objective value linked with the Trial object.
study = optuna.create_study() # Create a new study.
study.optimize(objective, n_trials=100) # Invoke optimization of the objective function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment