python实现差分隐私Laplace机制详解Laplace分布定义:
下⾯先给出Laplace分布实现代码:
import matplotlib.pyplot as plt
import numpy as np
def laplace_function(x,beta):鹤咀
result = (1/(2*beta)) * np.e**(-1*(np.abs(x)/beta))
return result
#在-5到5之间等间隔的取10000个数
x = np.linspace(-5,5,10000)
y1 = [laplace_function(x_,0.5) for x_ in x]
y2 = [laplace_function(x_,1) for x_ in x]
y3 = [laplace_function(x_,2) for x_ in x]
plt.plot(x,y1,color='r',label='beta:0.5')
plt.plot(x,y2,color='g',label='beta:1')
plt.plot(x,y3,color='b',label='beta:2')
plt.title("Laplace distribution")
plt.legend()
怎样养乌龟plt.show()
效果图如下:
白月光歌曲
接下来给出Laplace机制实现:
Laplace机制,即在操作函数结果中加⼊服从Laplace分布的噪声。
Laplace概率密度函数Lap(x|b)=1/2b exp(-|x|/b)正⽐于exp(-|x|/b)。
import numpy as np
def noisyCount(nsitivety,epsilon):
beta = nsitivety/epsilon景阳岗
酋犬
u1 = np.random.random()
u2 = np.random.random()
if u1 <= 0.5:
n_value = -beta*np.log(1.-u2)
el:
n_value = beta*np.log(u2)
print(n_value)
return n_value滑膜炎的最佳治疗方法
def laplace_mech(data,nsitivety,epsilon):
for i in range(len(data)):
data[i] += noisyCount(nsitivety,epsilon)
return data
if __name__ =='__main__':
藕断丝连歌词x = [1.,1.,0.]
nsitivety = 1
epsilon = 1
data = laplace_mech(x,nsitivety,epsilon)
for j in data:
草莓多print(j)
以上这篇python实现差分隐私Laplace机制详解就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。