Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions examples/miscellaneous/plot_metadata_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
from sklearn.utils.validation import check_is_fitted

n_samples, n_features = 100, 4
rng = np.random.RandomState(42)
X = rng.rand(n_samples, n_features)
y = rng.randint(0, 2, size=n_samples)
my_groups = rng.randint(0, 10, size=n_samples)
my_weights = rng.rand(n_samples)
my_other_weights = rng.rand(n_samples)
rng = np.random.default_rng(42)
X = rng.random((n_samples, n_features))
y = rng.integers(0, 2, size=n_samples)
my_groups = rng.integers(0, 10, size=n_samples)
my_weights = rng.random(n_samples)
my_other_weights = rng.random(n_samples)

# %%
# Metadata routing is only available if explicitly enabled:
Expand Down Expand Up @@ -97,6 +97,7 @@ def print_routing(obj):

class ExampleClassifier(ClassifierMixin, BaseEstimator):
def fit(self, X, y, sample_weight=None):
# X and y are required by the scikit-learn API but not used in this example.
check_metadata(self, sample_weight=sample_weight)
# all classifiers need to expose a classes_ attribute once they're fit.
self.classes_ = np.array([0, 1])
Expand Down Expand Up @@ -498,23 +499,23 @@ def fit(self, X, y, **fit_params):
self.transformer_ = clone(self.transformer).fit(
X, y, **routed_params.transformer.fit
)
X_transformed = self.transformer_.transform(
x_transformed = self.transformer_.transform(
X, **routed_params.transformer.transform
)

self.classifier_ = clone(self.classifier).fit(
X_transformed, y, **routed_params.classifier.fit
x_transformed, y, **routed_params.classifier.fit
)
return self

def predict(self, X, **predict_params):
routed_params = process_routing(self, "predict", **predict_params)

X_transformed = self.transformer_.transform(
x_transformed = self.transformer_.transform(
X, **routed_params.transformer.transform
)
return self.classifier_.predict(
X_transformed, **routed_params.classifier.predict
x_transformed, **routed_params.classifier.predict
)


Expand All @@ -539,6 +540,7 @@ def predict(self, X, **predict_params):

class ExampleTransformer(TransformerMixin, BaseEstimator):
def fit(self, X, y, sample_weight=None):
"""Fit the transformer on X and y with optional sample_weight."""
check_metadata(self, sample_weight=sample_weight)
return self

Expand Down Expand Up @@ -623,7 +625,7 @@ def get_metadata_routing(self):
class WeightedMetaRegressor(MetaEstimatorMixin, RegressorMixin, BaseEstimator):
# show warning to remind user to explicitly set the value with
# `.set_{method}_request(sample_weight={boolean})`
__metadata_request__fit = {"sample_weight": metadata_routing.WARN}
_metadata_request__fit = {"sample_weight": metadata_routing.WARN}

def __init__(self, estimator):
self.estimator = estimator
Expand Down Expand Up @@ -670,7 +672,11 @@ def __init__(self):
self.set_fit_request(sample_weight=metadata_routing.WARN)

def fit(self, X, y, sample_weight=None):
"""Fit the regressor using X, y and sample_weight as metadata."""
check_metadata(self, sample_weight=sample_weight)
# X and y are used by convention in the scikit-learn estimator API
self.X_ = X
self.y_ = y
return self

def predict(self, X):
Expand Down
Loading