差分隱私的機器學習模型

Data Privacy and Anonymization in Python

Rebeca Gonzalez

Data Engineer

安全共享資料

類似資料的公司,安全共享以優化產品與服務。
  • 包含 Machine Learning(ML)模型

有人查看資料圖表的圖片

Data Privacy and Anonymization in Python

差分隱私的機器學習模型

  • 這是一家與多個線上商店合作的 SaaS 公司。
  • 新夥伴加入時,可能需數月才累積足夠資料
  • 有了 DP,SaaS 公司可鼓勵夥伴共享資料

手持筆電、螢幕射出火箭的男子插圖

$$ $$ 一名男子上方連結多個人像圖示,代表多個夥伴的插圖

Data Privacy and Anonymization in Python

機器學習與隱私

  • 資料集包含敏感資訊。
  • 攻擊者可能濫用機器學習演算法的輸出。
Data Privacy and Anonymization in Python

機器學習與隱私

差分隱私的機器學習模型

  • 服從對應的資料分佈
  • 可保障個體隱私
Data Privacy and Anonymization in Python

差分隱私的分類模型

# Import the scikit-learn naive Bayes classifier
from sklearn.naive_bayes import GaussianNB


# Import the differentially private naive Bayes classifier from diffprivlib.models import GaussianNB
Data Privacy and Anonymization in Python

非隱私分類器

from sklearn.naive_bayes import GaussianNB

# Built the non-private classifier nonprivate_clf = GaussianNB()
# Fit the model to the data 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
Data Privacy and Anonymization in Python

差分隱私分類器

from diffprivlib.models import GaussianNB as dp_GaussianNB

# Build the private classifier with empty constructor private_clf = dp_GaussianNB()
# Fit the model to the data and see the score 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)
Data Privacy and Anonymization in Python

避免隱私洩漏

為避免隱私洩漏,你可透過傳入 bounds 參數來指定最小與最大值。 可以是:

  • 形如(min, max)的 tuple
    • 覆蓋整體資料最小/最大值的整數
      • 範例:
        (0,100)
        
    • 各欄位最小與最大值的陣列。
      • 範例:
        ([0,1,0,2],[10,80,5,70])
        
Data Privacy and Anonymization in Python

避免隱私洩漏

# Set the bounds to cover at least the min and max values 
bounds = (X_train.min(axis=0) - 1, X_train.max(axis=0) + 1)

# Built the classifier with epsilon of 0.5 dp_clf = dp_GaussianNB(epsilon=0.5, bounds=bounds)
# Fit the model to the data and see the score 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
Data Privacy and Anonymization in Python

更多關於設定 bounds

# Import random module
import random
# Set the min and max of bounds in the data plus some noise
bounds = (X_train.min(axis=0) - random.sample(range(0, 30), 12), 
          X_train.max(axis=0) + random.sample(range(0, 30), 12))


# Build the classifier with epsilon of 0.5 dp_clf = dp_GaussianNB(epsilon=0.5, bounds=bounds)
# Fit the model to the data and see the score 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
Data Privacy and Anonymization in Python

不同的 epsilon 值

比較不同 epsilon 值之準確率的圖

Data Privacy and Anonymization in Python

一起來練習吧!

Data Privacy and Anonymization in Python

Preparing Video For Download...