Z3是大小为M * d的矩阵。
JSD函数是:
def JSD(p, q, alpha):
return np.sum(np.sqrt(p+alpha) * np.log(np.sqrt(p+alpha) / np.sqrt(q+alpha)))
similarity = 0
for i in range(Z3.shape[0]):
for j in range(Z3.shape[0]):
if (Z3[j] != Z3[i]).all():
m = 0.5*(Z3[i]+Z3[j])
similarity += 0.5*JSD(Z3[i],m,2)+0.5*JSD(Z3[j],m,2)
print(similarity)
如何为上述方程式编写完美的代码?
答案 0 :(得分:1)
def JSD(p, q, alpha):
return np.sum(np.sqrt(p+alpha) * np.log(np.sqrt(p+alpha) / np.sqrt(q+alpha)))
similarity = 0
for i in range(Z3.shape[0]):
for j in range(Z3.shape[0]):
if (j != i):
m = 0.5*(Z3[i]+Z3[j])
similarity += 0.5*JSD(Z3[i],m,2)+0.5*JSD(Z3[j],m,2)
print(similarity)