我有以下数据
import numpy as np
x = np.random.normal(100, 20, 100) # these data point come from normal but they could come from any distribution
y = np.random.normal(110, 20, 100) # these data point come from normal but they could come from any distribution在plotly-express的帮助下,我可以绘制它们的联合分布
import plotly.express as px
fig = px.density_contour(None, x=x, y=y)
fig.update_traces(contours_coloring="fill", contours_showlabels = True)
fig.show()

我正在寻找一种方法,从上面的图中显示的分布(这是未知的)随机抽样n观测。
我怎么能这么做?
发布于 2022-10-18 15:11:58
这里有一个通过scikit学习的快速方法。最难的部分是找到适合你需要的超参数。
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.neighbors import KernelDensity
x = np.random.normal(100, 20, 100) # these data point come from normal but they could come from any distribution
y = np.random.normal(110, 20, 100)
S = np.vstack((x,y)).T # stack our samples to be of Dx2
kde = KernelDensity(bandwidth=2, rtol=0.01)
kde.fit(S)
new_data = kde.sample(100, random_state=0)
kde = KernelDensity(bandwidth=2, rtol=0.01)
kde.fit(S)
new_data = kde.sample(100, random_state=1)
sns.kdeplot(x=S[:,0], y=S[:,1], cmap="coolwarm", fill=True)
plt.title("Original Distribution")
plt.show()
sns.kdeplot(x=new_data[:,0], y=new_data[:,1], cmap="coolwarm", fill=True)
plt.title("KDE Distribution")
plt.show()


https://stackoverflow.com/questions/74112530
复制相似问题