[英]How to apply multivariate normal pdf function in Python with my own data
我使用以下代碼生成隨機數據和 plot 概率密度分布。 我怎樣才能對我自己的數據做同樣的事情,如下所示?
代碼
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
mean, cov = [0, 1], [(1, .5), (.5, 1)]
data = np.random.multivariate_normal(mean, cov, 200)
df = pd.DataFrame(data, columns=["X", "Y"])
x, y = np.random.multivariate_normal(mean, cov, 1000).T
g = sns.jointplot(x=x, y=y, data=df, kind="kde", n_levels=75, color="m")
g.plot_joint(plt.scatter, c="black", s=30, linewidth=1, marker="+")
g.ax_joint.collections[0].set_alpha(0)
g.set_axis_labels("X", "Y");
我自己的數據樣本
X Y
0 1 8
1 7 8
2 7 9
3 5 8
4 7 7
5 9 9
6 1 3
4 6 8
5 9 7
6 9 6
7 8 2
8 1 9
9 0 10
10 22 2
11 4 45
12 9 8
我試過了,但我得到了奇怪的值
import numpy as np
mean = np.mean(data1['X'], axis=0)
cov = np.cov(data1['Y'], rowvar=0)
X = multivariate_normal.pdf(data1['X'], mean=2.5, cov=0.5)
mean = np.mean(data1['X'], axis=0)
cov = np.cov(data1['Y'], rowvar=0)
Y = multivariate_normal.pdf(data1['Y'], mean=2.5, cov=0.5)
df = np.concatenate( (X.reshape(-1,1),Y.reshape(-1,1)) , axis=1)
df = pd.DataFrame(df)
df = df.rename({0: 'X', 1: 'Y'}, axis=1)
g = sns.jointplot(x=X, y=Y, data=df, kind="kde", n_levels=75, color="r")
g.plot_joint(plt.scatter, c="black", s=30, linewidth=1, marker="+")
g.ax_joint.collections[0].set_alpha(0)
g.set_axis_labels("X", "Y");
這個解決方案奏效了。
import numpy as np
mean = np.mean(data1['X'], axis=0)
cov = np.cov(data1['Y'], rowvar=0)
X = multivariate_normal.pdf(data1['X'], mean=2.5, cov=0.5)
mean = np.mean(data1['X'], axis=0)
cov = np.cov(data1['Y'], rowvar=0)
Y = multivariate_normal.pdf(data1['Y'], mean=2.5, cov=0.5)
df = np.concatenate( (X.reshape(-1,1),Y.reshape(-1,1)) , axis=1)
df = pd.DataFrame(df)
df = df.rename({0: 'X', 1: 'Y'}, axis=1)
g = sns.jointplot(x=X, y=Y, data=df, kind="kde", n_levels=75, color="r")
g.plot_joint(plt.scatter, c="black", s=30, linewidth=1, marker="+")
g.ax_joint.collections[0].set_alpha(0)
g.set_axis_labels("X", "Y");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.