{"id":7704,"library":"scikeras","title":"Scikit-Learn API wrapper for Keras","description":"Scikeras provides a Scikit-Learn compatible API wrapper for Keras models, allowing Keras deep learning models to be used seamlessly with Scikit-Learn's powerful tools like GridSearchCV, Pipelines, and cross-validation. The current version is 0.13.0, and it follows a somewhat regular release cadence, typically every few months, often coinciding with new Keras or TensorFlow releases.","status":"active","version":"0.13.0","language":"en","source_language":"en","source_url":"https://github.com/adriangb/scikeras","tags":["scikit-learn","keras","tensorflow","deep learning","machine learning","wrapper","sklearn"],"install":[{"cmd":"pip install scikeras keras","lang":"bash","label":"Basic installation with Keras 3 (default backend)"},{"cmd":"pip install scikeras 'keras[tensorflow]'","lang":"bash","label":"Installation with Keras 3 using TensorFlow backend"}],"dependencies":[{"reason":"Scikeras is a wrapper for Keras models. Keras >= 3.0.0 is required for scikeras >= 0.13.0.","package":"keras","optional":false},{"reason":"Required for integration with Scikit-Learn API; scikeras wraps Keras models to be compatible with sklearn.","package":"scikit-learn","optional":false},{"reason":"Common backend for Keras. Can be installed as 'keras[tensorflow]'.","package":"tensorflow","optional":true},{"reason":"Alternative backend for Keras. Can be installed as 'keras[torch]'.","package":"torch","optional":true}],"imports":[{"note":"The `keras.wrappers.scikit_learn` module is deprecated in Keras and should not be used. Scikeras provides its own, enhanced wrappers.","wrong":"from keras.wrappers.scikit_learn import KerasClassifier","symbol":"KerasClassifier","correct":"from scikeras.wrappers import KerasClassifier"},{"note":"Similarly, direct imports from `tensorflow.keras.wrappers` are for older TensorFlow versions and do not expose Scikeras's features or Keras 3 compatibility.","wrong":"from tensorflow.keras.wrappers.scikit_learn import KerasRegressor","symbol":"KerasRegressor","correct":"from scikeras.wrappers import KerasRegressor"}],"quickstart":{"code":"import numpy as np\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom scikeras.wrappers import KerasClassifier\n\n# 1. Define a Keras model creation function\ndef build_classifier_model(meta):\n    # meta contains useful information like n_features_in_, n_outputs_\n    model = Sequential([\n        Dense(10, activation=\"relu\", input_shape=(meta[\"n_features_in_\"],)),\n        Dense(meta[\"n_outputs_\"], activation=\"softmax\")\n    ])\n    model.compile(optimizer=\"adam\", loss=\"sparse_categorical_crossentropy\", metrics=[\"accuracy\"])\n    return model\n\n# 2. Generate some dummy data\nX = np.random.rand(100, 10).astype(np.float32)\ny = np.random.randint(0, 3, 100).astype(np.int32) # 3 classes\n\n# 3. Create a KerasClassifier instance\nkeras_clf = KerasClassifier(\n    model=build_classifier_model,\n    epochs=10,\n    batch_size=32,\n    verbose=0 # Suppress verbose output for quickstart\n)\n\n# 4. Train the model using the Scikit-Learn API\nkeras_clf.fit(X, y)\n\n# 5. Make predictions\npredictions = keras_clf.predict(X[:5])\nprint(f\"Predictions for first 5 samples: {predictions}\")\n\n# You can also evaluate using the Scikit-Learn .score() method\nscore = keras_clf.score(X, y)\nprint(f\"Model accuracy: {score:.4f}\")","lang":"python","description":"This quickstart demonstrates how to wrap a Keras model with `KerasClassifier` for use with Scikit-Learn's API. It shows model definition, data generation, training with `.fit()`, and prediction with `.predict()`."},"warnings":[{"fix":"Ensure your environment has Keras >= 3.0.0, TensorFlow >= 2.15.0 (if using TF backend), Scikit-Learn >= 1.0, and Python >= 3.9. Upgrade dependencies: `pip install --upgrade scikeras keras scikit-learn`.","message":"Scikeras v0.13.0 drops support for Keras 2.x, TensorFlow < 2.15.0, and older Scikit-Learn versions. It requires Keras >= 3.0.0 and Python >= 3.9.","severity":"breaking","affected_versions":">=0.13.0"},{"fix":"Upgrade your Python environment to 3.9 or later. If you must use Python 3.7, you need to pin scikeras to a version < 0.11.0, e.g., `pip install scikeras<0.11.0`.","message":"Scikeras v0.11.0 dropped support for Python 3.7. Later versions require Python 3.9 or newer.","severity":"breaking","affected_versions":">=0.11.0"},{"fix":"Pass a function reference, e.g., `KerasClassifier(model=build_classifier_model, ...)` instead of `KerasClassifier(model=build_classifier_model(), ...)`.","message":"Scikeras expects the `model` argument to be a callable (function) that returns a compiled Keras model, not an already instantiated `tf.keras.Model` object.","severity":"gotcha","affected_versions":"All"},{"fix":"Convert `tf.data.Dataset` objects to NumPy arrays or iterate through them to collect data before passing to scikeras wrappers.","message":"TensorFlow Datasets (`tf.data.Dataset`) are not directly supported as inputs (X, y) for `fit()`, `predict()`, or `score()` methods. Inputs must be NumPy arrays or similar array-like structures.","severity":"gotcha","affected_versions":"All (documented since 0.6.1)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Update your import statement to `from scikeras.wrappers import KerasClassifier` (or `KerasRegressor`).","cause":"You are attempting to import from the old, deprecated Keras wrapper module which is not part of the `scikeras` library.","error":"ModuleNotFoundError: No module named 'keras.wrappers.scikit_learn'"},{"fix":"Replace `build_fn=create_model` with `model=create_model` when instantiating `KerasClassifier` or `KerasRegressor`.","cause":"You are using `build_fn` as an argument, which was used in the old `tf.keras.wrappers.scikit_learn` API. Scikeras uses `model`.","error":"ValueError: Unknown model argument: 'build_fn'"},{"fix":"Ensure Keras 3 is installed: `pip install --upgrade keras`. If you need to use Keras 2, you must downgrade scikeras to a compatible version (e.g., `pip install 'scikeras<0.13.0'`).","cause":"This usually happens when `scikeras` >= 0.13.0 is installed, but an older version of Keras (e.g., Keras 2.x) is present in the environment, causing a compatibility mismatch. Scikeras v0.13.0+ requires Keras 3.","error":"ImportError: cannot import name 'KerasClassifier' from 'scikeras.wrappers' (...)"},{"fix":"If `learn_rate` is a Keras optimizer argument (e.g., for Adam), pass it as `optimizer__learning_rate=0.01` to `KerasClassifier` or ensure it's handled within your `build_model` function.","cause":"When passing Keras optimizer arguments, they should be defined as keyword arguments in the `model` function or passed directly to the `KerasClassifier`/`KerasRegressor` constructor if they are generic optimizer arguments (like `optimizer__learning_rate`).","error":"TypeError: __init__() got an unexpected keyword argument 'learn_rate'"}]}