Skip to main content

Diffprivlib v0.6

Python versions Downloads PyPi version PyPi status General tests Documentation Status CodeQL codecov

Diffprivlib is a general-purpose library for differential privacy (DP). Use diffprivlib if you are looking to:

  • Experiment with differential privacy
  • Explore the impact of differential privacy on machine learning and data analytics applications
  • Prototype your own differential privacy algorithms

Since its initial release in 2019, diffprivlib has proven to be an invaluable resource for the DP community, with hundreds of citations, stars, forks and deployments. The library has lowered the barrier to entry for new scientists and engineers working in and learning about DP, spawned new research, and served as a benchmark for new algorithms and libraries.

Note: The public release of diffprivlib is intended for research and education purposes only. Please reach out to us if you are interested in using diffprivlib in a production environment.

Getting started: Machine learning 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.

from diffprivlib.models import GaussianNB

clf = 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!

print("Test accuracy: %f" % clf.score(X_test, y_test))

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, 2.0, 1.1, 0.1], [7.9, 4.4, 6.9, 2.5])
accuracy = list()

for epsilon in epsilons:
    clf = GaussianNB(bounds=bounds, epsilon=epsilon)
    clf.fit(X_train, y_train)
    
    accuracy.append(clf.score(X_test, y_test))

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 four major components:

  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, classification, regression, dimensionality reduction and pre-processing.
  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.
  4. Accountant: The BudgetAccountant class can be used to track privacy budget and calculate total privacy loss using advanced composition techniques.

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

Citing diffprivlib

If you use diffprivlib for research, please consider citing the following reference paper:

@article{diffprivlib,
  title={Diffprivlib: the {IBM} differential privacy library},
  author={Holohan, Naoise and Braghin, Stefano and Mac Aonghusa, P{\'o}l and Levacher, Killian},
  year={2019},
  journal = {ArXiv e-prints},
  archivePrefix = "arXiv",
  volume = {1907.02444 [cs.CR]},
  primaryClass = "cs.CR",
  month = jul
}

References

Acknowledgement

Work in this repository was partially supported by the European Union's Horizon research and innovation programme under grant numbers 951911 (AI4Media) and 101070473 (FLUIDOS).

Metadata

Release files for diffprivlib 0.6.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for diffprivlib 0.6.6
File Size Uploaded
diffprivlib-0.6.6.tar.gz 98.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for diffprivlib 0.6.6
File Interpreter ABI Platform
diffprivlib-0.6.6-py3-none-any.whl Python 3 none any Details

Total release size: 274.9 kB

Release files / diffprivlib-0.6.6.tar.gz

Download URL diffprivlib-0.6.6.tar.gz
Size 98.1 kB
Tags Source
SHA-256 checksum
How to use checksums
564262ce534b13d58c82b1bbf109f25b5c55dc1b198674bc348d092e7a38a7f0
BLAKE2b-256 checksum
How to use checksums
c714b41715a8c48df80ce5e4f0459b03edb206e3b13fc0b399a8e7d13808e092
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / diffprivlib-0.6.6-py3-none-any.whl

Download URL diffprivlib-0.6.6-py3-none-any.whl
Size 176.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8368611a43d76471839f14e2c6e576a979ff10f54c1957f150d538a28074751
BLAKE2b-256 checksum
How to use checksums
23ccea04afcaef23bd934eebba9d915fd71e876d9ce255884e78184f1c1445bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.6.6 This release

2 release files

0.6.5

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.2

2 release files

0.5.1

1 release file

0.5.0

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.2

1 release file

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page