Scikit-Learn API wrapper for Keras models enabling seamless integration of deep learning into scikit-learn workflows.
—
Transformer classes for preprocessing targets and features to ensure compatibility between scikit-learn and Keras data formats. These transformers handle automatic data preprocessing based on target types and provide seamless integration with scikit-learn pipelines.
Converts 1D targets to 2D format and back for compatibility with transformers that require 2D inputs, such as OneHotEncoder and OrdinalEncoder.
class TargetReshaper(BaseEstimator, TransformerMixin):
def __init__(self):
"""Initialize TargetReshaper."""
def fit(self, y):
"""
Fit the transformer to target array.
Args:
y: array-like - Target array to learn shape from
Returns:
self: Fitted transformer
"""
def transform(self, y):
"""
Transform 1D targets to 2D format.
Args:
y: array-like - Target array to transform
Returns:
array-like: Reshaped target array
"""
def inverse_transform(self, y):
"""
Transform 2D targets back to original dimensionality.
Args:
y: array-like - 2D target array to reshape back
Returns:
array-like: Target array in original shape
"""
@property
def ndim_(self):
"""int: Original dimensions of fitted target array."""Default target transformer for KerasClassifier that handles label encoding and one-hot encoding for classification targets with support for different target types.
class ClassifierLabelEncoder(BaseEstimator, TransformerMixin):
def __init__(self, loss=None):
"""
Initialize ClassifierLabelEncoder.
Args:
loss: Loss function to determine encoding strategy
"""
def fit(self, y):
"""
Fit encoder to label array.
Args:
y: array-like of shape (n_samples,) - Target class labels
Returns:
self: Fitted encoder
"""
def transform(self, y):
"""
Transform labels to encoded format.
Args:
y: array-like of shape (n_samples,) - Target labels to encode
Returns:
array-like: Encoded target labels suitable for Keras training
"""
def inverse_transform(self, y_transformed, return_proba=False):
"""
Transform encoded labels back to original format.
Args:
y_transformed: array-like - Encoded labels or probabilities
return_proba: bool - Whether to return probabilities or class predictions
Returns:
array-like: Original label format or probabilities
"""
def get_metadata(self):
"""
Get metadata about label encoding.
Returns:
dict: Metadata including classes, encoding type, etc.
"""Default target transformer for KerasRegressor that handles target preprocessing for regression tasks including reshaping and validation.
class RegressorTargetEncoder(BaseEstimator, TransformerMixin):
def __init__(self):
"""Initialize RegressorTargetEncoder."""
def fit(self, y):
"""
Fit encoder to target array.
Args:
y: array-like - Regression target values
Returns:
self: Fitted encoder
"""
def transform(self, y):
"""
Transform regression targets for Keras compatibility.
Args:
y: array-like - Target values to transform
Returns:
array-like: Transformed targets suitable for Keras
"""
def inverse_transform(self, y):
"""
Transform targets back to original format.
Args:
y: array-like - Transformed target values
Returns:
array-like: Original target format
"""
def get_metadata(self):
"""
Get metadata about target encoding.
Returns:
dict: Metadata including target type, shape, etc.
"""from scikeras.utils.transformers import TargetReshaper
import numpy as np
# Create 1D target array
y_1d = np.array([0, 1, 0, 1, 1])
# Initialize and fit reshaper
reshaper = TargetReshaper()
reshaper.fit(y_1d)
# Transform to 2D for compatibility with sklearn transformers
y_2d = reshaper.transform(y_1d)
print(f"Original shape: {y_1d.shape}") # (5,)
print(f"Reshaped: {y_2d.shape}") # (5, 1)
# Transform back to original shape
y_back = reshaper.inverse_transform(y_2d)
print(f"Back to original: {y_back.shape}") # (5,)from scikeras.utils.transformers import ClassifierLabelEncoder
from sklearn.datasets import make_classification
import numpy as np
# Create multiclass classification data
X, y = make_classification(n_samples=100, n_classes=3, n_features=10,
n_informative=5, random_state=42)
# Use string labels
y_str = np.array(['class_a', 'class_b', 'class_c'])[y]
# Initialize and fit encoder
encoder = ClassifierLabelEncoder()
encoder.fit(y_str)
# Transform for Keras training
y_encoded = encoder.transform(y_str)
print(f"Original labels: {y_str[:5]}")
print(f"Encoded shape: {y_encoded.shape}")
# Get encoding metadata
metadata = encoder.get_metadata()
print(f"Classes: {metadata.get('classes', 'Not available')}")from scikeras.utils.transformers import TargetReshaper
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
import numpy as np
# Create categorical target data
y_categorical = np.array(['A', 'B', 'A', 'C', 'B'])
# Create pipeline with TargetReshaper for OneHotEncoder compatibility
target_pipeline = Pipeline([
('reshape', TargetReshaper()),
('onehot', OneHotEncoder(sparse_output=False))
])
# Fit and transform
y_processed = target_pipeline.fit_transform(y_categorical)
print(f"Original: {y_categorical}")
print(f"One-hot encoded shape: {y_processed.shape}")
print(f"One-hot encoded:\\n{y_processed}")from scikeras.utils.transformers import ClassifierLabelEncoder
from scikeras.wrappers import KerasClassifier
import keras
import numpy as np
# Create imbalanced multiclass data
y_imbalanced = np.random.choice(['rare', 'common', 'medium'],
size=1000, p=[0.1, 0.7, 0.2])
def create_classifier():
model = keras.Sequential([
keras.layers.Dense(50, activation='relu', input_dim=10),
keras.layers.Dense(3, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
return model
# The classifier automatically uses ClassifierLabelEncoder
clf = KerasClassifier(model=create_classifier, epochs=10)
# Generate dummy features
X = np.random.random((1000, 10))
# Fit - encoder handles label preprocessing automatically
clf.fit(X, y_imbalanced)
# Predictions return original label format
predictions = clf.predict(X[:5])
probabilities = clf.predict_proba(X[:5])
print(f"Original labels: {y_imbalanced[:5]}")
print(f"Predictions: {predictions}")
print(f"Probability shape: {probabilities.shape}")from scikeras.utils.transformers import RegressorTargetEncoder
from scikeras.wrappers import KerasRegressor
import numpy as np
import keras
# Create multi-output regression data
n_samples, n_outputs = 100, 3
y_multi = np.random.random((n_samples, n_outputs))
def create_regressor():
model = keras.Sequential([
keras.layers.Dense(50, activation='relu', input_dim=5),
keras.layers.Dense(n_outputs)
])
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
return model
# The regressor automatically uses RegressorTargetEncoder
reg = KerasRegressor(model=create_regressor, epochs=10)
# Generate dummy features
X = np.random.random((n_samples, 5))
# Fit - encoder handles target preprocessing automatically
reg.fit(X, y_multi)
# Predictions maintain original target format
predictions = reg.predict(X[:5])
print(f"Target shape: {y_multi.shape}")
print(f"Prediction shape: {predictions.shape}")from scikeras.utils.transformers import TargetReshaper
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
import numpy as np
# Create custom target preprocessing pipeline
def create_target_pipeline():
return Pipeline([
('reshape', TargetReshaper()),
('scale', StandardScaler())
])
# Use with regression data
y_regression = np.random.randn(100) * 100 + 50 # Mean=50, std=100
pipeline = create_target_pipeline()
y_processed = pipeline.fit_transform(y_regression)
print(f"Original stats: mean={y_regression.mean():.2f}, std={y_regression.std():.2f}")
print(f"Processed stats: mean={y_processed.mean():.2f}, std={y_processed.std():.2f}")
# Inverse transform back to original scale
y_back = pipeline.inverse_transform(y_processed)
print(f"Recovered stats: mean={y_back.mean():.2f}, std={y_back.std():.2f}")# Target types supported by transformers
TargetType = Union[np.ndarray, List, Tuple]
# Metadata structure returned by get_metadata()
TransformerMetadata = Dict[str, Any]
# Encoding options for ClassifierLabelEncoder
EncodingType = Literal['ordinal', 'onehot', 'binary']Install with Tessl CLI
npx tessl i tessl/pypi-scikeras