def fairness_dashboard(sensitive_feature_df, yelp_test_dataset):

    from fairlearn.widget import FairlearnDashboard
    #TODO split out test dataset to features and labels
    X_test, y_test = yelp_test_dataset, yelp_test_dataset
    FairlearnDashboard(sensitive_features=sensitive_feature_df, 
                    sensitive_feature_names=['gender'],
                    y_true=y_test,
                    y_pred={"lr_model": predict(text, model, vocab, ngrams=2)})
def show_comparison(model, X_test, y_test, A_test, protected_features,
                    prostprocess_preds):
    """
    Returns Dashboard to show comparison of models based on the trade off of the disparity and accuracy
    
    """
    FairlearnDashboard(sensitive_features=A_test,
                       sensitive_feature_names=protected_features,
                       y_true=Y_test,
                       y_pred={
                           "Unmitigated": model.predict(X_test),
                           "ThresholdOptimizer": postprocess_preds
                       })

    return dashboard
示例#3
0
def show_comparison(model, X_test, Y_test, A_test, protected_features,
                    non_dominated):
    """
    Returns Dashboard to show comparison of models based on the trade off of the disparity and accuracy
    
    """
    dashboard_predicted = {"unmitigated": model.predict(X_test)}
    for i in range(len(non_dominated)):
        key = "dominant_model_{0}".format(i)
        value = non_dominated[i].predict(X_test)
        dashboard_predicted[key] = value

    dashboard = FairlearnDashboard(sensitive_features=A_test,
                                   sensitive_feature_names=protected_features,
                                   y_true=Y_test,
                                   y_pred=dashboard_predicted)

    return dashboard
示例#4
0
def fairness_dashboard(A_test, protected_features, y_true, y_pred):
    """
    Displays an interactive dashboards that shows fairness metrics given the data, 
    protected features and the train-test targets.
    
    Parameters:
    A_test (pandas dataframe): Input data to be analyzed for fairness metrics.
    protected_features (list): Features or columns we need to account for potential biases.
    y_true (list): List of ground truths
    y_pred (list): List of predictions
    
    Returns:
    Interactive dashboard for fairness metrics.
    """

    FairlearnDashboard(sensitive_features=A_test,
                       sensitive_feature_names=protected_features,
                       y_true=y_true.values.tolist(),
                       y_pred=[y_pred.tolist()])
示例#5
0
# To show the effect of Fairlearn we will first train a standard ML predictor
# that does not incorporate fairness.
# For speed of demonstration, we use the simple
# :class:`sklearn.linear_models.LogisticRegression` class:

unmitigated_predictor = LogisticRegression(solver='liblinear',
                                           fit_intercept=True)

unmitigated_predictor.fit(X_train, Y_train)

# %%
# We can load this predictor into the Fairness dashboard, and assess its fairness:

FairlearnDashboard(
    sensitive_features=A_test,
    sensitive_feature_names=['sex'],
    y_true=Y_test,
    y_pred={"unmitigated": unmitigated_predictor.predict(X_test)})

# %%
# Looking at the disparity in accuracy, we see that males have an error
# about three times greater than the females.
# More interesting is the disparity in opportunity - males are offered loans at
# three times the rate of females.
#
# Despite the fact that we removed the feature from the training data, our
# predictor still discriminates based on sex.
# This demonstrates that simply ignoring a sensitive feature when fitting a
# predictor rarely eliminates unfairness.
# There will generally be enough other features correlated with the removed
# feature to lead to disparate impact.
示例#6
0
                                y_pred,
                                sensitive_features=sex)
print("group_summary", result1)
result2 = metrics.selection_rate_group_summary(y_true,
                                               y_pred,
                                               sensitive_features=sex)
print("selection_rate_group_summary", result2)
# FairlearnDashboard(sensitive_features=sex,
#                        sensitive_feature_names=['sex'],
#                        y_true=y_true,
#                        y_pred={"initial model": y_pred})

np.random.seed(0)
constraint = DemographicParity()
classifier = DecisionTreeClassifier()
mitigator = ExponentiatedGradient(classifier, constraint)
#print("constructing mitigator")
mitigator.fit(X, y_true, sensitive_features=sex)
y_pred_mitigated = mitigator.predict(X)
result2_mitigated = metrics.selection_rate_group_summary(
    y_true, y_pred_mitigated, sensitive_features=sex)
print("selection_rate_group_summary mitigated", result2_mitigated)
FairlearnDashboard(sensitive_features=sex,
                   sensitive_feature_names=['sex'],
                   y_true=y_true,
                   y_pred={
                       "initial model": y_pred,
                       "mitigated model": y_pred_mitigated
                   })
#FairlearnDashboard(sensitive_features=sex, sensitive_feature_names=['sex'],y_true=y_true,y_pred={"initial model": y_pred})