IBM/differential-privacy-library
GitHub: IBM/differential-privacy-library
IBM 出品的 Python 差分隐私库,提供与 scikit-learn 兼容的隐私保护机器学习模型和数据分析工具。
Stars: 905 | Forks: 206
# Diffprivlib v0.6
[](https://pypi.org/project/diffprivlib/)
[](https://pepy.tech/project/diffprivlib)
[](https://pypi.org/project/diffprivlib/)
[](https://pypi.org/project/diffprivlib/)
[](https://github.com/IBM/differential-privacy-library/actions/workflows/general.yml)
[](https://diffprivlib.readthedocs.io/en/latest/?badge=latest)
[](https://github.com/IBM/differential-privacy-library/actions/workflows/codeql.yml)
[](https://codecov.io/gh/IBM/differential-privacy-library)
Diffprivlib 是一个用于差分隐私 (DP) 的通用库。如果您有以下需求,请使用 diffprivlib:
- 实验差分隐私
- 探索差分隐私对机器学习和数据分析应用的影响
- 构建您自己的差分隐私算法原型
自 2019 年首次发布以来,diffprivlib 已被证明是 DP 社区的宝贵资源,拥有数百次的引用、Star、Fork 和部署。该库降低了从事 DP 工作和学习的新科学家和工程师的入门门槛,催生了新的研究,并作为新算法和库的基准。
__注意:__ diffprivlib 的公开版本仅用于研究和教育目的。如果您有兴趣在生产环境中使用 diffprivlib,请联系我们。
## 快速入门:[30 秒上手差分隐私机器学习](https://github.com/IBM/differential-privacy-library/blob/main/notebooks/30seconds.ipynb)
我们使用 [Iris 数据集](https://archive.ics.uci.edu/ml/datasets/iris),先加载它并进行 80/20 的训练/测试集拆分。
```
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)
```
现在,让我们训练一个差分隐私朴素贝叶斯分类器。我们的分类器__运行起来就像一个 `sklearn` 分类器__,因此您可以快速上手。
`diffprivlib.models.GaussianNB` 可以在__没有任何参数__的情况下运行,尽管这会抛出一个警告(我们需要指定 `bounds` 参数来避免这种情况)。隐私级别由参数 `epsilon` 控制,该参数在初始化时传递给分类器(例如 `GaussianNB(epsilon=0.1)`)。默认值为 `epsilon = 1.0`。
```
from diffprivlib.models import GaussianNB
clf = GaussianNB()
clf.fit(X_train, y_train)
```
我们现在可以对未见过的样本进行分类,并且知道训练好的模型是差分隐私的,保护了训练集中“个体”的隐私(花朵也有权享有隐私!)。
```
clf.predict(X_test)
```
由于差分隐私的随机性,每次使用 `.fit()` 训练模型时,都会产生不同的模型。因此,即使使用相同的训练数据重新训练,准确率也会发生变化。亲自试一下就知道了!
```
print("Test accuracy: %f" % clf.score(X_test, y_test))
```
我们可以轻松评估模型在不同 `epsilon` 值下的准确率,并使用 `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()
```

恭喜,您已经使用 Differential Privacy Library 完成了您的第一个差分隐私机器学习任务! 查看 [notebooks](https://github.com/IBM/differential-privacy-library/blob/main/notebooks/) 目录中的更多示例,或者[直接开始](https://github.com/IBM/differential-privacy-library/blob/main/diffprivlib/)。
## 内容
Diffprivlib 由四个主要组件构成:
1. __Mechanisms(机制):__ 这些是差分隐私的构建模块,用于所有实现差分隐私的模型。机制很少有默认设置,旨在供实现自己模型的专家使用。然而,它们也可以在模型之外用于单独的调查等。
2. __Models(模型):__ 此模块包含具有差分隐私的机器学习模型。Diffprivlib 目前拥有用于聚类、分类、回归、降维和预处理的模型。
3. __Tools(工具):__ Diffprivlib 附带了许多用于差分隐私数据分析的通用工具。这包括差分隐私直方图,遵循与 [Numpy 的直方图函数](https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html)相同的格式。
4. __Accountant(账本):__ `BudgetAccountant` 类可用于跟踪隐私预算,并使用高级组合技术计算总隐私损失。
## 设置
### 使用 `pip` 安装
该库设计为在 Python 3 下运行。
可以使用 `pip`(或 `pip3`)从 PyPi 存储库安装该库:
```
pip install diffprivlib
```
### 手动安装
要获取最新版本的库,请下载源代码或将存储库克隆到您选择的目录中:
```
git clone https://github.com/IBM/differential-privacy-library
```
要安装 `diffprivlib`,请在项目文件夹中执行以下操作(或者,您可以运行 `python3 -m pip install .`):
```
pip install .
```
该库附带了一套基本的 `pytest` 单元测试。要检查您的安装,您可以通过在安装文件夹中调用 `pytest` 来运行所有单元测试:
```
pytest
```
## 引用 diffprivlib
如果您在研究中使用 diffprivlib,请考虑引用以下参考论文:
```
@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
}
```
## 参考文献
* Holohan, N., Antonatos, S., Braghin, S. and Mac Aonghusa, P., 2018. [The Bounded Laplace Mechanism in Differential privacy](https://doi.org/10.29012/jpc.715). *Journal of Privacy and Confidentiality 10 (1).*
* Holohan, N., Braghin, S., Mac Aonghusa, P. and Levacher, K., 2019. [Diffprivlib: the IBM Differential Privacy Library](https://arxiv.org/abs/1907.02444). *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](https://doi.org/10.48550/arXiv.2007.10987). *ArXiv e-prints 2007.10987 [cs.LG].*
* Holohan, N. and Braghin, S., 2021. [Secure Random Sampling in Differential Privacy](https://doi.org/10.1007/978-3-030-88428-4_26). *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](https://doi.org/10.48550/arXiv.2307.03543). *ArXiv e-prints 2307.03543 [cs.CR].*
* Holohan, N., Braghin, S. and Suliman, M., 2024. [Securing Floating-Point Arithmetic for Noise Addition](https://doi.org/10.1145/3658644.3690347). *In Proceedings of the 2024 on ACM SIGSAC Conference on Computer and Communications Security (pp. 1954-1966).*
## 致谢
本存储库中的工作部分得到了欧盟 Horizon 研究和创新计划的支持,资助编号为 951911 (AI4Media) 和 101070473 (FLUIDOS)。
标签:Apex, IBM, Python, 人工智能, 代码示例, 差分隐私, 开源库, 抗噪声, 搜索引擎爬虫, 数据分析, 数据脱敏, 无后门, 机器学习, 用户模式Hook绕过, 科学研究, 算法原型, 统计数据, 网络安全, 逆向工具, 隐私保护, 隐私计算