Pythonで学ぶデータプライバシーと匿名化
Rebeca Gonzalez
Data Engineer


$$
$$

# scikit-learn のナイーブベイズ分類器をインポート from sklearn.naive_bayes import GaussianNB# 差分プライバシー対応のナイーブベイズ分類器をインポート from diffprivlib.models import GaussianNB
from sklearn.naive_bayes import GaussianNB# 非プライベートな分類器を作成 nonprivate_clf = GaussianNB()# 学習データで学習 nonprivate_clf.fit(X_train, y_train)print("The accuracy of the non-private model is ", nonprivate_clf.score(X_test, y_test))
The accuracy of the non-private model is 0.8333333333333334
from diffprivlib.models import GaussianNB as dp_GaussianNB# 既定設定でプライベートな分類器を作成 private_clf = dp_GaussianNB()# 学習し、スコアを確認 private_clf.fit(X_train, y_train)print("The accuracy of the private model is ", private_clf.score(X_test, y_test))
The accuracy of the private model is 0.7
PrivacyLeakWarning: Bounds have not been specified and will be calculated
on the data provided. This will result in additional privacy leakage.
To ensure differential privacy and no additional privacy leakage, specify bounds for each dimension.
"privacy leakage, specify bounds for each dimension.", PrivacyLeakWarning)
データ漏えいを避けるには、bounds 引数で最小・最大値を指定します。 指定方法:
(0,100)
([0,1,0,2],[10,80,5,70])
# 最小・最大を十分に覆うように bounds を設定 bounds = (X_train.min(axis=0) - 1, X_train.max(axis=0) + 1)# epsilon=0.5 で分類器を作成 dp_clf = dp_GaussianNB(epsilon=0.5, bounds=bounds)# 学習し、スコアを確認 dp_clf.fit(X_train, y_train) print("The accuracy of the private model is ", private_clf.score(X_test, y_test))
The accuracy of the private model is 0.807000
# random モジュールをインポート import random # データの bounds の最小・最大に少しノイズを加える bounds = (X_train.min(axis=0) - random.sample(range(0, 30), 12), X_train.max(axis=0) + random.sample(range(0, 30), 12))# epsilon=0.5 で分類器を作成 dp_clf = dp_GaussianNB(epsilon=0.5, bounds=bounds)# 学習し、スコアを確認 dp_clf.fit(X_train, y_train) print("The accuracy of private classifier with bounds is ", dp_clf.score(X_test, y_test))
The accuracy of private classifier with bounds is 0.7544444444

Pythonで学ぶデータプライバシーと匿名化