預測薪資
在這個練習中,你會使用 census income 資料集來預測個人年薪是否超過 $50K。
記得在建立私有模型時,要以參數形式指定界線(bounds),以避免額外的隱私流失或資訊外洩。通常你可以不依賴資料本身來選擇界線,而是運用領域知識,或透過 DP 直方圖搜尋。
資料集已載入並分割為 X_train、y_train、X_test 與 y_test。分類器為 dp_GaussianNB。
本練習屬於課程
Data Privacy and Anonymization in Python
練習說明
- 以訓練資料計算各欄位的
min與max,並在 5 個欄位上分別減去與加上介於 5 到 40 的亂數來加入雜訊,據此設定模型的界線。 - 建立一個 epsilon 為
0.5,並使用上述界線的 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))