Install & Compatibility
Where this runs
tested against v0.13.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.940 runs
build_error
glibcpy 3.10–3.940 runs
installs and imports cleanly · install 12.5s · import 0.000s · 340MB
354MB installed
● package 354MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
KerasClassifier
✓ from scikeras.wrappers import KerasClassifier
✗ from keras.wrappers.scikit_learn import KerasClassifier
The `keras.wrappers.scikit_learn` module is deprecated in Keras and should not be used. Scikeras provides its own, enhanced wrappers.
KerasRegressor
✓ from scikeras.wrappers import KerasRegressor
✗ from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
Similarly, direct imports from `tensorflow.keras.wrappers` are for older TensorFlow versions and do not expose Scikeras's features or Keras 3 compatibility.
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()`.
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from scikeras.wrappers import KerasClassifier
# 1. Define a Keras model creation function
def build_classifier_model(meta):
# meta contains useful information like n_features_in_, n_outputs_
model = Sequential([
Dense(10, activation="relu", input_shape=(meta["n_features_in_"],)),
Dense(meta["n_outputs_"], activation="softmax")
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
return model
# 2. Generate some dummy data
X = np.random.rand(100, 10).astype(np.float32)
y = np.random.randint(0, 3, 100).astype(np.int32) # 3 classes
# 3. Create a KerasClassifier instance
keras_clf = KerasClassifier(
model=build_classifier_model,
epochs=10,
batch_size=32,
verbose=0 # Suppress verbose output for quickstart
)
# 4. Train the model using the Scikit-Learn API
keras_clf.fit(X, y)
# 5. Make predictions
predictions = keras_clf.predict(X[:5])
print(f"Predictions for first 5 samples: {predictions}")
# You can also evaluate using the Scikit-Learn .score() method
score = keras_clf.score(X, y)
print(f"Model accuracy: {score:.4f}")
Debug
Known issues
breakingScikeras 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.fixEnsure 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`.
affects: >=0.13.0
breakingScikeras v0.11.0 dropped support for Python 3.7. Later versions require Python 3.9 or newer.fixUpgrade 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`.
affects: >=0.11.0
gotchaScikeras expects the `model` argument to be a callable (function) that returns a compiled Keras model, not an already instantiated `tf.keras.Model` object.fixPass a function reference, e.g., `KerasClassifier(model=build_classifier_model, ...)` instead of `KerasClassifier(model=build_classifier_model(), ...)`.
affects: All
gotchaTensorFlow 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.fixConvert `tf.data.Dataset` objects to NumPy arrays or iterate through them to collect data before passing to scikeras wrappers.
affects: All (documented since 0.6.1)
Upgrade
Version history
0.13.0latest on PyPI · released Apr 11, 2024
Audit
Dependencies
kerasrequiredScikeras is a wrapper for Keras models. Keras >= 3.0.0 is required for scikeras >= 0.13.0.
scikit-learnrequiredRequired for integration with Scikit-Learn API; scikeras wraps Keras models to be compatible with sklearn.
tensorflowoptionalCommon backend for Keras. Can be installed as 'keras[tensorflow]'.
torchoptionalAlternative backend for Keras. Can be installed as 'keras[torch]'.