【问题标题】:Calculate group fairness metrics with AIF360使用 AIF360 计算组公平性指标
【发布时间】:2021-02-06 22:19:51
【问题描述】:

我想使用AIF360 计算group fairness metrics。这是一个样本数据集和模型,其中性别是受保护的属性,收入是目标。

import pandas as pd
from sklearn.svm import SVC
from aif360.sklearn import metrics

df = 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 的类似错误。似乎需要以不同的方式输入数据,但我无法弄清楚如何输入。

【问题讨论】:

    标签: python pandas machine-learning aif360


    【解决方案1】:

    这可以通过将数据转换为StandardDataset 然后调用下面的fair_metrics 函数来完成:

    from aif360.datasets import StandardDataset
    from aif360.metrics import BinaryLabelDatasetMetric, ClassificationMetric
    
    dataset = 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 result
    
    
    fair_metrics(dataset, y_pred)
    

    返回正确结果 (image ref):

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

    【讨论】:

      【解决方案2】:

      删除函数调用中的y_true=y_pred= 字符并重试。正如在documentation 中看到的那样,函数原型中的*y 代表任意数量的参数(请参阅this post)。所以这是最合乎逻辑的猜测。

      换句话说,y_truey_pred 不是关键字参数。所以他们不能用他们的名字传递。关键字参数在函数原型中表示为**kwargs

      【讨论】:

      • 谢谢。它解决了当前的错误,但现在它抛出了ValueError: Some of the attributes provided are not present in the dataset,考虑到df ["gender"] 没有提供给函数,这是有道理的。
      • 我敢打赌,问题现在出在数据中,因为错误消息现在是数据集属性上的 ValueError。它现在与函数调用本身无关。
      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2020-12-15
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多