我正在嘗試創建一個適合高斯分布的資料集。x 和 y 值將相同,并且在 z 軸上,這些值將符合高斯分布。把這個網站作為我自己的資源:https : //towardsdatascience.com/a-python-tutorial-on-generating-and-plotting-a-3d-guassian-distribution-8c6ec6c41d03我寫了以下代碼。但不幸的是,我得到的輸出與我提供的鏈接中的輸出不同。我認為數學公式有錯誤。如果你能幫我修復它,我會很高興。當我在等待 這樣的圖表時,我得到了那種圖表。
先感謝您。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
def np_bivariate_normal_pdf(domain, mean, variance):
X = np.arange(-domain mean, domain mean, variance)
Y = np.arange(-domain mean, domain mean, variance)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 Y**2)
Z = ((1. / np.sqrt(2 * np.pi)) * np.exp(-.5*R**2))
return X, Y, Z
def plt_plot_bivariate_normal_pdf(x, y, z):
fig = plt.figure(figsize=(10, 10))
ax = fig.gca(projection='3d')
ax.plot_surface(x, y, z,
cmap=cm.coolwarm,
linewidth=0,
antialiased=True)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
ax.set_xlim(-5, 20)
ax.set_ylim(-5, 20)
plt.show()
a = np_bivariate_normal_pdf(0.75, 5, 0.01)
b = np_bivariate_normal_pdf(1.875, 3, 0.025)
c = np_bivariate_normal_pdf(1.5, 7.5, 0.02)
d = np_bivariate_normal_pdf(2.25, 12, 0.03)
plt_plot_bivariate_normal_pdf(*a)
plt_plot_bivariate_normal_pdf(*b)
plt_plot_bivariate_normal_pdf(*c)
plt_plot_bivariate_normal_pdf(*d)
uj5u.com熱心網友回復:
該函式np_bivariate_normal_pdf()使用一維正態分布的公式,而您打算計算多元正態分布。多元分布取決于作為向量的均值(這決定了圖形的峰值所在的位置)和協方差矩陣(當您從不同側接近峰值時,它控制圖形的陡峭程度)。在您的函式中,均值和方差都是數字,您使用的公式實際上根本不涉及這些引數。您可以更改您的代碼來修復它,或者您可以使用實作了多元正態分布的多個 Python 庫之一(例如scipy.stats)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/394385.html
標籤:Python 麻木的 matplotlib 数据集 高斯
