Fairness Audits: Equal Treatment¶
- class explanationspace.audits.EqualTreatment(model, gmodel, algorithm: str = 'auto', masker: bool = False, data_masker: DataFrame | None = None, verbose=False)¶
Bases:
BaseEstimator
,ClassifierMixin
Given a model, a dataset, and the protected attribute, we want to know if the model violates demographic parity or not and what are the features pushing for it. We do this by computing the shap values of the model, and then train a classifier to distinguish the protected attribute.
Example¶
>>> import pandas as pd >>> import numpy as np >>> from sklearn.linear_model import LogisticRegression >>> from xgboost import XGBRegressor >>> from fairtools.detector import EqualTreatment >>> N = 5_000 >>> x1 = np.random.normal(1, 1, size=N) >>> x2 = np.random.normal(1, 1, size=N) >>> x34 = np.random.multivariate_normal([1, 1], [[1, 0.5], [0.5, 1]], size=N) >>> x3 = x34[:, 0] >>> x4 = x34[:, 1] >>> # Binarize protected attribute >>> x4 = np.where(x4 > np.mean(x4), 1, 0) >>> X = pd.DataFrame([x1, x2, x3, x4]).T >>> X.columns = ["var%d" % (i + 1) for i in range(X.shape[1])] >>> y = (x1 + x2 + x3) / 3 >>> y = 1 / (1 + np.exp(-y)) >>> detector = EqualTreatment(model=XGBRegressor(), gmodel=LogisticRegression()) >>> detector.fit(X, y, Z="var4") >>> detector.get_auc_val()
- fit(X, y, Z)¶
Automatically fits the whole pipeline
Parameters¶
- Xarray-like of shape (n_samples, n_features)
The training input samples.
- yarray-like of shape (n_samples,)
The target values (class labels in classification, real numbers in regression)
- Zarray-like of shape (n_samples,)
The protected attribute values (class labels in classification, real numbers in regression)
- fit_inspector(X, z)¶
Fits the inspector model to the explanations
Parameters¶
- Xarray-like of shape (n_samples, n_features)
The training input samples.
- zarray-like of shape (n_samples,)
The protected attribute values (class labels in classification, real numbers in regression).
- fit_pipeline(X, y, z)¶
Fits the model F to X and y
2. Call fit_inspector to fit the Equal Treatment Inspector Careful this method trains F and G on the same data
Parameters¶
- Xarray-like of shape (n_samples, n_features)
The training input samples.
- yarray-like of shape (n_samples,)
The target values (class labels in classification, real numbers in regression).
- Zarray-like of shape (n_samples,)
The protected attribute values (class labels in classification, real numbers in regression).
- get_explanations(X)¶
Returns the explanations of the model on the data X. Produces a dataframe with the explanations of the model on the data X.
- predict(X)¶
Returns the predictions (ID,OOD) of the detector on the data X.
- predict_proba(X)¶
Returns the soft predictions (ID,OOD) of the detector on the data X.
- set_fit_request(*, Z: bool | None | str = '$UNCHANGED$') EqualTreatment ¶
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.Parameters¶
- Zstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
Z
parameter infit
.
Returns¶
- selfobject
The updated object.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') EqualTreatment ¶
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.Parameters¶
- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
Returns¶
- selfobject
The updated object.