高斯混合聚类python实现
时间: 2023-06-29 17:17:20 浏览: 149
基于python的高斯混合模型(GMM 聚类)的 EM 算法实现
5星 · 资源好评率100%
好的,下面是高斯混合聚类的Python实现代码:
```python
import numpy as np
from scipy.stats import multivariate_normal
class GaussianMixture:
def __init__(self, n_components, max_iter=100, tol=1e-3):
self.n_components = n_components
self.max_iter = max_iter
self.tol = tol
def _init_params(self, X):
self.n_samples, self.n_features = X.shape
self.weights = np.ones(self.n_components) / self.n_components
self.means = X[np.random.choice(self.n_samples, self.n_components, replace=False)]
self.covs = np.array([np.eye(self.n_features)] * self.n_components)
def _e_step(self, X):
self.responsibilities = np.zeros((self.n_samples, self.n_components))
for k in range(self.n_components):
self.responsibilities[:, k] = self.weights[k] * multivariate_normal.pdf(X, self.means[k], self.covs[k])
self.responsibilities /= np.sum(self.responsibilities, axis=1, keepdims=True)
def _m_step(self, X):
Nk = np.sum(self.responsibilities, axis=0)
self.weights = Nk / self.n_samples
self.means = np.dot(self.responsibilities.T, X) / Nk.reshape(-1, 1)
for k in range(self.n_components):
diff = X - self.means[k]
self.covs[k] = np.dot(self.responsibilities[:, k] * diff.T, diff) / Nk[k]
def fit(self, X):
self._init_params(X)
for i in range(self.max_iter):
weights_prev = self.weights.copy()
self._e_step(X)
self._m_step(X)
if np.allclose(self.weights, weights_prev, atol=self.tol):
break
```
其中,`n_components`是高斯混合模型的成分数,`max_iter`是最大迭代次数,`tol`是收敛阈值。`_init_params`函数用于初始化模型参数,`_e_step`函数用于进行E步,计算每个样本属于每个成分的后验概率,`_m_step`函数用于进行M步,更新模型参数。`fit`函数用于训练模型,输入数据`X`,并进行迭代优化,直到满足收敛条件为止。
阅读全文