Tom Duym
Tom Duym

Reputation: 31

Cannot clone object ' does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods

I have:

import numpy as np
import tensorflow as tf
from sklearn.preprocessing import minmax_scale
from sklearn.model_selection import GridSearchCV, KFold

# Extract the main and side frequency factors concentractions and catalyst concentrations from the data
main_freq_factors = np.array(FreqFac['Main frequency factors concentraction (l/(mol*s))'])
side_freq_factors = np.array(FreqFac['Side frequency factors concentraction (1/s)'])
catalyst_conc = np.array(FreqFac.iloc[:,1].values)

# Scale the data using the min-max scaler
main_freq_factors_scaled = minmax_scale(main_freq_factors)
side_freq_factors_scaled = minmax_scale(side_freq_factors)
catalyst_conc_scaled = minmax_scale(catalyst_conc)

# Combine the scaled data into a single array
X = catalyst_conc_scaled
y = np.column_stack((main_freq_factors_scaled, side_freq_factors_scaled))


X_train, X_test, y_train, y_test = train_test_split(Xsc, Ysc, test_size=0.33, random_state=42)

# Define the model
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(32, activation='relu', input_shape=(1,)))
model.add(tf.keras.layers.Dense(1, activation='linear'))

# Compile the model with the mean squared error loss function and Adam optimization algorithm
model.compile(loss='mean_squared_error', optimizer='adam')

# Define the grid of hyperparameters to search
param_grid = {
    'epochs': [50, 100, 150],
    'batch_size': [32, 64, 128]
}

# Create a K-fold cross-validation generator
kfold = KFold(n_splits=5, shuffle=True, random_state=42)

# Create a grid search object using the model, param_grid, and kfold
grid_search = GridSearchCV(model, param_grid, cv=kfold, scoring='neg_mean_squared_error', return_train_score=True)

# Fit the grid search object to the data
grid_search.fit(catalyst_conc.reshape(-1, 1), main_freq_factors)

# Print the results of the grid search
print(grid_search.best_params_)

But I get the error:

Cannot clone object '<keras.engine.sequential.Sequential object at 0x7eff87f72580>' (type <class 'keras.engine.sequential.Sequential'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.

What am I doing wrong?

Upvotes: 2

Views: 4798

Answers (1)

Helder
Helder

Reputation: 548

The error comes from scikit's clone which is used by GridSearchCV (through BaseSearchCV) to create a separate copy of the estimator for each parameter configuration during the grid search.

You could try using a wrapper for the Scikit-Learn API:

from keras.wrappers.scikit_learn import KerasRegressor

# ...

def build_fn():
  # Define the model
  model = tf.keras.Sequential()
  model.add(tf.keras.layers.Dense(32, activation='relu', input_shape=(1,)))
  model.add(tf.keras.layers.Dense(1, activation='linear'))

  # Compile the model with the mean squared error loss function and Adam optimization algorithm
  model.compile(loss='mean_squared_error', optimizer='adam')
  return model

model = KerasRegressor(build_fn)

# ...

Another option is to pip install scikeras and replace the import above with:

from scikeras.wrappers import KerasRegressor

Upvotes: 2

Related Questions