Repository navigation
[ENH] New MetricFrame - #975
riedgar-ms wants to merge 15 commits into
Conversation
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
|
Other than the v2 name (which I assume you're planning to replace) this looks fine to me. I think this is also related to @bram49's concern about metrics that don't need one of |
|
@romanlutz it was @bram49 's ask to discuss that in today's community call that prompted me to get going. |
| import pandas as pd | ||
|
|
||
|
|
||
| class MetricFunctionRequest: |
There was a problem hiding this comment.
@adrinjalali this is my pass at the 'add attributes for your requests' thing you mentioned is starting to happen in sklearn. I'm sure there are plenty of improvements.
There was a problem hiding this comment.
Instead of this, we could have something like
class ConsumerMetric:
def __init__(self, metric, *, metadata=None):
self.metric = metric
self.metadata = set(metadata)
def __call__(self, y_true, y_pred, **kwargs):
if set(kwargs.keys()) != self.metadata:
raise TypeError("Passed metadata don't match requested ones!")
return self.metric(y_true, y_pred, **kwargs)
def get_metadata_request(self):
return self.metadataThen we could use the old MetricFrame, and all is as before, except that the user can pass something like ConsumerMetric(metric_func, metadata=["sample_weight"]), and the MetricFrame checks if a given metric has get_metadata_request or not, if not, then it only accepts y_true and y_pred, otherwise it accepts extra metadata. This would simplify the sample_params part of the MetricFrame quite a bit.
If you really want to handle not passing y_true and y_pred to the metric, you could add that to the same class, but a very much simplified version of what we have now in this PR, and I think you could do with refactoring MetricFrame.
| class MFv2: | ||
| """An alternative for MetricFrame.""" | ||
|
|
||
| def __init__(self, |
There was a problem hiding this comment.
Right now, this is not supporting much flexibility in the arguments. I can see handling the case where the user just passes in a Dict[str,Callable] and we automatically wrap with the defaults of MetricFunctionRequest.
| for sf_2 in sf_2_groups: | ||
| assert target.by_group[m][cf_1][cf_2][sf_1][sf_2] ==\ | ||
| other.by_group[m][cf_1][cf_2][sf_1][sf_2] | ||
| assert delta_new < delta_old, "Check new version is faster" |
There was a problem hiding this comment.
The actual speedup appears to be about 10x, thanks to DataFrame.groupby().apply()
| def __init__(self, | ||
| *, | ||
| func: Callable, | ||
| args: List[str] = ['y_true', 'y_pred'], |
There was a problem hiding this comment.
I don't think we want to have mutable default arguments. See, e.g, https://stackoverflow.com/questions/9158294/good-uses-for-mutable-function-argument-default-values
There was a problem hiding this comment.
Ouch.... I've not dealt with stuff like that since learning to use -stackvar when compiling F77 programs.
| metric_functions: Dict[str, MetricFunctionRequest], | ||
| data: pd.DataFrame, | ||
| sensitive_features: List[str], | ||
| control_features: List[str] = [] |
| return result | ||
|
|
||
|
|
||
| class MFv2: |
There was a problem hiding this comment.
My preference would be to do one of the following:
- have the class MFv2 (suitably renamed) as a private class, and use it internally to implement MetricFrame. This could mean that MetricFrame would be a subclass of MFv2, or possibly a wrapper.
- merge MFv2 with MetricFrame, so that we use MetricFrame's API, but MFv2's implementation.
If we are on board re. 1 or 2, we could kick off a separate conversation how to support a more generic API for MetricFrame variants that: (1) don't need y_true and/or y_pred; (2) or, don't even need specifying metrics (because the metrics are just simple averages of values provided in an array or in a DataFrame).
There was a problem hiding this comment.
I've definitely given some thought to using this to replace the innards of MetricFrame. The work required for handling sample_params wasn't especially appealing at the time, though.
There was a problem hiding this comment.
The MetricFrame API is my greater concern, though. We know that it's confusing people, and they're not realising how flexible MetricFrame really is. So this alternative proposal is about "Give us everything in a DataFrame, tell us which columns are your sensitive features, which are your control features, and give us annotated functions to select the input columns." We then 'do the rest.'
There was a problem hiding this comment.
Re. new API -- do you propose to settle that on this thread :-)... or perhaps schedule a discussion with a subset of folks that might be interested?
There was a problem hiding this comment.
Another issue we have with MetricFrame is that it's just not well documented.
Signed-off-by: Richard Edgar <[email protected]>
|
This is superseded by #985 |
This is to give some concrete code for #756 . It's not a full implementation yet, but is provided to help discussion. People who comment may be deemed to have volunteered for a working group.
The basic idea is that the new-style
MetricFrametakes in all the data as a single DataFrame. Sensitive and control features are specified via column names, and the functions are required to declare the names of the columns they want to process (currently implemented with aMetricFunctionRequestwrapper class - that is subject to change.It does not yet offer all the functionality of
MetricFrame- I want to get a discussion started on this first. Since it usesDataFrame.groupbyinternally, it does appear to be about 10x faster thanMetricFrame.