【发布时间】:2015-12-01 20:53:00
【问题描述】:
所以我已经使用scikit-learn的Gaussian mixture models(http://scikit-learn.org/stable/modules/mixture.html)来拟合我的数据,现在我想使用模型,我该怎么做呢?具体来说:
- 如何绘制概率密度分布?
- 如何计算拟合模型的均方误差?
这是您可能需要的代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from sklearn import mixture
import matplotlib as mpl
from matplotlib.patches import Ellipse
%matplotlib inline
n_samples = 300
# generate random sample, two components
np.random.seed(0)
shifted_gaussian = np.random.randn(n_samples, 2) + np.array([20, 5])
sample= shifted_gaussian
# fit a Gaussian Mixture Model with two components
clf = mixture.GMM(n_components=2, covariance_type='full')
clf.fit(sample)
# plot sample scatter
plt.scatter(sample[:, 0], sample[:, 1])
# 1. Plot the probobility density distribution
# 2. Calculate the mean square error of the fitting model
更新: 我可以通过以下方式绘制分布:
x = np.linspace(-20.0, 30.0)
y = np.linspace(-20.0, 40.0)
X, Y = np.meshgrid(x, y)
XX = np.array([X.ravel(), Y.ravel()]).T
Z = -clf.score_samples(XX)[0]
Z = Z.reshape(X.shape)
CS = plt.contour(X, Y, Z, norm=LogNorm(vmin=1.0, vmax=1000.0),
levels=np.logspace(0, 3, 10))
CB = plt.colorbar(CS, shrink=0.8, extend='both')
【问题讨论】:
-
为什么你认为结果不对?请参阅下面的修改图:
标签: python scikit-learn