Python 0-1之间的二元正态分布

Python 0-1之间的二元正态分布,python,pandas,gaussian,Python,Pandas,Gaussian,我在下面生成一个多元概率密度函数。这很好,但我希望将Z值标准化,这样它会得到一个介于0和1之间的值 为了实现这一点,我想在平均值处除以分布值,所以在平均值处它总是1,在其他地方则更低。我知道所有值的总和将大于1 我正在潜入Z但是Z的sum但是在打印值时,它们仍然超出了我预期的标准化范围 import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot3d imp

我在下面生成一个多元概率密度函数。这很好,但我希望将Z值标准化,这样它会得到一个介于0和1之间的值

为了实现这一点,我想在平均值处除以分布值,所以在平均值处它总是1,在其他地方则更低。我知道所有值的总和将大于1

我正在潜入
Z
但是
Z
sum
但是在打印值时,它们仍然超出了我预期的标准化范围

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

# Our 2-dimensional distribution will be over variables X and Y
N = 60
X = np.linspace(-3, 3, N)
Y = np.linspace(-3, 4, N)
X, Y = np.meshgrid(X, Y)

# Mean vector and covariance matrix
mu = np.array([0., 1.])
Sigma = np.array([[ 1. , -0.5], [-0.5,  1.5]])

# Pack X and Y into a single 3-dimensional array
pos = np.empty(X.shape + (2,))
pos[:, :, 0] = X
pos[:, :, 1] = Y

def multivariate_gaussian(pos, mu, Sigma):
    """Return the multivariate Gaussian distribution on array pos.

    pos is an array constructed by packing the meshed arrays of variables
    x_1, x_2, x_3, ..., x_k into its _last_ dimension.

    """

    n = mu.shape[0]
    Sigma_det = np.linalg.det(Sigma)
    Sigma_inv = np.linalg.inv(Sigma)
    N = np.sqrt((2*np.pi)**n * Sigma_det)
    # This einsum call calculates (x-mu)T.Sigma-1.(x-mu) in a vectorized
    # way across all the input variables.
    fac = np.einsum('...k,kl,...l->...', pos-mu, Sigma_inv, pos-mu)

    return np.exp(-fac / 2) / N

# The distribution on the variables X, Y packed into pos.
Z = multivariate_gaussian(pos, mu, Sigma)

#normalise Z so range is 0-1
Z = Z/sum(Z)
print(Z)

# Create a surface plot and projected filled contour plot under it.
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, rstride=3, cstride=3, linewidth=1, antialiased=True,
            cmap=cm.magma)

cset = ax.contourf(X, Y, Z, zdir='z', offset=-0.15, cmap=cm.magma)

# Adjust the limits, ticks and view angle
ax.set_zlim(-0.15,1)
ax.view_init(27, -21)

plt.show()

如果要将
Z
标准化,则不需要将其除以总和,而是除以其所有值的最大值。因此,您可以确保新的最大值为1:

#将Z归一化,使范围为0-1
Z=Z/np.最大值(Z)
#显示结果的摘要统计信息
作为pd进口熊猫
打印(pd.Series(Z.Flatte()).Descripte())
因为你有一个高斯分布,你只改变了比例,最大值仍然在平均值
x
y
上。然而,请注意,
Z
现在不再是概率密度函数

count    3.600000e+03
mean     1.605148e-01
std      2.351826e-01
min      6.184228e-08
25%      7.278911e-03
50%      4.492385e-02
75%      2.135538e-01
max      1.000000e+00
dtype: float64