首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在我的代码中使用AIF360度量时的一个问题

在我的代码中使用AIF360度量时的一个问题
EN

Stack Overflow用户
提问于 2021-09-07 05:24:18
回答 1查看 336关注 0票数 0

我试图在短剧-学习(不平衡-学习)算法上运行AI Fairness 360指标,但我的代码有问题。问题是,当我应用skit-learn (不平衡学习)算法时,它返回一个numpy数组。而AI Fairness 360预处理方法返回BinaryLabelDataset。然后指标应该从BinaryLabelDataset类接收一个对象。我被困在如何将数组转换为BinaryLabelDataset以便能够使用度量的问题上。

我的预处理算法需要接收X,Y。因此,我在调用SMOTE方法之前将数据集分为X和Y。使用SMOTE之前的数据集是standard_dataset的,可以使用指标,但使用SMOTE方法之后的问题是它将数据转换为numpy数组。

在运行代码后,我得到了以下错误:

代码语言:javascript
复制
File "adult_dataset.py", line 654, in <module>
cm = BinaryLabelDatasetMetric(y_pred, privileged_groups=p, unprivileged_groups=u)

raise TypeError("'dataset' should be a BinaryLabelDataset or a MulticlassLabelDataset")
TypeError: 'dataset' should be a BinaryLabelDataset or a MulticlassLabelDataset

下面是我的代码:

代码语言:javascript
复制
# dataset_orig is standard_dataset
scaler = MinMaxScaler(copy=False)
dataset_orig.features = scaler.fit_transform(dataset_orig.features)

# Spilitting data to X and Y 
X_orig = dataset_orig.features
y_orig = dataset_orig.labels

# SMOTETomek returns X_resampled, y_resampled as numpy arrays
smote_tomek = SMOTETomek(random_state=0)
X_resampled, y_resampled = smote_tomek.fit_resample(X_orig, X_orig)


#Creation of Train and Test dataset
X_train, X_test, y_train, y_test = 
train_test_split(X_resampled,y_resampled,test_size=0.2,random_state=42)


model.fit(X_train, y_train)
y_pred = model.predict(X_test)

p = [{'sex': 1.}]
u = [{'sex': 0.}]
cm = BinaryLabelDatasetMetric(y_pred, privileged_groups=p, unprivileged_groups=u)
print("Disparate_Impact", cm.disparate_impact())
print("Statistical Parity Difference", cm.statistical_parity_difference())
print("Consistency (Individual Fairness)", cm.consistency())

我认为调用BinaryLabelDatasetMetric时y_pred的问题。应该是BinaryLabelDataset。有没有办法在我的代码中使用AIF360指标?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-21 17:34:25

您说的对,问题出在y_pred上。您可以将其连接到X_test,将其转换为StandardDataset对象,然后将该对象传递给BinaryLabelDatasetMetric。输出对象将具有计算不同公平度量的方法。我不知道您的数据集是什么样子,但here是一个完整的可重现的示例,您可以调整它来为您的数据集执行此过程。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69082773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档