Package: diffprivlib
Diffprivlib v0.6Diffprivlib is a general-purpose library for experimenting with, investigating and developing applications in, differential privacy.Use diffprivlib if you are looking to:Experiment with differential privacyExplore the impact of differential privacy on machine learning accuracy using classification and clustering modelsBuild your own differential privacy applications, using our extensive collection of mechanismsDiffprivlib supports Python versions 3.8 to 3.11.Getting started:Machine learning with differential privacy in 30 secondsWe're using theIris dataset, so let's load it and perform an 80/20 train/test split.fromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_splitdataset=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 classifierruns just like ansklearnclassifier, so you can get up and running quickly.diffprivlib.models.GaussianNBcan be runwithout any parameters, although this will throw a warning (we need to specify theboundsparameter to avoid this). The privacy level is controlled by the parameterepsilon, which is passed to the classifier at initialisation (e.g.GaussianNB(epsilon=0.1)). The default isepsilon = 1.0.fromdiffprivlib.modelsimportGaussianNBclf=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 variousepsilonvalues and plot it withmatplotlib.importnumpyasnpimportmatplotlib.pyplotaspltepsilons=np.logspace(-2,2,50)bounds=([4.3,2.0,1.1,0.1],[7.9,4.4,6.9,2.5])accuracy=list()forepsiloninepsilons: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()Congratulations, you've completed your first differentially private machine learning task with the Differential Privacy Library!  Check out more examples in thenotebooksdirectory, ordive straight in.ContentsDiffprivlib is comprised of four major components: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.Models:This module includes machine learning models with differential privacy. Diffprivlib currently has models for clustering, classification, regression, dimensionality reduction and pre-processing.Tools:Diffprivlib comes with a number of generic tools for differentially private data analysis. This includes differentially private histograms, following the same format asNumpy's histogram function.Accountant:TheBudgetAccountantclass can be used to track privacy budget and calculate total privacy loss using advanced composition techniques.SetupInstallation withpipThe library is designed to run with Python 3.
The library can be installed from the PyPi repository usingpip(orpip3):pipinstalldiffprivlibManual installationFor the most recent version of the library, either download the source code or clone the repository in your directory of choice:gitclonehttps://github.com/IBM/differential-privacy-libraryTo installdiffprivlib, do the following in the project folder (alternatively, you can runpython3 -m pip install .):pipinstall.The library comes with a basic set of unit tests forpytest. To check your install, you can run all the unit tests by callingpytestin the install folder:pytestCiting diffprivlibIf 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
}ReferencesHolohan, N., Antonatos, S., Braghin, S. and Mac Aonghusa, P., 2018.The Bounded Laplace Mechanism in Differential privacy.Journal of Privacy and Confidentiality 10 (1).Holohan, N., Braghin, S., Mac Aonghusa, P. and Levacher, K., 2019.Diffprivlib: the IBM Differential Privacy Library.ArXiv e-prints 1907.02444 [cs.CR].Ludwig, H., Baracaldo, N., Thomas, G., Zhou, Y., Anwar, A., Rajamoni, S., Ong, Y., Radhakrishnan, J., Verma, A., Sinn, M. and Purcell, M., 2020.IBM Federated Learning: an Enterprise Framework White Paper v0.1.ArXiv e-prints 2007.10987 [cs.LG].Holohan, N. and Braghin, S., 2021.Secure Random Sampling in Differential Privacy.In Computer Security–ESORICS 2021: 26th European Symposium on Research in Computer Security, Darmstadt, Germany, October 4–8, 2021, Proceedings, Part II 26 (pp. 523-542). Springer International Publishing.Holohan, N., 2023.Random Number Generators and Seeding for Differential Privacy.ArXiv e-prints 2307.03543 [cs.CR].AcknowledgementWork in this repository was partially supported by the European Union's Horizon 2020 research and innovation programme under grant number 951911 – AI4Media.