python用高斯混合模型(GMM)拟合加权数据,协方差最小

时间:2016-04-05 10:26:22

标签: python opencv scikit-learn cluster-analysis em

我想使用python将高斯混合模型拟合到一组加权数据点。

我尝试了sklearn.mixture.GMM(),它的工作正常,除了它对所有数据点的权重相等。有没有人知道在这种方法中为数据点分配权重的方法?我尝试多次使用数据点来“增加它们的重量”,但这似乎对大型数据集无效。

我也考虑过自己实现EM算法,但这似乎要慢得多。上面的GMM方法会极大地增加大型数据集的计算时间。

我刚刚发现了EM算法cv2.EM()的opencv方法。这再次正常,但与sklearn.mixture.GMM具有相同的问题,此外,似乎没有办法改变协方差允许的最小值。或者有没有办法将协方差最小值更改为例如0.001?我希望可以使用probe参数为数据分配权重,但这似乎只是一个输出参数,对拟合过程没有影响,不是吗?使用probs0并使用trainM以M步骤启动算法也没有帮助。对于probs0,我使用了(数据点的数量)x(GMM组件的数量)矩阵,其列相同,而数据点的加权参数被写入对应于数据点的行。这也没有解决问题。它只是产生了一个混合模型,其中所有均值为0.

有谁知道如何操纵上述方法或有没有人知道另一种方法,以便GMM可以配备加权数据?

谢谢, 简

2 个答案:

答案 0 :(得分:0)

如果您仍在寻找解决方案,石榴现在支持对加权数据进行GMM培训。您需要做的就是在训练时传递一个重量矢量,然后它会为您处理。这是关于石榴中GMMs的简短教程!

https://github.com/jmschrei/pomegranate/blob/master/tutorials/Tutorial_2_General_Mixture_Models.ipynb

答案 1 :(得分:0)

根据Jacobs的建议,我编写了一个石榴实现示例:

import pomegranate
import numpy
import sklearn
import sklearn.datasets 

#-------------------------------------------------------------------------------
#Get data from somewhere (moons data is nice for examples)
Xmoon, ymoon = sklearn.datasets.make_moons(200, shuffle = False, noise=.05, random_state=0)
Moon1 = Xmoon[:100] 
Moon2 = Xmoon[100:] 
MoonsDataSet = Xmoon

#Weight the data from moon2 much higher than moon1:
MoonWeights = numpy.array([numpy.ones(100), numpy.ones(100)*10]).flatten()

#Make the GMM model using pomegranate
model = pomegranate.gmm.GeneralMixtureModel.from_samples(
    pomegranate.MultivariateGaussianDistribution,   #Either single function, or list of functions
    n_components=6,     #Required if single function passed as first arg
    X=MoonsDataSet,     #data format: each row is a point-coordinate, each column is a dimension
    )

#Force the model to train again, using additional fitting parameters
model.fit(
    X=MoonsDataSet,         #data format: each row is a coordinate, each column is a dimension
    weights = MoonWeights,  #List of weights. One for each point-coordinate
    stop_threshold = .001,  #Lower this value to get better fit but take longer. 
                            #   (sklearn likes better/slower fits than pomegrante by default)
    )

#Wrap the model object into a probability density python function 
#   f(x_vector)
def GaussianMixtureModelFunction(Point):
    return model.probability(numpy.atleast_2d( numpy.array(Point) ))

#Plug in a single point to the mixture model and get back a value:
ExampleProbability = GaussianMixtureModelFunction( numpy.array([ 0,0 ]) )
print ('ExampleProbability', ExampleProbability)