gpt4 book ai didi

python - 高斯混合模型 (GMM) 拟合不佳

转载 作者:太空宇宙 更新时间:2023-11-04 09:02:17 25 4
gpt4 key购买 nike

我一直在使用 Scikit-learn 的 GMM 函数。首先,我刚刚创建了一个沿 x=y 行的分布。

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

line_model = mixture.GMM(n_components = 99)
#Create evenly distributed points between 0 and 1.
xs = np.linspace(0, 1, 100)
ys = np.linspace(0, 1, 100)

#Create a distribution that's centred along y=x
line_model.fit(zip(xs,ys))
plt.plot(xs, ys)
plt.show()

这会产生预期的分布: The distribution

接下来我将 GMM 拟合到它,并绘制结果:

#Create the x,y mesh that will be used to make a 3D plot
x_y_grid = []
for x in xs:
for y in ys:
x_y_grid.append([x,y])

#Calculate a probability for each point in the x,y grid.
x_y_z_grid = []
for x,y in x_y_grid:
z = line_model.score([[x,y]])
x_y_z_grid.append([x,y,z])

x_y_z_grid = np.array(x_y_z_grid)

#Plot probabilities on the Z axis.
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x_y_z_grid[:,0], x_y_z_grid[:,1], 2.72**x_y_z_grid[:,2])
plt.show()

生成的概率分布在 x=0x=1 上有一些奇怪的尾部,并且在角落(x=1、y=1 和 x =0,y=0)。 Probability distribution n=99

使用 n_components=5 也显示了这种行为: Probability distribution n=5

这是 GMM 固有的东西,还是实现存在问题,还是我做错了什么?

编辑:从模型中获取分数似乎可以消除这种行为——应该这样吗?

我正在同一数据集上训练这两个模型(x=y 从 x=0 到 x=1)。简单地通过 gmm 的 score 方法检查概率似乎可以消除这种边界效应。为什么是这样?我附上了下面的图和代码。

Checking the scores over different domains affects the distribution.

# Creates a line of 'observations' between (x_small_start, x_small_end)
# and (y_small_start, y_small_end). This is the data both gmms are trained on.
x_small_start = 0
x_small_end = 1
y_small_start = 0
y_small_end = 1

# These are the range of values that will be plotted
x_big_start = -1
x_big_end = 2
y_big_start = -1
y_big_end = 2


shorter_eval_range_gmm = mixture.GMM(n_components = 5)
longer_eval_range_gmm = mixture.GMM(n_components = 5)

x_small = np.linspace(x_small_start, x_small_end, 100)
y_small = np.linspace(y_small_start, y_small_end, 100)
x_big = np.linspace(x_big_start, x_big_end, 100)
y_big = np.linspace(y_big_start, y_big_end, 100)

#Train both gmms on a distribution that's centered along y=x
shorter_eval_range_gmm.fit(zip(x_small,y_small))
longer_eval_range_gmm.fit(zip(x_small,y_small))


#Create the x,y meshes that will be used to make a 3D plot
x_y_evals_grid_big = []
for x in x_big:
for y in y_big:
x_y_evals_grid_big.append([x,y])
x_y_evals_grid_small = []

for x in x_small:
for y in y_small:
x_y_evals_grid_small.append([x,y])

#Calculate a probability for each point in the x,y grid.
x_y_z_plot_grid_big = []
for x,y in x_y_evals_grid_big:
z = longer_eval_range_gmm.score([[x, y]])
x_y_z_plot_grid_big.append([x, y, z])
x_y_z_plot_grid_big = np.array(x_y_z_plot_grid_big)

x_y_z_plot_grid_small = []
for x,y in x_y_evals_grid_small:
z = shorter_eval_range_gmm.score([[x, y]])
x_y_z_plot_grid_small.append([x, y, z])
x_y_z_plot_grid_small = np.array(x_y_z_plot_grid_small)


#Plot probabilities on the Z axis.
fig = plt.figure()
fig.suptitle("Probability of different x,y pairs")

ax1 = fig.add_subplot(1, 2, 1, projection='3d')
ax1.plot(x_y_z_plot_grid_big[:,0], x_y_z_plot_grid_big[:,1], np.exp(x_y_z_plot_grid_big[:,2]))
ax1.set_xlabel('X Label')
ax1.set_ylabel('Y Label')
ax1.set_zlabel('Probability')
ax2 = fig.add_subplot(1, 2, 2, projection='3d')
ax2.plot(x_y_z_plot_grid_small[:,0], x_y_z_plot_grid_small[:,1], np.exp(x_y_z_plot_grid_small[:,2]))
ax2.set_xlabel('X Label')
ax2.set_ylabel('Y Label')
ax2.set_zlabel('Probability')

plt.show()

最佳答案

适合度没有问题,但您使用的可视化效果不佳。一个hint应该是连接(0,1,5)到(0,1,0)的直线,其实只是两点连接的渲染图(这是由于读取点的顺序造成的) .尽管极值处的两个点在您的数据中,但实际上这条线上没有其他点。

出于上述原因,我个人认为使用 3d 图(线)来表示表面是一个相当糟糕的主意,我建议改为使用曲面图或等高线图。

试试这个:

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

line_model = mixture.GMM(n_components = 99)
#Create evenly distributed points between 0 and 1.
xs = np.atleast_2d(np.linspace(0, 1, 100)).T
ys = np.atleast_2d(np.linspace(0, 1, 100)).T

#Create a distribution that's centred along y=x
line_model.fit(np.concatenate([xs, ys], axis=1))
plt.scatter(xs, ys)
plt.show()

#Create the x,y mesh that will be used to make a 3D plot
X, Y = np.meshgrid(xs, ys)
x_y_grid = np.c_[X.ravel(), Y.ravel()]

#Calculate a probability for each point in the x,y grid.
z = line_model.score(x_y_grid)
z = z.reshape(X.shape)

#Plot probabilities on the Z axis.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, z)
plt.show()

从学术的角度来看,我对通过 2D 混合模型在 2D 空间中拟合 1D 线的目标感到非常不舒服。使用 GMM 的流形学习至少要求法线方向具有零方差,从而减少到狄拉克分布。在数值和分析上这是不稳定的,应该避免(在 gmm 拟合中似乎有一些稳定技巧,因为模型的方差在直线的法线方向上相当大)。

还建议在绘制数据时使用 plt.scatter 而不是 plt.plot,因为在拟合它们时没有理由连接点联合分配。

希望这有助于阐明您的问题。

关于python - 高斯混合模型 (GMM) 拟合不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24174349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com