You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AIF360指标时numpy数组转BinaryLabelDataset的解决方案

解决方法

核心思路是将测试集特征与预测标签结合,复用原始数据集的元数据,构造AIF360要求的BinaryLabelDataset对象后再传入指标类。原有代码中SMOTETomek的调用存在参数错误,已同步修正。

修改后的完整代码如下:

from aif360.datasets import BinaryLabelDataset
from sklearn.preprocessing import MinMaxScaler
from imblearn.combine import SMOTETomek
from sklearn.model_selection import train_test_split
# 此处补充你自己的模型导入逻辑

# dataset_orig is standard_dataset
scaler = MinMaxScaler(copy=False)
dataset_orig.features = scaler.fit_transform(dataset_orig.features)

# 拆分数据集为特征X和标签Y 
X_orig = dataset_orig.features
y_orig = dataset_orig.labels

# 修正SMOTETomek传参错误,第二个参数为标签y_orig
smote_tomek = SMOTETomek(random_state=0)
X_resampled, y_resampled = smote_tomek.fit_resample(X_orig, y_orig)

# 划分训练集和测试集
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)

# 构造符合要求的BinaryLabelDataset对象,复用原始数据集的元配置
pred_dataset = BinaryLabelDataset(
    df=None,
    features=X_test,
    labels=y_pred.reshape(-1, 1),  # AIF360要求标签为二维数组
    protected_attributes=X_test[:, [dataset_orig.feature_names.index('sex')]],  # 提取受保护属性列
    protected_attribute_names=dataset_orig.protected_attribute_names,
    label_names=dataset_orig.label_names,
    favorable_label=dataset_orig.favorable_label,
    unfavorable_label=dataset_orig.unfavorable_label,
    privileged_protected_attributes=dataset_orig.privileged_protected_attributes,
    unprivileged_protected_attributes=dataset_orig.unprivileged_protected_attributes
)

p = [{'sex': 1.}]
u = [{'sex': 0.}]
cm = BinaryLabelDatasetMetric(pred_dataset, 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())

关键说明

  • 构造BinaryLabelDataset时必须传入测试集特征,否则consistency这类依赖个体特征的指标无法正常计算
  • 所有元数据(标签取值、受保护属性配置等)直接复用原始dataset_orig的属性,避免手动配置出现取值不匹配问题
  • 预测得到的y_pred默认是一维数组,需要通过reshape(-1,1)转为AIF360要求的二维标签格式

内容的提问来源于stack exchange,提问作者bb12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 18:36:05