Sklearn高斯混合锁参数? [英] Sklearn Gaussian Mixture lock parameters?
问题描述
我正在尝试拟合一些高斯分布,其中我已经对初始参数有了很好的了解(在这种情况下,我正在生成分布,因此我应该始终能够拟合这些分布).但是,我似乎无法弄清楚如何强制平均值为例如0 对于两个高斯.是否可以?m.means_ = ...
不起作用.
I'm trying to fit some gaussians, of which I already have a pretty good idea about the initial parameters (in this case, I'm generating the distributions, so I should always be able to fit these). However, I can't seem to figure out how to force the mean to be e.g. 0 for both gaussians. Is it possible? m.means_ = ...
doesn't work.
from sklearn import mixture
import numpy as np
import math
import matplotlib.pyplot as plt
from scipy import stats
a = np.random.normal(0, 0.2, 500)
b = np.random.normal(0, 2, 800)
obs = np.concatenate([a,b]).reshape(-1,1)
plt.hist(obs, bins = 100, normed = True, color = "lightgrey")
min_range = -8
max_range = 8
n_gaussians = 2
m = mixture.GaussianMixture(n_components = n_gaussians)
m.fit(obs)
# # Get the gaussian parameters
weights = m.weights_
means = m.means_
covars = m.covariances_
# Plot all gaussians
n_gaussians = 2
gaussian_sum = []
for i in range(n_gaussians):
mean = means[i]
sigma = math.sqrt(covars[i])
plotpoints = np.linspace(min_range,max_range, 1000)
gaussian_points = weights[i] * stats.norm.pdf(plotpoints, mean, sigma)
gaussian_points = np.array(gaussian_points)
gaussian_sum.append(gaussian_points)
plt.plot(plotpoints,
weights[i] * stats.norm.pdf(plotpoints, mean, sigma))
sum_gaussian = np.sum(gaussian_sum, axis=0)
plt.plot(plotpoints, sum_gaussian, color = "black", linestyle = "--")
plt.xlim(min_range, max_range)
plt.show()
推荐答案
所以我真正追求的是先验,这意味着它实际上应该与 BayesianGaussianMixture
,允许设置 mean_prior
和 mean_prior_precision
So what I was actually after was known priors, which means that it should actually be fitted with BayesianGaussianMixture
, which allows one to set a mean_prior
and a mean_prior_precision
配合
m = mixture.BayesianGaussianMixture(n_components = n_gaussians, mean_prior = np.array([0]), mean_precision_prior = np.array([1]))
甚至可以强迫它解决这个问题:
One can force it to work out even this:
这篇关于Sklearn高斯混合锁参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!