-
Notifications
You must be signed in to change notification settings - Fork 8
/
estimate_gmm_sklearn.py
44 lines (39 loc) · 1.46 KB
/
estimate_gmm_sklearn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
import visualization
from sklearn.mixture import GaussianMixture
## Generate synthetic data
N,D = 1000, 2 # number of points and dimenstinality
if D == 2:
#set gaussian ceters and covariances in 2D
means = np.array([[0.5, 0.0],
[0, 0],
[-0.5, -0.5],
[-0.8, 0.3]])
covs = np.array([np.diag([0.01, 0.01]),
np.diag([0.025, 0.01]),
np.diag([0.01, 0.025]),
np.diag([0.01, 0.01])])
elif D == 3:
# set gaussian ceters and covariances in 3D
means = np.array([[0.5, 0.0, 0.0],
[0.0, 0.0, 0.0],
[-0.5, -0.5, -0.5],
[-0.8, 0.3, 0.4]])
covs = np.array([np.diag([0.01, 0.01, 0.03]),
np.diag([0.08, 0.01, 0.01]),
np.diag([0.01, 0.05, 0.01]),
np.diag([0.03, 0.07, 0.01])])
n_gaussians = means.shape[0]
points = []
for i in range(len(means)):
x = np.random.multivariate_normal(means[i], covs[i], N )
points.append(x)
points = np.concatenate(points)
#fit the gaussian model
gmm = GaussianMixture(n_components=n_gaussians, covariance_type='diag')
gmm.fit(points)
#visualize
if D == 2:
visualization.visualize_2D_gmm(points, gmm.weights_, gmm.means_.T, np.sqrt(gmm.covariances_).T)
elif D == 3:
visualization.visualize_3d_gmm(points, gmm.weights_, gmm.means_.T, np.sqrt(gmm.covariances_).T)