使用AIF360计算群体公平性指标

我想使用群体公平性指标通过AIF360进行计算。这是一个示例数据集和模型,其中性别是受保护属性,收入是目标变量。

import pandas as pdfrom sklearn.svm import SVCfrom aif360.sklearn import metricsdf = pd.DataFrame({'gender': [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],                  'experience': [0, 0.1, 0.2, 0.4, 0.5, 0.6, 0, 0.1, 0.2, 0.4, 0.5, 0.6],                  'income': [0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1]})clf = SVC(random_state=0).fit(df[['gender', 'experience']], df['income'])y_pred = clf.predict(df[['gender', 'experience']])metrics.statistical_parity_difference(y_true=df['income'], y_pred=y_pred, prot_attr='gender', priv_group=1, pos_label=1)

它抛出了以下错误:

---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-7-609692e52b2a> in <module>     11 y_pred = clf.predict(X)     12 ---> 13 metrics.statistical_parity_difference(y_true=df['income'], y_pred=y_pred, prot_attr='gender', priv_group=1, pos_label=1)TypeError: statistical_parity_difference() got an unexpected keyword argument 'y_true'

对于disparate_impact_ratio也出现了类似的错误。看起来数据的输入方式需要不同,但我还没能弄清楚该怎么做。


回答:

可以通过将数据转换为StandardDataset,然后调用下面的fair_metrics函数来实现:

from aif360.datasets import StandardDatasetfrom aif360.metrics import BinaryLabelDatasetMetric, ClassificationMetricdataset = StandardDataset(df,                           label_name='income',                           favorable_classes=[1],                           protected_attribute_names=['gender'],                           privileged_classes=[[1]])def fair_metrics(dataset, y_pred):    dataset_pred = dataset.copy()    dataset_pred.labels = y_pred            attr = dataset_pred.protected_attribute_names[0]        idx = dataset_pred.protected_attribute_names.index(attr)    privileged_groups =  [{attr:dataset_pred.privileged_protected_attributes[idx][0]}]     unprivileged_groups = [{attr:dataset_pred.unprivileged_protected_attributes[idx][0]}]     classified_metric = ClassificationMetric(dataset, dataset_pred, unprivileged_groups=unprivileged_groups, privileged_groups=privileged_groups)    metric_pred = BinaryLabelDatasetMetric(dataset_pred, unprivileged_groups=unprivileged_groups, privileged_groups=privileged_groups)    result = {'statistical_parity_difference': metric_pred.statistical_parity_difference(),             'disparate_impact': metric_pred.disparate_impact(),             'equal_opportunity_difference': classified_metric.equal_opportunity_difference()}            return resultfair_metrics(dataset, y_pred)

这将返回正确的结果(图像参考):

{'statistical_parity_difference': -0.6666666666666667, 'disparate_impact': 0.3333333333333333, 'equal_opportunity_difference': 0.0}

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注