This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from fairlearn.postprocessing import ThresholdOptimizer | |
| reduction = ThresholdOptimizer(estimator = classifier) | |
| reduction.fit(df, target, sensitive_features = gender) | |
| prediction_threshold = reduction.predict(df, sensitive_features = gender) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FairlearnDashboard(y_true = target, | |
| y_pred = {"prediction_original" : prediction, | |
| "prediction_dp": prediction_dp, | |
| "prediction_eo": prediction_eo}, | |
| sensitive_features = gender, | |
| sensitive_feature_names = ["gender"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| eo = EqualizedOdds() | |
| reduction = ExponentiatedGradient(classifier, eo) | |
| reduction.fit(df, target, sensitive_features=gender) | |
| prediction_eo = reduction.predict(df) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from fairlearn.reductions import ExponentiatedGradient, DemographicParity, EqualizedOdds | |
| classifier = DecisionTreeClassifier(min_samples_leaf=10, max_depth=4) | |
| dp = DemographicParity() | |
| reduction = ExponentiatedGradient(classifier, dp) | |
| reduction.fit(df, target, sensitive_features=gender) | |
| prediction_dp = reduction.predict(df) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from fairlearn.widget import FairlearnDashboard | |
| FairlearnDashboard(y_true = target, | |
| y_pred = prediction, | |
| sensitive_features = gender, | |
| sensitive_feature_names = ["gender"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from fairlearn.metrics import demographic_parity_difference, equalized_odds_difference, demographic_parity_ratio | |
| dpd = demographic_parity_difference(target, prediction, sensitive_features = gender) | |
| eod = equalized_odds_difference(target, prediction, sensitive_features = gender) | |
| dpr = demographic_parity_ratio(target, prediction, sensitive_features = gender) | |
| print("Demographic parity difference: {}".format(round(dpd, 2))) | |
| print("Equalized odds difference: {}".format(round(eod, 2))) | |
| print("Demographic parity ratio: {}".format(round(dpr, 2))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from fairlearn.metrics import group_summary | |
| from sklearn.metrics import accuracy_score | |
| group_summary(accuracy_score , target, prediction, sensitive_features = gender) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from sklearn.tree import DecisionTreeClassifier | |
| classifier = DecisionTreeClassifier(min_samples_leaf=10, max_depth=4) # parameters have not been tuned | |
| classifier.fit(df, target) | |
| # Note that we are predicting using the same data as we used for training, this is just for the sake of example | |
| # Never do this in real life | |
| prediction = classifier.predict(df) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| race = df.pop("race") # Pop function drops and assigns at the same time | |
| gender = df.pop("gender") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| df.loc[:, "target"] = df.income.apply(lambda x: int(x == ">50K")) # int(): Fairlearn dashboard requires integer target | |
| df = df.drop("income", axis = 1) # drop income variable to avoid perfect prediction |
NewerOlder