-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathgaussian_mixture_model.py
More file actions
295 lines (247 loc) · 9.65 KB
/
gaussian_mixture_model.py
File metadata and controls
295 lines (247 loc) · 9.65 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""
README, Author - Md Ruman Islam (mailto:ruman23.github.io)
Requirements:
- numpy
- matplotlib
Python:
- 3.8+
Inputs:
- data : a 2D numpy array of features.
- n_components : number of Gaussian distributions (clusters) to fit.
- max_iter : maximum number of EM iterations.
- tol : convergence tolerance.
Usage:
1. define 'n_components' value and 'data' features array
2. initialize model:
gmm = GaussianMixture(n_components=3, max_iter=100)
3. fit model to data:
gmm.fit(data)
4. get cluster predictions:
labels = gmm.predict(data)
5. visualize results:
gmm.plot_results(data)
"""
import warnings
from typing import TypeAlias
import matplotlib.pyplot as plt
import numpy as np
from numpy.typing import NDArray
from scipy.stats import multivariate_normal
warnings.filterwarnings("ignore")
TAG = "GAUSSIAN-MIXTURE/ "
FloatArray: TypeAlias = NDArray[np.float64]
class GaussianMixture:
"""
Gaussian Mixture Model implemented using the Expectation-Maximization algorithm.
"""
def __init__(
self,
n_components: int = 2,
max_iter: int = 100,
tol: float = 1e-4,
seed: int | None = None,
) -> None:
self.n_components: int = n_components
self.max_iter: int = max_iter
self.tol: float = tol
self.seed: int | None = seed
# parameters
self.weights_: FloatArray | None = None
self.means_: FloatArray | None = None
self.covariances_: NDArray[np.float64] | None = None
self.log_likelihoods_: list[float] = []
def _initialize_parameters(self, data: FloatArray) -> None:
"""Randomly initialize means, covariances, and mixture weights.
Examples
--------
>>> sample = np.array(
... [[0.0, 0.5], [1.0, 1.5], [2.0, 2.5], [3.0, 3.5]]
... )
>>> model = GaussianMixture(n_components=2, seed=0)
>>> model._initialize_parameters(sample)
>>> model.means_.shape
(2, 2)
>>> bool(np.isclose(model.weights_.sum(), 1.0))
True
"""
rng = np.random.default_rng(self.seed)
n_samples, _ = data.shape
indices = rng.choice(n_samples, self.n_components, replace=False)
self.means_ = data[indices]
identity = np.eye(data.shape[1]) * 1e-6
self.covariances_ = np.array(
[np.cov(data, rowvar=False) + identity for _ in range(self.n_components)]
)
self.weights_ = np.ones(self.n_components) / self.n_components
def _e_step(self, data: FloatArray) -> FloatArray:
"""Compute responsibilities (posterior probabilities).
Examples
--------
>>> sample = np.array(
... [[0.0, 0.5], [1.0, 1.5], [2.0, 2.5], [3.0, 3.5]]
... )
>>> model = GaussianMixture(n_components=2, seed=0)
>>> model._initialize_parameters(sample)
>>> resp = model._e_step(sample)
>>> resp.shape
(4, 2)
>>> bool(np.allclose(resp.sum(axis=1), 1.0))
True
"""
if self.weights_ is None or self.means_ is None or self.covariances_ is None:
raise ValueError(
"Model parameters must be initialized before running the E-step."
)
n_samples = data.shape[0]
responsibilities = np.zeros((n_samples, self.n_components))
weights = self.weights_
means = self.means_
covariances = self.covariances_
for k in range(self.n_components):
rv = multivariate_normal(
mean=means[k], cov=covariances[k], allow_singular=True
)
responsibilities[:, k] = weights[k] * rv.pdf(data)
# Normalize to get probabilities
responsibilities /= responsibilities.sum(axis=1, keepdims=True)
return responsibilities
def _m_step(self, data: FloatArray, responsibilities: FloatArray) -> None:
"""Update weights, means, and covariances.
Note: assumes the model parameters are already initialized.
Examples
--------
>>> sample = np.array(
... [[0.0, 0.5], [1.0, 1.5], [2.0, 2.5], [3.0, 3.5]]
... )
>>> model = GaussianMixture(n_components=2, seed=0)
>>> model._initialize_parameters(sample)
>>> resp = model._e_step(sample)
>>> model._m_step(sample, resp)
>>> bool(np.isclose(model.weights_.sum(), 1.0))
True
"""
n_samples, n_features = data.shape
component_counts = responsibilities.sum(axis=0)
self.weights_ = component_counts / n_samples
self.means_ = (responsibilities.T @ data) / component_counts[:, np.newaxis]
if self.covariances_ is None or self.means_ is None:
raise ValueError(
"Model parameters must be initialized before running the M-step."
)
covariances = self.covariances_
means = self.means_
for k in range(self.n_components):
diff = data - means[k]
covariances[k] = (responsibilities[:, k][:, np.newaxis] * diff).T @ diff
covariances[k] /= component_counts[k]
# Add small regularization term for numerical stability
covariances[k] += np.eye(n_features) * 1e-6
def _compute_log_likelihood(self, data: FloatArray) -> float:
"""Compute total log-likelihood of the model.
Note: assumes the model parameters are already initialized.
Examples
--------
>>> sample = np.array(
... [[0.0, 0.5], [1.0, 1.5], [2.0, 2.5], [3.0, 3.5]]
... )
>>> model = GaussianMixture(n_components=2, seed=0)
>>> model._initialize_parameters(sample)
>>> bool(np.isfinite(model._compute_log_likelihood(sample)))
True
"""
if self.weights_ is None or self.means_ is None or self.covariances_ is None:
raise ValueError(
"Model parameters must be initialized before computing likelihood."
)
n_samples = data.shape[0]
total_pdf = np.zeros((n_samples, self.n_components))
weights = self.weights_
means = self.means_
covariances = self.covariances_
for k in range(self.n_components):
rv = multivariate_normal(
mean=means[k], cov=covariances[k], allow_singular=True
)
total_pdf[:, k] = weights[k] * rv.pdf(data)
log_likelihood = np.sum(np.log(np.sum(total_pdf, axis=1) + 1e-12))
return log_likelihood
def fit(self, data: FloatArray) -> None:
"""Fit the Gaussian Mixture Model to data using the EM algorithm.
Examples
--------
>>> sample = np.array(
... [[0.0, 0.5], [1.0, 1.5], [2.0, 2.5], [3.0, 3.5]]
... )
>>> model = GaussianMixture(n_components=2, max_iter=5, tol=1e-3, seed=0)
>>> model.fit(sample) # doctest: +ELLIPSIS
GAUSSIAN-MIXTURE/ ...
>>> len(model.log_likelihoods_) > 0
True
"""
self._initialize_parameters(data)
prev_log_likelihood = None
for i in range(self.max_iter):
# E-step
responsibilities = self._e_step(data)
# M-step
self._m_step(data, responsibilities)
# Log-likelihood
log_likelihood = self._compute_log_likelihood(data)
self.log_likelihoods_.append(log_likelihood)
if (
prev_log_likelihood is not None
and abs(log_likelihood - prev_log_likelihood) < self.tol
):
print(f"{TAG}Converged at iteration {i}.")
break
prev_log_likelihood = log_likelihood
print(f"{TAG}Training complete. Final log-likelihood: {log_likelihood:.4f}")
def predict(self, data: FloatArray) -> NDArray[np.int_]:
"""Predict cluster assignment for each data point.
Note: assumes the model parameters are already initialized.
Examples
--------
>>> sample = np.array(
... [[0.0, 0.5], [1.0, 1.5], [2.0, 2.5], [3.0, 3.5]]
... )
>>> model = GaussianMixture(n_components=2, max_iter=5, tol=1e-3, seed=0)
>>> model.fit(sample) # doctest: +ELLIPSIS
GAUSSIAN-MIXTURE/ ...
>>> labels = model.predict(sample)
>>> labels.shape
(4,)
"""
responsibilities = self._e_step(data)
return np.argmax(responsibilities, axis=1)
def plot_results(self, data: FloatArray) -> None:
"""Visualize GMM clustering results (2D only).
Note: This method assumes self.means_ is initialized.
Examples
--------
>>> sample = np.ones((3, 3))
>>> model = GaussianMixture()
>>> model.plot_results(sample)
GAUSSIAN-MIXTURE/ Plotting only supported for 2D data.
"""
if data.shape[1] != 2:
print(f"{TAG}Plotting only supported for 2D data.")
return
labels = self.predict(data)
if self.means_ is None:
raise ValueError("Model means must be initialized before plotting.")
plt.scatter(data[:, 0], data[:, 1], c=labels, cmap="viridis", s=30)
plt.scatter(self.means_[:, 0], self.means_[:, 1], c="red", s=100, marker="x")
plt.title("Gaussian Mixture Model Clustering")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.show()
# Mock test
if __name__ == "__main__":
from sklearn.datasets import make_blobs
sample_data, _ = make_blobs(
n_samples=300, centers=3, cluster_std=1.2, random_state=42
)
gmm = GaussianMixture(n_components=3, max_iter=100, seed=42)
gmm.fit(sample_data)
labels = gmm.predict(sample_data)
gmm.plot_results(sample_data)