Skip to content

Commit c86f619

Browse files
authored
ACCEPT: SLEP006 - Metadata Routing (#65)
* accept SLEP006 * props -> metadata * Andy's comments * add type of the metadata to the info * method_requests -> set_method_request
1 parent 939dc55 commit c86f619

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
:caption: Accepted
1111

1212
slep000/proposal
13+
slep006/proposal
1314
slep007/proposal
1415
slep009/proposal
1516
slep010/proposal
@@ -18,7 +19,6 @@
1819
:maxdepth: 1
1920
:caption: Under review
2021

21-
slep006/proposal
2222
slep012/proposal
2323
slep013/proposal
2424

slep006/proposal.rst

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ SLEP006: Metadata Routing
55
=========================
66

77
:Author: Joel Nothman, Adrin Jalali, Alex Gramfort, Thomas J. Fan
8-
:Status: Under Review
8+
:Status: Accepted
99
:Type: Standards Track
1010
:Created: 2019-03-07
1111

@@ -56,18 +56,25 @@ This SLEP proposes to add
5656

5757
* `get_metadata_routing` to all **consumers** and **routers**
5858
(i.e. all estimators, scorers, and splitters supporting this API)
59-
* `*_requests` to consumers (including estimators, scorers, and CV splitters),
60-
where `*` is a method that requires metadata. (e.g. `fit_requests`,
61-
`score_requests`, `transform_requests`, etc.)
59+
* `set_*_request` to consumers (including estimators, scorers, and CV
60+
splitters), where `*` is a method that requires metadata. (e.g.
61+
`set_fit_request`, `set_score_request`, `set_transform_request`, etc.)
6262

63-
For example, `fit_requests` configures an estimator to request metadata::
63+
For example, `set_fit_request` configures an estimator to request metadata::
6464

65-
>>> log_reg = LogisticRegression().fit_requests(sample_weight=True)
65+
>>> log_reg = LogisticRegression().set_fit_request(sample_weight=True)
6666

6767
`get_metadata_routing` are used by **routers** to inspect the metadata needed
6868
by **consumers**. `get_metadata_routing` returns a `MetadataRouter` or a
69-
`MetadataRequest` object that stores and handles metadata routing. See the
70-
draft implementation for more implementation details.
69+
`MetadataRequest` object that stores and handles metadata routing.
70+
`get_metadata_routing` returns enough information for a router to know what
71+
metadata is requested, and whether the metadata is sample aligned or not. See
72+
the draft implementation for more implementation details.
73+
74+
Note that in the core library nothing is requested by default, except
75+
``groups`` in ``Group*CV`` objects which request the ``groups`` metadata. At
76+
the time of writing this proposal, all metadata requested in the core library
77+
are sample aligned.
7178

7279
Detailed description
7380
--------------------
@@ -85,11 +92,11 @@ requests `groups` by default::
8592

8693
>>> weighted_acc = make_scorer(accuracy_score).score_request(sample_weight=True)
8794
>>> log_reg = (LogisticRegressionCV(cv=GroupKFold(), scoring=weighted_acc)
88-
... .fit_requests(sample_weight=True))
95+
... .set_fit_request(sample_weight=True))
8996
>>> cv_results = cross_validate(
9097
... log_reg, X, y,
9198
... cv=GroupKFold(),
92-
... props={"sample_weight": my_weights, "groups": my_groups},
99+
... metadata={"sample_weight": my_weights, "groups": my_groups},
93100
... scoring=weighted_acc)
94101

95102
To support unweighted fitting and weighted scoring, metadata is set to `False`
@@ -100,7 +107,7 @@ in `fit_request`::
100107
>>> cross_validate(
101108
... log_reg, X, y,
102109
... cv=GroupKFold(),
103-
... props={'sample_weight': weights, 'groups': groups},
110+
... metadata={'sample_weight': weights, 'groups': groups},
104111
... scoring=weighted_acc)
105112

106113
Unweighted Feature selection
@@ -110,7 +117,7 @@ Unweighted Feature selection
110117
will _not_ be routed weights::
111118

112119
>>> log_reg = (LogisticRegressionCV(cv=GroupKFold(), scoring=weighted_acc)
113-
... .fit_requests(sample_weight=True))
120+
... .set_fit_request(sample_weight=True))
114121
>>> sel = SelectKBest(k=2)
115122
>>> pipe = make_pipeline(sel, log_reg)
116123
>>> pipe.fit(X, y, sample_weight=weights, groups=groups)
@@ -128,13 +135,13 @@ this example, `scoring_weight` is passed to the scoring and `fitting_weight`
128135
is passed to `LogisticRegressionCV`::
129136

130137
>>> weighted_acc = (make_scorer(accuracy_score)
131-
... .score_requests(sample_weight="scoring_weight"))
138+
... .set_score_request(sample_weight="scoring_weight"))
132139
>>> log_reg = (LogisticRegressionCV(cv=GroupKFold(), scoring=weighted_acc)
133-
... .fit_requests(sample_weight="fitting_weight"))
140+
... .set_fit_request(sample_weight="fitting_weight"))
134141
>>> cv_results = cross_validate(
135142
... log_reg, X, y,
136143
... cv=GroupKFold(),
137-
... props={"scoring_weight": my_weights,
144+
... metadata={"scoring_weight": my_weights,
138145
... "fitting_weight": my_other_weights,
139146
... "groups": my_groups},
140147
... scoring=weighted_acc)
@@ -155,7 +162,7 @@ and the inner random search::
155162
>>> cv_results = cross_validate(
156163
... log_reg, X, y,
157164
... cv=GroupKFold(),
158-
... props={"groups": my_groups})
165+
... metadata={"groups": my_groups})
159166

160167
Implementation
161168
--------------
@@ -164,7 +171,7 @@ This SLEP has a draft implementation at :pr:`22083` by :user:`adrinjalali`. The
164171
implementation provides developer utilities that are used by scikit-learn and
165172
available to third-party estimators for adopting this SLEP. Specifically, the
166173
draft implementation makes it easier to define `get_metadata_routing` and
167-
`*_requests` for **consumers** and **routers**.
174+
`set_*_request` for **consumers** and **routers**.
168175

169176
Backward compatibility
170177
----------------------
@@ -184,7 +191,9 @@ a deprecation warning is raised::
184191
To avoid the warning, one would need to specify the request in
185192
`LogisticRegression`::
186193

187-
>>> grid = GridSearchCV(LogisticRegression().fit_requests(sample_weight=True), ...)
194+
>>> grid = GridSearchCV(
195+
... LogisticRegression().set_fit_request(sample_weight=True), ...
196+
... )
188197
>>> grid.fit(X, y, sample_weight=sw)
189198

190199
Meta-estimators such as `GridSearchCV` will check which metadata is requested,
@@ -200,23 +209,26 @@ not configured to request it::
200209
>>> # `grid.fit`.
201210
>>> grid.fit(X, y, sample_weight=sw)
202211

203-
To avoid the error, `LogisticRegression` must specify its metadata request by calling
204-
`fit_requests`::
212+
To avoid the error, `LogisticRegression` must specify its metadata request by
213+
calling `set_fit_request`::
205214

206215
>>> # Request sample weights
207-
>>> log_reg_weights = LogisticRegression().fit_requests(sample_weight=True)
216+
>>> log_reg_weights = LogisticRegression().set_fit_request(sample_weight=True)
208217
>>> grid = GridSearchCV(log_reg_with_weights, ...)
209218
>>> grid.fit(X, y, sample_weight=sw)
210219
>>>
211220
>>> # Do not request sample_weights
212-
>>> log_reg_no_weights = LogisticRegression().fit_requests(sample_weight=False)
221+
>>> log_reg_no_weights = LogisticRegression().set_fit_request(sample_weight=False)
213222
>>> grid = GridSearchCV(log_reg_no_weights, ...)
214223
>>> grid.fit(X, y, sample_weight=sw)
215224

216-
Third-party estimators will need to adopt this SLEP in order to support metadata
217-
routing, while the dunder syntax is deprecated. Our implementation will provide
218-
developer APIs to trigger warnings and errors as described above to help with
219-
adopting this SLEP.
225+
Note that a meta-estimator will raise an error if the user passes a metadata
226+
which is not requested by any of the child objects of the meta-estimator.
227+
228+
Third-party estimators will need to adopt this SLEP in order to support
229+
metadata routing, while the dunder syntax is deprecated. Our implementation
230+
will provide developer APIs to trigger warnings and errors as described above
231+
to help with adopting this SLEP.
220232

221233
Alternatives
222234
------------

0 commit comments

Comments
 (0)