python - 使用 sklearn 在 Python 中初始化参数高斯混合

标签 python numpy scikit-learn gaussian mixture

我真的很努力地用 sklearn 做一个高斯混合,但我想我错过了一些东西,因为它绝对不起作用。
我的原始数据如下所示:

Genotype LogRatio  Strength
AB       0.392805  10.625016
AA       1.922468  10.765716
AB       0.22074   10.405445
BB       -0.059783 10.625016
我想做一个包含 3 个组件 = 3 个基因型 (AA|AB|BB) 的高斯混合物。
我知道每个基因型的权重、每个基因型的对数比的平均值和每个基因型的强度的平均值。
wgts = [0.8,0.19,0.01]  # weight of AA,AB,BB
means = [[-0.5,9],[0.5,9],[1.5,9]] # mean(LogRatio), mean(Strenght) for AA,AB,BB 
我保留 LogRatio 和 Strength 列并创建一个 NumPy 数组。
datas = [[  0.392805  10.625016]
         [  1.922468  10.765716]
         [  0.22074   10.405445]
         [ -0.059783   9.798655]]
然后我测试了来自 sklearn v0.18 的混合函数 GaussianMixture 并尝试了来自 sklearn v0.17 的函数 GaussianMixtureModel (我仍然没有看到差异,也不知道使用哪个)。
gmm = mixture.GMM(n_components=3) 
OR
gmm = mixture.GaussianMixture(n_components=3)

gmm.fit(datas)

colors = ['r' if i==0 else 'b' if i==1 else 'g' for i in gmm.predict(datas)]
ax = plt.gca()
ax.scatter(datas[:,0], datas[:,1], c=colors, alpha=0.8)
plt.show()
这是我得到的,这是一个很好的结果,但它每次都会改变,因为每次运行的初始参数计算方式不同
enter image description here
我想在 gaussianMixture 或 GMM 函数中初始化我的参数,但我不明白我必须如何格式化我的数据:(

最佳答案

可以通过显式播种 random_state 来控制结果可重复性的随机性。伪随机数发生器。

代替 :

gmm = mixture.GaussianMixture(n_components=3)

做 :
gmm = mixture.GaussianMixture(n_components=3, random_state=3)
random_state必须是 int : 我随机设置为3但您可以选择任何其他整数。

使用相同的 random_state 多次运行时,您将获得相同的结果。

关于python - 使用 sklearn 在 Python 中初始化参数高斯混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40489942/

相关文章:

python - ValueError : Expected 2D array, 在拟合模型时得到一维数组

python - 了解机器学习、NLP : Text Classification using scikit-learn, python 和 NLTK

python - LightGBM 错误 : ValueError: For early stopping, 至少需要一个数据集和评估指标进行评估

python - “python manage.py createsuperuser”错误

python - 在 Django 中修改旧的迁移文件是否正确?

python - 我如何有效地将 2d python 列表提供给 C++ 扩展模块

python - 完全矢量化 numpy polyfit

python - numpy 2d boolean 数组计数连续真实大小

Python - 将坐标映射到由 numpy.meshgrid 定义的单元格

python - 将索引和数据放入字典