gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:43 26 4
gpt4 key购买 nike

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

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

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

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

有没有人知道如何操作上述方法,或者有没有人知道另一种方法,以便 GMM 可以用加权数据拟合?

最佳答案

采纳 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)

关于python 用协方差最小的高斯混合模型(GMM)拟合加权数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36423741/

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