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) est_gmm.train_em(min_cov, max_iter, min_change) est_mean1=est_gmm.get_nth_mean(0) est_mean2=est_gmm.get_nth_mean(1) est_mean3=est_gmm.get_nth_mean(2) est_cov1=est_gmm.get_nth_cov(0) est_cov2=est_gmm.get_nth_cov(1) est_cov3=est_gmm.get_nth_cov(2) est_coef=est_gmm.get_coef() print est_mean1 print est_cov1 print est_mean2 print est_cov2 print est_mean3 print est_cov3 print est_coef min_gen=min(min(generated)) max_gen=max(max(generated)) plot_real=empty(0) plot_est=empty(0)
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) #train GMM using EM est_gmm=GMM(2, cov_type) est_gmm.train(feat_train) est_gmm.train_em(min_cov, max_iter, min_change) #get and print estimated means and covariances est_mean1=est_gmm.get_nth_mean(0) est_mean2=est_gmm.get_nth_mean(1) est_cov1=est_gmm.get_nth_cov(0) est_cov2=est_gmm.get_nth_cov(1) est_coef=est_gmm.get_coef() print est_mean1 print est_cov1 print est_mean2 print est_cov2 print est_coef #plot real GMM, data and estimated GMM min_x_gen=min(min(generated[[0]]))-0.1 max_x_gen=max(max(generated[[0]]))+0.1 min_y_gen=min(min(generated[[1]]))-0.1 max_y_gen=max(max(generated[[1]]))+0.1 plot_real=empty(0)
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) est_gmm.train_em(min_cov, max_iter, min_change) est_mean1 = est_gmm.get_nth_mean(0) est_mean2 = est_gmm.get_nth_mean(1) est_cov1 = est_gmm.get_nth_cov(0) est_cov2 = est_gmm.get_nth_cov(1) est_coef = est_gmm.get_coef() print est_mean1 print est_cov1 print est_mean2 print est_cov2 print est_coef min_x_gen = min(min(generated[[0]])) - 0.1 max_x_gen = max(max(generated[[0]])) + 0.1 min_y_gen = min(min(generated[[1]])) - 0.1 max_y_gen = max(max(generated[[1]])) + 0.1 plot_real = empty(0) plot_est = empty(0)
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()