使用组件参数进行GaussianMixture初始化-Sklearn [英] GaussianMixture initialization using component parameters - sklearn

查看:363
本文介绍了使用组件参数进行GaussianMixture初始化-Sklearn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 sklearn.mixture.GaussianMixture 存储高斯混合模型,以便以后可以使用它使用score_samples方法在样本点生成样本或值.这是一个示例,其中的分量具有以下权重,均值和协方差

I want to use sklearn.mixture.GaussianMixture to store a gaussian mixture model so that I can later use it to generate samples or a value at a sample point using score_samples method. Here is an example where the components have the following weight, mean and covariances

import numpy as np
weights = np.array([0.6322941277066596, 0.3677058722933399])
mu = np.array([[0.9148052872961359, 1.9792961751316835], 
               [-1.0917396392992502, -0.9304220945910037]])
sigma = np.array([[[2.267889129267119, 0.6553245618368836], 
                        [0.6553245618368835, 0.6571014653342457]], 
                       [[0.9516607767206848, -0.7445831474157608], 
                        [-0.7445831474157608, 1.006599716443763]]])

然后我按照以下步骤初始化混合物

Then I initialised the mixture as follow

from sklearn import mixture
gmix = mixture.GaussianMixture(n_components=2, covariance_type='full')
gmix.weights_ = weights   # mixture weights (n_components,) 
gmix.means_ = mu          # mixture means (n_components, 2) 
gmix.covariances_ = sigma  # mixture cov (n_components, 2, 2) 

最后,我尝试根据导致错误的参数生成样本:

Finally I tried to generate a sample based on the parameters which resulted in an error:

x = gmix.sample(1000)
NotFittedError: This GaussianMixture instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

据我了解,GaussianMixture旨在使用高斯混合来拟合样本,但是有没有办法为它提供最终值并从那里继续呢?

As I understand GaussianMixture is intended to fit a sample using a mixture of Gaussian but is there a way to provide it with the final values and continue from there?

推荐答案

彼得森(J.P. Petersen),您真是不可思议! 看到您的答案后,我比较了使用fit方法引入的更改.似乎初始实例化并未创建gmix的所有属性.具体来说,它缺少以下属性

You rock, J.P.Petersen! After seeing your answer I compared the change introduced by using fit method. It seems the initial instantiation does not create all the attributes of gmix. Specifically it is missing the following attributes,

covariances_
means_
weights_
converged_
lower_bound_
n_iter_
precisions_
precisions_cholesky_

在分配给定输入时引入前三个.在其余的应用程序中,对于我的应用程序,我唯一需要的属性是precisions_cholesky_,这是逆Covarinace矩阵的cholesky分解.作为最低要求,我将其添加如下,

The first three are introduced when the given inputs are assigned. Among the rest, for my application the only attribute that I need is precisions_cholesky_ which is cholesky decomposition of the inverse covarinace matrices. As a minimum requirement I added it as follow,

gmix.precisions_cholesky_ = np.linalg.cholesky(np.linalg.inv(sigma)).transpose((0, 2, 1))

这篇关于使用组件参数进行GaussianMixture初始化-Sklearn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆