Created an objective function for optuna to find the best parameters for KNN regressor but faced with this error:
ValueError: CategoricalDistribution does not support dynamic value space
Any suggestions as to why this is happening?
def objective(trial):
params = {
'n_neighbors': trial.suggest_int('n_neighbors', 2, 10, step=2),
'algorithm': trial.suggest_categorical('weights', ['auto', 'ball_tree', 'kd_tree', 'brute']),
'weights': trial.suggest_categorical("weights", ['uniform', 'distance']),
"leaf_size": trial.suggest_int("leaf_size", 10, 60, step=10),
"p": trial.suggest_categorical("p", [1, 2]),
}
regression_model = KNeighborsRegressor(**params)
regression_model.fit(x_train.values, y_train.values)
y_pred = regression_model.predict(x_test)
rmse = mean_squared_error(y_test, y_pred)
return rmse
find_params = optuna.create_study(direction='minimize')
find_params.optimize(objective, n_trials=5)
You have "weights" parameter defined twice. Change from
'algorithm': trial.suggest_categorical('weights', ['auto', 'ball_tree', 'kd_tree', 'brute']),
'weights': trial.suggest_categorical("weights", ['uniform', 'distance']),
to
'algorithm': trial.suggest_categorical('algorithm', ['auto', 'ball_tree', 'kd_tree', 'brute']),
'weights': trial.suggest_categorical("weights", ['uniform', 'distance']),
and the error should go away. Optuna does not support having multiple parameters with same name but different value space.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With