<jupyter_start><jupyter_text>Logistic Regression<jupyter_code>eps_list = [0.005, 0.01, 0.05, 0.1]
eq_op_expgrad_error = []
eq_op_expgrad_violation = []
eq_op_best_gap = []

#estimator = LeastSquaresBinaryClassifierLearner()
estimator = LogisticRegression()
for eps in eps_list:
    expgrad_X = ExponentiatedGradient(estimator,
                                      constraints=TruePositiveRateDifference(),
                                      eps=eps, nu=1e-6)
    
    expgrad_X.fit(X, Y, sensitive_features=A)

    def Q(X): return expgrad_X._pmf_predict(X)[:, 1]
    # violation
    disparity_moment = TruePositiveRateDifference()
    disparity_moment.load_data(X, Y, sensitive_features=A)
    disparity = disparity_moment.gamma(Q).max()
    
    # error
    error = ErrorRate()
    error.load_data(X, Y, sensitive_features=A)
    error = error.gamma(Q)[0]
    
    eq_op_expgrad_violation.append(disparity)
    eq_op_expgrad_error.append(error)
    eq_op_best_gap.append(expgrad_X._best_gap)
print('{}\t\t{}\t\t\t{}\t\t\t{}'.format('Epsilon', 'Max Violation', 'Error', 'Best Gap'))
for i in range(len(eps_list)):
    print('{}\t\t{}\t\t{}\t{}'.format(eps_list[i], eq_op_expgrad_violation[i],
                                  eq_op_expgrad_error[i], eq_op_best_gap[i]))
print('{}\t\t{}\t\t{}\t{}'.format('Unmit.', eq_op_violation_unmitigated,
                              eq_op_error_unmitigated, '-'))
import matplotlib.pyplot as plt

plt.scatter(eq_op_expgrad_violation, eq_op_expgrad_error, label="expgrad")
plt.plot(eq_op_violation_unmitigated, eq_op_error_unmitigated, 'ro', label="unmitigated")
plt.xlabel('Violation of the fairness constraint')
plt.ylabel('Error')
plt.title('Adult UCI / Equal Opportunity / Logistic Regression')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>GradBoost<jupyter_code>from sklearn.ensemble import GradientBoostingClassifier
unmitigated_predictor = GradientBoostingClassifier()
unmitigated_predictor.fit(X,Y)
eps_list = [0.005, 0.01, 0.05, 0.1]
eq_op_expgrad_error = []
eq_op_expgrad_violation = []
eq_op_best_gap = []

#estimator = LeastSquaresBinaryClassifierLearner()
estimator = GradientBoostingClassifier()
for eps in eps_list:
    expgrad_X = ExponentiatedGradient(estimator,
                                      constraints=TruePositiveRateDifference(),
                                      eps=eps, nu=1e-6)
    
    expgrad_X.fit(X, Y, sensitive_features=A)

    def Q(X): return expgrad_X._pmf_predict(X)[:, 1]
    # violation
    disparity_moment = TruePositiveRateDifference()
    disparity_moment.load_data(X, Y, sensitive_features=A)
    disparity = disparity_moment.gamma(Q).max()
    
    # error
    error = ErrorRate()
    error.load_data(X, Y, sensitive_features=A)
    error = error.gamma(Q)[0]
    
    eq_op_expgrad_violation.append(disparity)
    eq_op_expgrad_error.append(error)
    eq_op_best_gap.append(expgrad_X._best_gap)
print('{}\t\t{}\t\t\t{}\t\t\t{}'.format('Epsilon', 'Max Violation', 'Error', 'Best Gap'))
for i in range(len(eps_list)):
    print('{}\t\t{}\t\t{}\t{}'.format(eps_list[i], eq_op_expgrad_violation[i],
                                  eq_op_expgrad_error[i], eq_op_best_gap[i]))
print('{}\t\t{}\t\t{}\t{}'.format('Unmit.', eq_op_violation_unmitigated,
                              eq_op_error_unmitigated, '-'))


plt.scatter(eq_op_expgrad_violation, eq_op_expgrad_error, label="expgrad")
plt.plot(eq_op_violation_unmitigated, eq_op_error_unmitigated, 'ro', label="unmitigated")
plt.xlabel('Violation of the fairness constraint')
plt.ylabel('Error')
plt.title('Adult UCI / Equal Opportunity / Grad Boost')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13734022300788687, 'fraction_numerical': 0.008702746804460157, 'mean_word_length': 3.8522427440633247, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '104789', 'license': 'MIT', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1337, 'n_words': 183, 'path': 'notebooks/Exp Gradient for Eq-Op(adultl) w LogReg and GradBoost.ipynb', 'repo_name': 'GeGao2014/fairlearn', 'size': '3677'}