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 인자를 전달해 min/max를 지정합니다. 가능한 형태:
(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으로 배우는 데이터 프라이버시와 익명화