I use the following code to produce random data and plot the distribution of probability densities. How can I do the same with my own data as shown below?
Code
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");
My own data sample
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
I tried this but I am getting strange values
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");
This solution worked.
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");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.