explanationspace.EqualTreatment¶
- class explanationspace.EqualTreatment(model, gmodel, algorithm: str = 'auto', masker: bool = False, data_masker: DataFrame | None = None, verbose=False)¶
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()