Skip to main content

IBM Differential Privacy Library

Project description

IBM Differential Privacy Library

Python versions PyPi version Build Status Documentation Status Language grade: Python

You have just found the IBM Differential Privacy Library

The IBM Differential Privacy Library is a general-purpose library for experimenting, investigating and developing applications in differential privacy.

Use the Differential Privacy Library if you are looking to:

  • Experiment with differential privacy
  • Explore the impact of differential privacy on machine learning accuracy using basic classification and clustering models
  • Build your own differential privacy applications, using our extensive collection of mechanisms

Diffprivlib is compatible with: Python 3.4–3.7.

Getting started: ML with differential privacy in 30 seconds

We're using the Iris dataset, so let's load it and perform an 80/20 train/test split.

from sklearn import datasets
from sklearn.model_selection import train_test_split

dataset = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(dataset.data, dataset.target, test_size=0.2)

Now, let's train a differentially private naive Bayes classifier. Our classifier runs just like an sklearn classifier, so you can get up and running quickly.

diffprivlib.models.GaussianNB can be run without any parameters, although this will throw a warning (we need to specify the bounds parameter to avoid this). The privacy level is controlled by the parameter epsilon, which is passed to the classifier at initialisation (e.g. GaussianNB(epsilon=0.1)). The default is epsilon = 1.0.

import diffprivlib.models as dp

clf = dp.GaussianNB()
clf.fit(X_train, y_train)

We can now classify unseen examples, knowing that the trained model is differentially private and preserves the privacy of the 'individuals' in the training set (flowers are entitled to their privacy too!).

clf.predict(X_test)

Every time the model is trained with .fit(), a different model is produced due to the randomness of differential privacy. The accuracy will therefore change, even if it's re-trained with the same training data. Try it for yourself to find out!

(clf.predict(X_test) == y_test).sum() / y_test.shape[0]

We can easily evaluate the accuracy of the model for various epsilon values and plot it with matplotlib.

import numpy as np
import matplotlib.pyplot as plt

epsilons = np.logspace(-2, 2, 50)
bounds = [(4.3, 7.9), (2.0, 4.4), (1.1, 6.9), (0.1, 2.5)]
accuracy = list()

for epsilon in epsilons:
    clf = dp.GaussianNB(bounds=bounds, epsilon=epsilon)
    clf.fit(X_train, y_train)

    accuracy.append((clf.predict(X_test) == y_test).sum() / y_test.shape[0])

plt.semilogx(epsilons, accuracy)
plt.title("Differentially private Naive Bayes accuracy")
plt.xlabel("epsilon")
plt.ylabel("Accuracy")
plt.show()

Differentially private naive Bayes

Congratulations, you've completed your first differentially private machine learning task with the Differential Privacy Library! Check out more examples in the notebooks directory, or dive straight in.

Contents

Diffprivlib is comprised of three modules:

  1. Mechanisms: These are the building blocks of differential privacy, and are used in all models that implement differential privacy. Mechanisms have little or no default settings, and are intended for use by experts implementing their own models. They can, however, be used outside models for separate investigations, etc.
  2. Models: This module includes machine learning models with differential privacy. Diffprivlib currently has models for clustering and classification.
  3. Tools: Diffprivlib comes with a number of generic tools for differentially private data analysis. This includes differentially private histograms, following the same format as Numpy's histogram function.

Setup

Installation with pip

The library is designed to run with Python 3. The library can be installed from the PyPi repository using pip (or pip3):

pip install diffprivlib

Manual installation

For the most recent version of the library, either download the source code or clone the repository in your directory of choice:

git clone https://github.com/IBM/differential-privacy-library

To install diffprivlib, do the following in the project folder (alternatively, you can run python3 -m pip install .):

pip install .

The library comes with a basic set of unit tests for pytest. To check your install, you can run all the unit tests by calling pytest in the install folder:

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

diffprivlib-0.1.1.tar.gz (42.4 kB view details)

Uploaded Source

Built Distribution

diffprivlib-0.1.1-py3-none-any.whl (91.1 kB view details)

Uploaded Python 3

File details

Details for the file diffprivlib-0.1.1.tar.gz.

File metadata

  • Download URL: diffprivlib-0.1.1.tar.gz
  • Upload date:
  • Size: 42.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for diffprivlib-0.1.1.tar.gz
Algorithm Hash digest
SHA256 db3829a7e7f85edc5f488c754d90a5180a18dfb2ca60d6a1412e2323670bc02f
MD5 475b656404742d2c6ec1ad2fc6ec9828
BLAKE2b-256 e3d2e34512a792dc8a794c1bea8d4bbf1755299f61cfea4920df7f11f28eb5a0

See more details on using hashes here.

File details

Details for the file diffprivlib-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: diffprivlib-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 91.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

File hashes

Hashes for diffprivlib-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0805d29cd509709809fd6ddf6f4c9c280bf117384cd1689a02e06533d98f1788
MD5 87847b8c8bfabf3025ff0f77cc8cb58e
BLAKE2b-256 ca8a87dcf8723f4958d03400ef01cd761fc4c7fe8fe2305041f0e53fee4995e5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page