<jupyter_start><jupyter_text>First, let us load the training and testing data from the adult dataset.<jupyter_code>import numpy as np

X_train = np.loadtxt("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data",
                        usecols=(0, 4, 10, 11, 12), delimiter=", ")

y_train = np.loadtxt("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data",
                        usecols=14, dtype=str, delimiter=", ")

X_test = np.loadtxt("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test",
                        usecols=(0, 4, 10, 11, 12), delimiter=", ", skiprows=1)

y_test = np.loadtxt("https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test",
                        usecols=14, dtype=str, delimiter=", ", skiprows=1)
y_test = np.array([a[:-1] for a in y_test])<jupyter_output><empty_output><jupyter_text>Naive Bayes with No PrivacyTo begin, let us first train a regular (non-private) naive Bayes classifier, and test its accuracy.<jupyter_code>from sklearn.naive_bayes import GaussianNB
nonprivate_clf = GaussianNB()
nonprivate_clf.fit(X_train, y_train)

from sklearn.metrics import accuracy_score

print("Non-private test accuracy: %.2f%%" % 
     (accuracy_score(y_test, nonprivate_clf.predict(X_test)) * 100))<jupyter_output>Non-private test accuracy: 79.64%<jupyter_text>Differentially Private Naive Bayes ClassificationFirst, install IBM Differential Privacy Library.<jupyter_code>!pip install diffprivlib<jupyter_output>Collecting diffprivlib
[?25l  Downloading https://files.pythonhosted.org/packages/fe/b8/852409057d6acc060f06cac8d0a45b73dfa54ee4fbd1577c9a7d755e9fb6/diffprivlib-0.3.0.tar.gz (70kB)

[K     |████▋                           | 10kB 20.3MB/s eta 0:00:01
[K     |█████████▎                      | 20kB 19.2MB/s eta 0:00:01
[K     |██████████████                  | 30kB 12.4MB/s eta 0:00:01
[K     |██████████████████▋             | 40kB 13.7MB/s eta 0:00:01
[K     |███████████████████████▎        | 51kB 14.9MB/s eta 0:00:01
[K     |████████████████████████████    | 61kB 16.9MB/s eta 0:00:01
[K     |████████████████████████████████| 71kB 5.4MB/s 
[?25hRequirement already satisfied: numpy>=1.15.0 in /usr/local/lib/python3.6/dist-packages (from diffprivlib) (1.18.5)
Requirement already satisfied: setuptools>=39.0.1 in /usr/local/lib/python3.6/dist-packages (from diffprivlib) (50.3.2)
Requirement already satisfied: scikit-learn>=0.22.0 in /usr/local/lib/python3.6/dist-packages (from diffp[...]<jupyter_text>Using the models.GaussianNB module of diffprivlib, we can train a naive Bayes classifier while satisfying differential privacy. If we don't specify any parameters, the model defaults to epsilon = 1.00.<jupyter_code>import diffprivlib.models as dp
dp_clf = dp.GaussianNB()

dp_clf.fit(X_train, y_train)

print("Differentially private test accuracy (epsilon=%.2f): %.2f%%" % 
      (dp_clf.epsilon, accuracy_score(y_test, dp_clf.predict(X_test)) * 100))<jupyter_output>Differentially private test accuracy (epsilon=1.00): 79.98%<jupyter_text>As we can see from the output accuracies above, the regular (non-private) Naïve Bayes classifier could produce an accuracy of 79.64%, while setting epsilon=1.00, the differentially private Naïve Bayes classifier could achieve an accuracy of 78.59%. If we use a smaller epsilon, it usually leads to better privacy protection while less accuracy. For instance, if we set epsilon=0.01:<jupyter_code>import diffprivlib.models as dp
dp_clf = dp.GaussianNB(epsilon=float("0.01"))
dp_clf.fit(X_train, y_train)

print("Differentially private test accuracy (epsilon=%.2f): %.2f%%" % 
      (dp_clf.epsilon, accuracy_score(y_test, dp_clf.predict(X_test)) * 100))<jupyter_output>Differentially private test accuracy (epsilon=0.01): 76.91%
StarcoderdataJupyterStructuredCleanDedup