Skip to content

[ENH] New MetricFrame - #975

Closed
riedgar-ms wants to merge 15 commits into
fairlearn:mainfrom
riedgar-ms:riedgar-ms/metrics-new-style
Closed

riedgar-ms wants to merge 15 commits into
fairlearn:mainfrom
riedgar-ms:riedgar-ms/metrics-new-style

Conversation

@riedgar-ms

Copy link
Copy Markdown
Member

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 MetricFrame takes 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 a MetricFunctionRequest wrapper 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 uses DataFrame.groupby internally, it does appear to be about 10x faster than MetricFrame.

Signed-off-by: Richard Edgar <[email protected]>
Signed-off-by: Richard Edgar <[email protected]>
@romanlutz

Copy link
Copy Markdown
Member

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 y_true or y_pred.

@riedgar-ms

Copy link
Copy Markdown
Member Author

@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:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@adrinjalali adrinjalali Nov 9, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.metadata

Then 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,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual speedup appears to be about 10x, thanks to DataFrame.groupby().apply()

Comment thread fairlearn/metrics/_mfv2.py Outdated
def __init__(self,
*,
func: Callable,
args: List[str] = ['y_true', 'y_pred'],

@MiroDudik MiroDudik Oct 21, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch.... I've not dealt with stuff like that since learning to use -stackvar when compiling F77 programs.

Comment thread fairlearn/metrics/_mfv2.py Outdated
metric_functions: Dict[str, MetricFunctionRequest],
data: pd.DataFrame,
sensitive_features: List[str],
control_features: List[str] = []

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutable default argument.

return result


class MFv2:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference would be to do one of the following:

  1. 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.
  2. 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).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another issue we have with MetricFrame is that it's just not well documented.

@riedgar-ms riedgar-ms changed the title [WIP] New MetricFrame [ENH] New MetricFrame Dec 2, 2021
@riedgar-ms riedgar-ms added the API Anything which touches on the API label Dec 2, 2021
@riedgar-ms

Copy link
Copy Markdown
Member Author

This is superseded by #985

@riedgar-ms riedgar-ms closed this Dec 2, 2021
@riedgar-ms
riedgar-ms deleted the riedgar-ms/metrics-new-style branch October 23, 2023 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API Anything which touches on the API

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants