Overview

Scikit-learn 1.3, released on June 9, 2023, stabilizes metadata routing and TargetEncoder.

Main Features

Metadata routing

Metadata routing allows passing extra information (weights, groups) through pipelines and cross-validation methods.

python
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
import numpy as np

X = np.random.randn(100, 5)
y = np.random.randint(0, 2, 100)
weights = np.random.rand(100)

# Routing passes weights to the model
clf = LogisticRegression()
clf.set_fit_request(sample_weight=True)
scores = cross_val_score(clf, X, y,
    params={'sample_weight': weights})

Stable TargetEncoder

TargetEncoder encodes categorical features based on the target variable, avoiding overfitting thanks to built-in regularization.

python
from sklearn.preprocessing import TargetEncoder
import numpy as np

X = np.array([['cat'], ['dog'], ['cat'],
              ['bird'], ['dog'], ['cat']])
y = np.array([0.5, 1.0, 0.3, 0.8, 0.9, 0.4])

enc = TargetEncoder()
X_enc = enc.fit_transform(X, y)
print(X_enc)  # values encoded by target mean

Sources