我试图在短剧-学习(不平衡-学习)算法上运行AI Fairness 360指标,但我的代码有问题。问题是,当我应用skit-learn (不平衡学习)算法时,它返回一个numpy数组。而AI Fairness 360预处理方法返回BinaryLabelDataset。然后指标应该从BinaryLabelDataset类接收一个对象。我被困在如何将数组转换为BinaryLabelDataset以便能够使用度量的问题上。
我的预处理算法需要接收X,Y。因此,我在调用SMOTE方法之前将数据集分为X和Y。使用SMOTE之前的数据集是standard_dataset的,可以使用指标,但使用SMOTE方法之后的问题是它将数据转换为numpy数组。
在运行代码后,我得到了以下错误:
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下面是我的代码:
# 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指标?
发布于 2021-09-21 17:34:25
您说的对,问题出在y_pred上。您可以将其连接到X_test,将其转换为StandardDataset对象,然后将该对象传递给BinaryLabelDatasetMetric。输出对象将具有计算不同公平度量的方法。我不知道您的数据集是什么样子,但here是一个完整的可重现的示例,您可以调整它来为您的数据集执行此过程。
https://stackoverflow.com/questions/69082773
复制相似问题