def clustering_gmm_modular(fm_train=generated, n=2, min_cov=1e-9, max_iter=1000, min_change=1e-9, cov_type=0): from shogun.Distribution import GMM from shogun.Features import RealFeatures from shogun.Library import Math_init_random Math_init_random(5) feat_train = RealFeatures(generated) est_gmm = GMM(n, cov_type) est_gmm.train(feat_train) est_gmm.train_em(min_cov, max_iter, min_change) return est_gmm
def clustering_gmm_modular (fm_train=generated,n=2,min_cov=1e-9,max_iter=1000,min_change=1e-9,cov_type=0): from shogun.Distribution import GMM from shogun.Features import RealFeatures from shogun.Library import Math_init_random Math_init_random(5) feat_train=RealFeatures(generated) est_gmm=GMM(n, cov_type) est_gmm.train(feat_train) est_gmm.train_em(min_cov, max_iter, min_change) return est_gmm
from pylab import figure, scatter, contour, show, legend, connect from numpy import array, append, arange, reshape, empty from shogun.Distribution import Gaussian, GMM from shogun.Features import RealFeatures import util util.set_title('SMEM for 2d GMM example') max_iter = 100 max_cand = 5 min_cov = 1e-9 max_em_iter = 1000 min_change = 1e-9 cov_type = 0 real_gmm = GMM(3) real_gmm.set_nth_mean(array([2.0, 2.0]), 0) real_gmm.set_nth_mean(array([-2.0, -2.0]), 1) real_gmm.set_nth_mean(array([2.0, -2.0]), 2) real_gmm.set_nth_cov(array([[1.0, 0.2], [0.2, 0.5]]), 0) real_gmm.set_nth_cov(array([[0.2, 0.1], [0.1, 0.5]]), 1) real_gmm.set_nth_cov(array([[0.3, -0.2], [-0.2, 0.8]]), 2) real_gmm.set_coef(array([0.3, 0.4, 0.3])) generated = array([real_gmm.sample()]) for i in range(199): generated = append(generated, array([real_gmm.sample()]), axis=0)
from pylab import figure,show,connect,hist,plot,legend from numpy import array, append, arange, empty from shogun.Distribution import Gaussian, GMM from shogun.Features import RealFeatures import util util.set_title('EM for 1d GMM example') min_cov=1e-9 max_iter=1000 min_change=1e-9 real_gmm=GMM(3) real_gmm.set_nth_mean(array([-2.0]), 0) real_gmm.set_nth_mean(array([0.0]), 1) real_gmm.set_nth_mean(array([2.0]), 2) real_gmm.set_nth_cov(array([[0.3]]), 0) real_gmm.set_nth_cov(array([[0.1]]), 1) real_gmm.set_nth_cov(array([[0.2]]), 2) real_gmm.set_coef(array([0.3, 0.5, 0.2])) generated=array([real_gmm.sample()]) for i in range(199): generated=append(generated, array([real_gmm.sample()]), axis=1) feat_train=RealFeatures(generated) est_gmm=GMM(3) est_gmm.train(feat_train)
from pylab import figure,show,connect,hist,plot,legend from numpy import array, append, arange, empty from shogun.Distribution import Gaussian, GMM from shogun.Features import RealFeatures import util util.set_title('SMEM for 1d GMM example') max_iter=100 max_cand=5 min_cov=1e-9 max_em_iter=1000 min_change=1e-9 real_gmm=GMM(3) real_gmm.set_nth_mean(array([-2.0]), 0) real_gmm.set_nth_mean(array([0.0]), 1) real_gmm.set_nth_mean(array([2.0]), 2) real_gmm.set_nth_cov(array([[0.3]]), 0) real_gmm.set_nth_cov(array([[0.1]]), 1) real_gmm.set_nth_cov(array([[0.2]]), 2) real_gmm.set_coef(array([0.3, 0.5, 0.2])) generated=array([real_gmm.sample()]) for i in range(199): generated=append(generated, array([real_gmm.sample()]), axis=1) feat_train=RealFeatures(generated)
from pylab import figure, scatter, contour, show, legend, connect from numpy import array, append, arange, reshape, empty from shogun.Distribution import Gaussian, GMM from shogun.Features import RealFeatures import util util.set_title('EM for 2d GMM example') min_cov = 1e-9 max_iter = 1000 min_change = 1e-9 cov_type = 0 real_gmm = GMM(2) real_gmm.set_nth_mean(array([1.0, 1.0]), 0) real_gmm.set_nth_mean(array([-1.0, -1.0]), 1) real_gmm.set_nth_cov(array([[1.0, 0.2], [0.2, 0.1]]), 0) real_gmm.set_nth_cov(array([[0.3, 0.1], [0.1, 1.0]]), 1) real_gmm.set_coef(array([0.3, 0.7])) generated = array([real_gmm.sample()]) for i in range(199): generated = append(generated, array([real_gmm.sample()]), axis=0) generated = generated.transpose() feat_train = RealFeatures(generated) est_gmm = GMM(2, cov_type) est_gmm.train(feat_train)
from shogun.Distribution import Gaussian, GMM from shogun.Features import RealFeatures import util util.set_title('SMEM for 2d GMM example') #set the parameters max_iter=100 max_cand=5 min_cov=1e-9 max_em_iter=1000 min_change=1e-9 cov_type=0 #setup the real GMM real_gmm=GMM(3) real_gmm.set_nth_mean(array([2.0, 2.0]), 0) real_gmm.set_nth_mean(array([-2.0, -2.0]), 1) real_gmm.set_nth_mean(array([2.0, -2.0]), 2) real_gmm.set_nth_cov(array([[1.0, 0.2],[0.2, 0.5]]), 0) real_gmm.set_nth_cov(array([[0.2, 0.1],[0.1, 0.5]]), 1) real_gmm.set_nth_cov(array([[0.3, -0.2],[-0.2, 0.8]]), 2) real_gmm.set_coef(array([0.3, 0.4, 0.3])) #generate training set from real GMM generated=array([real_gmm.sample()]) for i in range(199): generated=append(generated, array([real_gmm.sample()]), axis=0)
from numpy import array, append from shogun.Distribution import GMM from shogun.Library import Math_init_random Math_init_random(5) real_gmm = GMM(2, 0) real_gmm.set_nth_mean(array([1.0, 1.0]), 0) real_gmm.set_nth_mean(array([-1.0, -1.0]), 1) real_gmm.set_nth_cov(array([[1.0, 0.2], [0.2, 0.1]]), 0) real_gmm.set_nth_cov(array([[0.3, 0.1], [0.1, 1.0]]), 1) real_gmm.set_coef(array([0.3, 0.7])) generated = array([real_gmm.sample()]) for i in range(199): generated = append(generated, array([real_gmm.sample()]), axis=0) generated = generated.transpose() parameter_list = [[generated, 2, 1e-9, 1000, 1e-9, 0]] def clustering_gmm_modular(fm_train=generated, n=2, min_cov=1e-9, max_iter=1000, min_change=1e-9, cov_type=0):
# minimum covariance, maximum iterations and minimum log-likelihood change as # input. After training one can cluster observations by selecting the most # likely cluster to have generated the observation. # ##!/usr/bin/env python # """ # Explicit examples on how to use clustering # """ from numpy import array, append from shogun.Distribution import GMM from shogun.Library import Math_init_random Math_init_random(5) real_gmm = GMM(2, 0) real_gmm.set_nth_mean(array([1.0, 1.0]), 0) real_gmm.set_nth_mean(array([-1.0, -1.0]), 1) real_gmm.set_nth_cov(array([[1.0, 0.2], [0.2, 0.1]]), 0) real_gmm.set_nth_cov(array([[0.3, 0.1], [0.1, 1.0]]), 1) real_gmm.set_coef(array([0.3, 0.7])) generated = array([real_gmm.sample()]) for i in range(199): generated = append(generated, array([real_gmm.sample()]), axis=0) generated = generated.transpose()
from pylab import figure,scatter,contour,show,legend,connect from numpy import array, append, arange, reshape, empty, exp from shogun.Distribution import Gaussian, GMM from shogun.Features import RealFeatures import util util.set_title('EM for 2d GMM example') #set the parameters min_cov=1e-9 max_iter=1000 min_change=1e-9 cov_type=0 #setup the real GMM real_gmm=GMM(2) real_gmm.set_nth_mean(array([1.0, 1.0]), 0) real_gmm.set_nth_mean(array([-1.0, -1.0]), 1) real_gmm.set_nth_cov(array([[1.0, 0.2],[0.2, 0.1]]), 0) real_gmm.set_nth_cov(array([[0.3, 0.1],[0.1, 1.0]]), 1) real_gmm.set_coef(array([0.3, 0.7])) #generate training set from real GMM generated=array([real_gmm.sample()]) for i in range(199): generated=append(generated, array([real_gmm.sample()]), axis=0) generated=generated.transpose()
from pylab import figure, show, connect, hist, plot, legend from numpy import array, append, arange, empty from shogun.Distribution import Gaussian, GMM from shogun.Features import RealFeatures import util util.set_title('SMEM for 1d GMM example') max_iter = 100 max_cand = 5 min_cov = 1e-9 max_em_iter = 1000 min_change = 1e-9 real_gmm = GMM(3) real_gmm.set_nth_mean(array([-2.0]), 0) real_gmm.set_nth_mean(array([0.0]), 1) real_gmm.set_nth_mean(array([2.0]), 2) real_gmm.set_nth_cov(array([[0.3]]), 0) real_gmm.set_nth_cov(array([[0.1]]), 1) real_gmm.set_nth_cov(array([[0.2]]), 2) real_gmm.set_coef(array([0.3, 0.5, 0.2])) generated = array([real_gmm.sample()]) for i in range(199): generated = append(generated, array([real_gmm.sample()]), axis=1) feat_train = RealFeatures(generated)
import numpy as np import os from shogun.Features import RealFeatures from shogun.Distribution import GMM from shogun.Library import Math_init_random # Load the data. f = open(os.path.dirname(__file__) + '../data/mvnrnd.data') data = np.fromfile(f, dtype=np.float64, sep=' ') data = data.reshape(-1, 2) f.close() Math_init_random(5) feat = RealFeatures(data.T) # Calculate mixture of Gaussians. gmm = GMM(2, 0) gmm.set_features(feat) gmm.train_em() # Vector of covariances; one for each Gaussian. print gmm.get_nth_cov(0) print gmm.get_nth_cov(1) # The vector of means. print gmm.get_nth_mean(0) print gmm.get_nth_mean(1) # The a priori weights of each Gaussian. print gmm.get_coef()