import matplotlib.pyplot as plt
import numpy as np
from sklearn.mixture import GaussianMixture

from generator.sample_generator import generator_multivariate_normal
from robustEM import rEM
from visualization import visualization_2d as vs2
from visualization import plot

# Data Generate
means = [[.0, .0], [.0, .0], [-1.5, 1.5], [1.5, 1.5], [0, -2]]
covs = [[[.01, .0], [.0, 1.25]], [[8, .0], [.0, 8]], [[.2, .0], [.0, .015]],
        [[.2, .0], [.0, .015]], [[1., .0], [.0, .2]]]
mix_prob = [.2, .2, .2, .2, .2]

ex5 = generator_multivariate_normal(means=means, covs=covs, mix_prob=mix_prob)

X = ex5.get_sample(1000)

# Real
means_real = ex5.means_
covs_real = ex5.covs_

fig, ax = plt.subplots(1, 1)
vs2.scatter_sample(ax, X, 'Real Data and Real Gaussian Distribution')
vs2.get_figure(ax, means_real, covs_real, 'b', 'b')

plt.savefig('../plot/example5_1.png', dpi=300)

# Standard EM with initial values
init_idx = np.random.choice(np.arange(X.shape[0]), 5)
Esempio n. 2
0
         [0, 10, 8],
         [5, 10, 8]]

covs = [[[.5, 0, 0], [0, 2, 0], [0, 0, 1]],
        [[1, 0, 0], [0, 2, 0], [0, 0, 1]],
        [[.5, 0, 0], [0, 2, 0], [0, 0, 1]],
        [[.5, 0, 0], [0, 8, 0], [0, 0, 2]],
        [[1, 0, 0], [0, 8, 0], [0, 0, 2]],
        [[.5, 0, 0], [0, 8, 0], [0, 0, 2]],
        [[.5, 0, 0], [0, 2, 0], [0, 0, 1]],
        [[1, 0, 0], [0, 2, 0], [0, 0, 1]],
        [[.5, 0, 0], [0, 2, 0], [0, 0, 1]]]

mix_prob = [1/16, 2/16, 1/16, 2/16, 4/16, 2/16, 1/16, 2/16, 1/16]

ex7 = generator_multivariate_normal(means, covs, mix_prob)
X = ex7.get_sample(1600)

# robustEM
rem = rEM.robustEM()
rem.fit(X)
record = rem.save_record(save_option=True, filename='example7')

c_pred = rem.predict(X)
c_list = np.unique(c_pred)

# visualization
fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_subplot(121, projection='3d')
ax2 = fig.add_subplot(122, projection='3d')
ax1.scatter(X[:, 0], X[:, 1], X[:, 2], color='g')