Skip to content

Instantly share code, notes, and snippets.

@utahka
Created July 26, 2018 11:08
Show Gist options
  • Save utahka/f03c900c750747e93feb336f5d6d7f45 to your computer and use it in GitHub Desktop.
Save utahka/f03c900c750747e93feb336f5d6d7f45 to your computer and use it in GitHub Desktop.
import numpy as np
import scipy.stats
# %matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import mlab
plt.style.use("ggplot")
plt.rcParams["font.size"] = 16
plt.rcParams["figure.figsize"] = 10, 8
from sklearn.mixture import GaussianMixture
np.random.seed(111)
_c = 4 # True number of clusters
n = 1000 # number of samples per cluster
p_t = np.random.uniform(-10, 10, size=(_c, 2))
clusters = []
for x, y in p_t:
c_samples = np.random.normal(0, 2, size=(1000, 2))
c_samples[:, 0] += x
c_samples[:, 1] += y
clusters.append(c_samples)
samples = np.concatenate(clusters)
# Normalization
m = samples.mean(axis=0, keepdims=True)
s = samples.std(axis=0, keepdims=True)
samples = (samples - m) / s
X = samples[:, 0]
Y = samples[:, 1]
n_components = 4
model = GaussianMixture(n_components, covariance_type="full")
model.fit(samples)
plt.scatter(X, Y, marker='+', c='gray')
plt.xlabel('x')
plt.ylabel('y')
x = np.linspace(-3.5, 3.5, 1000)
y = np.linspace(-3.0, 3.0, 1000)
grid0, grid1 = np.meshgrid(x, y)
for k in range(n_components):
plt.plot(model.means_[k][0], model.means_[k][1], 'ro')
means_k = model.means_[k]
covars_k = model.covariances_[k]
grid2 = mlab.bivariate_normal(grid0, grid1,
np.sqrt(covars_k[0][0]), np.sqrt(covars_k[1][1]),
means_k[0], means_k[1],
covars_k[0][1])
plt.contour(grid0, grid1, grid2)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment