Keras Sklearn Tuner module ‘sklearn’ has no attribute ‘pipeline’

from sklearn import ensemble
from sklearn import linear_model

def build_model(hp):
    model_type = hp.Choice('model_type', ['random_forest', 'ridge'])
    if model_type == 'random_forest':
        with hp.conditional_scope('model_type', 'random_forest'):
            model = ensemble.RandomForestClassifier(
                n_estimators=hp.Int('n_estimators', 10, 50, step=10),
                max_depth=hp.Int('max_depth', 3, 10))
    elif model_type == 'ridge':
        with hp.conditional_scope('model_type', 'ridge'):
            model = linear_model.RidgeClassifier(
                alpha=hp.Float('alpha', 1e-3, 1, sampling='log'))
    else:
        raise ValueError('Unrecognized model_type')
    return model

tuner = kt.tuners.Sklearn(
        oracle=kt.oracles.BayesianOptimization(
            objective=kt.Objective('score', 'max'),
            max_trials=10),
        hypermodel=build_model,
        directory=".")

X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = model_selection.train_test_split(
    X, y, test_size=0.2)

tuner.search(X_train, y_train)

best_model = tuner.get_best_models(num_models=1)[0]

on execution of this code from the samples on keras-tuner https://keras.io/api/keras_tuner/tuners/sklearn/

I am getting the error shown below. What should be the fix?

c:users99ansappdatalocalprogramspythonpython39libsite-packageskeras_tunertunerssklearn_tuner.py in run_trial(self, trial, X, y, sample_weight, groups)
    161                 sample_weight[train_indices] if sample_weight is not None else None
    162             )
--> 163 
    164             model = self.hypermodel.build(trial.hyperparameters)
    165             #if isinstance(model, Pipeline):

AttributeError: module 'sklearn' has no attribute 'pipeline'

Answer

Adding import sklearn.pipeline would temporarily fix the problem.

This is a very recent issue and will be fixed in the next release.

You can find more about it here https://github.com/keras-team/keras-tuner/issues/600