Exemplo n.º 1
0
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)
for i in arange(min_gen, max_gen, 0.001):
    plot_real=append(plot_real, array([real_gmm.cluster(array([i]))[3]]))
    plot_est=append(plot_est, array([est_gmm.cluster(array([i]))[3]]))
Exemplo n.º 2
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()