Skip to content
Snippets Groups Projects
Commit 53faf17f authored by Yannick DAYER's avatar Yannick DAYER
Browse files

[doc] Plot code example of GMM

parent e47b5733
No related branches found
No related tags found
2 merge requests!42GMM implementation in Python,!40Transition to a pure python implementation
import bob.learn.em from bob.learn.em.mixture import MLGMMTrainer
from bob.learn.em.mixture import GMMMachine
from bob.learn.em.mixture import Gaussian
import bob.db.iris import bob.db.iris
import numpy import numpy
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import logging
logger = logging.getLogger("bob.learn.em")
logger.setLevel("DEBUG")
data_per_class = bob.db.iris.data() data_per_class = bob.db.iris.data()
setosa = numpy.column_stack( setosa = numpy.column_stack(
(data_per_class['setosa'][:, 0], data_per_class['setosa'][:, 3])) (data_per_class['setosa'][:, 0], data_per_class['setosa'][:, 3]))
...@@ -14,24 +22,55 @@ virginica = numpy.column_stack( ...@@ -14,24 +22,55 @@ virginica = numpy.column_stack(
data = numpy.vstack((setosa, versicolor, virginica)) data = numpy.vstack((setosa, versicolor, virginica))
# Two clusters with a feature dimensionality of 3 # Two clusters with a feature dimensionality of 3
machine = bob.learn.em.GMMMachine(3, 2) machine = GMMMachine(3, convergence_threshold=1e-5)
trainer = bob.learn.em.ML_GMMTrainer(True, True, True)
machine.means = numpy.array([[5, 3], [4, 2], [7, 3.]]) init_means = numpy.array([[5, 3], [4, 2], [7, 3]], dtype=float)
bob.learn.em.train(trainer, machine, data, max_iterations=200, gaussians = numpy.array([Gaussian(m) for m in init_means])
convergence_threshold=1e-5) # Train the KMeansMachine trainer = MLGMMTrainer(
init_method=gaussians,
figure, ax = plt.subplots() update_means=True,
plt.scatter(setosa[:, 0], setosa[:, 1], c="darkcyan", label="setosa") update_variances=True,
plt.scatter(versicolor[:, 0], versicolor[:, 1], update_weights=True,
c="goldenrod", label="versicolor") )
plt.scatter(virginica[:, 0], virginica[:, 1],
c="dimgrey", label="virginica") trainer.initialize(machine, data)
plt.scatter(machine.means[:, 0], repeat = 8
machine.means[:, 1], c="blue", marker="x", label="centroids", s=60) for step in range(10):
plt.legend() print(f"Step {step*repeat} through {step*repeat+repeat} out of {10*repeat}.")
plt.xticks([], []) for r in range(repeat):
plt.yticks([], []) print(f"Step {step*repeat+r}")
ax.set_xlabel("Sepal length") machine = machine.fit_partial(data, trainer=trainer)
ax.set_ylabel("Petal width")
plt.tight_layout() figure, ax = plt.subplots()
plt.show() ax.scatter(setosa[:, 0], setosa[:, 1], c="darkcyan", label="setosa")
ax.scatter(versicolor[:, 0], versicolor[:, 1],
c="goldenrod", label="versicolor")
ax.scatter(virginica[:, 0], virginica[:, 1],
c="dimgrey", label="virginica")
ax.scatter(
machine.gaussians_["mean"][:, 0],
machine.gaussians_["mean"][:, 1],
c="blue", marker="x", label="centroids", s=60
)
for g in machine.gaussians_:
covariance = numpy.diag(g["variance"])
radius = numpy.sqrt(5.991)
eigvals, eigvecs = numpy.linalg.eig(covariance)
axis = numpy.sqrt(eigvals) * radius
slope = eigvecs[1][0] / eigvecs[1][1]
angle = 180.0 * numpy.arctan(slope) / numpy.pi
e1 = Ellipse(
g["mean"], axis[0], axis[1],
angle=angle, linewidth=1, fill=False, zorder=2
)
ax.add_patch(e1)
plt.legend()
plt.xticks([], [])
plt.yticks([], [])
ax.set_xlabel("Sepal length")
ax.set_ylabel("Petal width")
plt.tight_layout()
plt.show()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment