拟合后,如何从Sklearn GMM中的每个组件中获得标准偏差



拟合后如何获得sklearn gmm中每个组件的标准偏差?

model.fit(dataSet)
model.means_ is the means of each components.
model.weights_ is the co-efficient of each components.

我可以在哪里找到每个高斯组件的偏差?

谢谢,

您可以在协方差矩阵的对角线中获得差异:第一个对角线元素是sigma_x,第二个是sigma_y。

基本上,如果您有N混合物,并且C是您的高斯混合物实例:

cov = C.covariances_
[ np.sqrt(  np.trace(cov[i])/N) for i in range(0,N) ]

将为您提供每种混合物的平均性病偏差。

我在下面的模拟中检查了一下,它似乎将实际值的1%收敛于数百或数千点:

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 24 12:37:38 2019
- - -
Simulate two point - gaussian normalized - distributions.
Use GMM cluster fit and look how covariance elements are related to sigma.

@author: Adrien MAU / ISMO & Abbelight
"""
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import sklearn
from sklearn import cluster, mixture
colorsList = ['c','r','g']
CustomCmap = matplotlib.colors.ListedColormap(colorsList)

sigma1=16
sigma2=4
npoints = 2000
s = (100,100)
x1 = np.random.normal( 50, sigma1, npoints )
y1 = np.random.normal( 70, sigma1, npoints )
x2 = np.random.normal( 20, sigma2, npoints )
y2 = np.random.normal( 50, sigma2, npoints )
x = np.hstack((x1,x2))
y = np.hstack((y1,y2))

C = mixture.GaussianMixture(n_components= 2 , covariance_type='full'  )
subdata = np.transpose( np.vstack((x,y)) )
C.fit( subdata )
m = C.means_
w = C.weights_
cov = C.covariances_

print('n')
print( 'test var 1 : ' , np.sqrt(  np.trace( cov[0]) /2 ) )
print( 'test var 2 : ' , np.sqrt(  np.trace( cov[1]) /2 ) )
plt.scatter(x1,y1)
plt.scatter(x2,y2)
plt.scatter( m[0,0], m[0,1])
plt.scatter( m[1,0], m[1,1])
plt.title('Initial data, and found Centroid')
plt.axis('equal')

gmm_sub_sigmas = [ np.sqrt(  np.trace(cov[i])/2) for i in range(0,2) ]
xdiff= (np.transpose(np.repeat([x],2 ,axis=0)) - m[:,0]) / gmm_sub_sigmas
ydiff= (np.transpose(np.repeat([y],2 ,axis=0)) - m[:,1]) / gmm_sub_sigmas
#            distances = np.hypot(xdiff,ydiff)  #not the effective distance for gaussian distributions...
distances = 0.5*np.hypot(xdiff,ydiff) + np.log(gmm_sub_sigmas)  # I believe this is a good estimate of closeness to a gaussian distribution
res2 = np.argmin( distances , axis=1) 
plt.figure()
plt.scatter(x,y, c=res2, cmap=CustomCmap )
plt.axis('equal')
plt.title('GMM Associated data')

model.covariances_将为您提供协方差信息。

返回协方差取决于covariance_type,这是GMM的参数。

例如,如果 covariance_type = 'diag',返回协方差为[pxq]矩阵,其中 p表示高斯组件的数量,而 q是输入尺寸的数量。

请参阅http://scikit-learn.org/stable/auto_examples/mixture/plot_gmm_covariances.html有关更多信息。