预测薪资
在本练习中,您将使用 census income 数据集来预测个体的年薪是否超过 $50K。
请记住:创建私有模型时,需将边界作为参数指定,以确保不会产生额外的隐私损失或信息泄露。通常,您可以独立于数据来选择边界,方法包括使用领域知识,或先用 DP 直方图进行搜索。
数据集已加载并拆分为 X_train、y_train、X_test 和 y_test。分类器以 dp_GaussianNB 提供。
本练习是课程的一部分
Python 中的数据隐私与匿名化
练习说明
- 通过在训练数据中计算每列的
min和max,并为我们的 5 列数据分别减去与加上位于 5 到 40 区间的随机数来添加随机噪声,从而设置模型的边界。 - 使用
0.5的 epsilon 和前面创建的边界,创建一个 dp_GaussianNB 分类器。 - 将模型拟合到数据上,并查看分数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Set the min and max of bounds for the data and add noise using random
bounds = (X_train.____(axis=0) - random.____(range(5, 40), 5),
____)
# Built the classifier with epsilon of 0.5
dp_clf = ____(epsilon=____, bounds=____)
# Fit the model to the data and print the score
____
print("The accuracy of the differentially private model is ",
dp_clf.score(X_test, y_test))