File size: 7,884 Bytes
d2a8669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import numpy as np
from sklearn.base import BaseEstimator, MetaEstimatorMixin, clone
from sklearn.utils.metaestimators import if_delegate_has_method
from sklearn.utils.validation import has_fit_parameter

from aif360.sklearn.utils import check_inputs, check_groups


class Reweighing(BaseEstimator):
    """Sample reweighing.

    Reweighing is a preprocessing technique that weights the examples in each
    (group, label) combination differently to ensure fairness before
    classification [#kamiran12]_.

    Note:
        This breaks the scikit-learn API by returning new sample weights from
        ``fit_transform()``. See :class:`ReweighingMeta` for a workaround.

    See also:
        :class:`ReweighingMeta`

    References:
        .. [#kamiran12] `F. Kamiran and T. Calders,  "Data Preprocessing
           Techniques for Classification without Discrimination," Knowledge and
           Information Systems, 2012.
           <https://link.springer.com/article/10.1007/s10115-011-0463-8>`_

    Attributes:
        prot_attr_ (str or list(str)): Protected attribute(s) used for
            reweighing.
        groups_ (array, shape (n_groups,)): A list of group labels known to the
            transformer.
        classes_ (array, shape (n_classes,)): A list of class labels known to
            the transformer.
        reweigh_factors_ (array, shape (n_groups, n_labels)): Reweighing factors
            for each combination of group and class labels used to debias
            samples. Existing sample weights are multiplied by the corresponding
            factor for that sample's group and class.
    """

    def __init__(self, prot_attr=None):
        """
        Args:
            prot_attr (single label or list-like, optional): Protected
                attribute(s) to use in the reweighing process. If more than one
                attribute, all combinations of values (intersections) are
                considered. Default is ``None`` meaning all protected attributes
                from the dataset are used.
        """
        self.prot_attr = prot_attr

    def fit(self, X, y, sample_weight=None):
        """Only :meth:`fit_transform` is allowed for this algorithm."""
        self.fit_transform(X, y, sample_weight=sample_weight)
        return self

    def fit_transform(self, X, y, sample_weight=None):
        """Compute the factors for reweighing the dataset and transform the
        sample weights.

        Args:
            X (pandas.DataFrame): Training samples.
            y (array-like): Training labels.
            sample_weight (array-like, optional): Sample weights.

        Returns:
            tuple:
                Samples and their weights.

                * **X** -- Unchanged samples.
                * **sample_weight** -- Transformed sample weights.
        """
        X, y, sample_weight = check_inputs(X, y, sample_weight)

        sample_weight_t = np.empty_like(sample_weight)
        groups, self.prot_attr_ = check_groups(X, self.prot_attr)
        # TODO: maintain categorical ordering
        self.groups_ = np.unique(groups)
        self.classes_ = np.unique(y)
        n_groups = len(self.groups_)
        n_classes = len(self.classes_)
        self.reweigh_factors_ = np.full((n_groups, n_classes), np.nan)

        def N_(i): return sample_weight[i].sum()
        N = sample_weight.sum()
        for i, g in enumerate(self.groups_):
            for j, c in enumerate(self.classes_):
                g_and_c = (groups == g) & (y == c)
                if np.any(g_and_c):
                    W_gc = N_(groups == g) * N_(y == c) / (N * N_(g_and_c))
                    sample_weight_t[g_and_c] = W_gc * sample_weight[g_and_c]
                    self.reweigh_factors_[i, j] = W_gc
        return X, sample_weight_t


class ReweighingMeta(BaseEstimator, MetaEstimatorMixin):
    """A meta-estimator which wraps a given estimator with a reweighing
    preprocessing step.

    This is necessary for use in a Pipeline, etc.

    Attributes:
        estimator_ (sklearn.BaseEstimator): The fitted underlying estimator.
        reweigher_: The fitted underlying reweigher.
        classes_ (array, shape (n_classes,)): Class labels from `estimator_`.
    """
    def __init__(self, estimator, reweigher=None):
        """
        Args:
            estimator (sklearn.BaseEstimator): Estimator to be wrapped.
            reweigher (optional): Preprocessor which returns new sample weights
                from ``transform()``. If ``None``, defaults to
                :class:`~aif360.sklearn.preprocessing.Reweighing`.
        """
        self.reweigher = reweigher
        self.estimator = estimator

    @property
    def _estimator_type(self):
        return self.estimator._estimator_type

    @property
    def classes_(self):
        """Class labels from the base estimator."""
        return self.estimator_.classes_

    def fit(self, X, y, sample_weight=None):
        """Performs ``self.reweigher_.fit_transform(X, y, sample_weight)`` and
        then ``self.estimator_.fit(X, y, sample_weight)`` using the reweighed
        samples.

        Args:
            X (pandas.DataFrame): Training samples.
            y (array-like): Training labels.
            sample_weight (array-like, optional): Sample weights.

        Returns:
            self
        """
        if not has_fit_parameter(self.estimator, 'sample_weight'):
            raise TypeError("`estimator` (type: {}) does not have fit parameter"
                            " `sample_weight`.".format(type(self.estimator)))

        if self.reweigher is None:
            self.reweigher_ = Reweighing()
        else:
            self.reweigher_ = clone(self.reweigher)
        self.estimator_ = clone(self.estimator)

        X, sample_weight = self.reweigher_.fit_transform(X, y,
                sample_weight=sample_weight)
        self.estimator_.fit(X, y, sample_weight=sample_weight)
        return self

    @if_delegate_has_method('estimator_')
    def predict(self, X):
        """Predict class labels for the given samples using ``self.estimator_``.

        Args:
            X (array-like): Test samples.

        Returns:
            array: Predicted class label per sample.
        """
        return self.estimator_.predict(X)

    @if_delegate_has_method('estimator_')
    def predict_proba(self, X):
        """Probability estimates from ``self.estimator_``.

        The returned estimates for all classes are ordered by the label of
        classes.

        Args:
            X (array-like): Test samples.

        Returns:
            array: Returns the probability of the sample for each class in the
            model, where classes are ordered as they are in ``self.classes_``.
        """
        return self.estimator_.predict_proba(X)

    @if_delegate_has_method('estimator_')
    def predict_log_proba(self, X):
        """Log of probability estimates from ``self.estimator_``.

        The returned estimates for all classes are ordered by the label of
        classes.

        Args:
            X (array-like): Test samples.

        Returns:
            array: Returns the log-probability of the sample for each class in
            the model, where classes are ordered as they are in
            ``self.classes_``.
        """
        return self.estimator_.predict_log_proba(X)

    @if_delegate_has_method('estimator_')
    def score(self, X, y, sample_weight=None):
        """Returns the output of the estimator's score function on the given
        test data and labels.

        Args:
            X (array-like): Test samples.
            y (array-like): True labels for X.
            sample_weight (array-like, optional): Sample weights.

        Returns:
            float: `self.estimator.score(X, y, sample_weight)`
        """
        return self.estimator_.score(X, y, sample_weight=sample_weight)