请手写k-means聚类
时间: 2023-08-24 10:07:06 浏览: 96
好的,下面是手写k-means聚类的Python代码:
```python
import numpy as np
class KMeans:
def __init__(self, n_clusters=8, max_iter=300):
self.n_clusters = n_clusters
self.max_iter = max_iter
def fit(self, X):
n_samples, n_features = X.shape
# Initialize centroids randomly
self.centroids = X[np.random.choice(n_samples, self.n_clusters, replace=False)]
# Iterate until convergence or max iterations reached
for i in range(self.max_iter):
# Assign each sample to closest centroid
distances = np.sqrt(((X - self.centroids[:, np.newaxis])**2).sum(axis=2))
self.labels = np.argmin(distances, axis=0)
# Update centroids based on new assignments
for k in range(self.n_clusters):
self.centroids[k] = X[self.labels == k].mean(axis=0)
# Check for convergence
if i > 0 and np.all(self.labels == prev_labels):
break
prev_labels = self.labels
def predict(self, X):
distances = np.sqrt(((X - self.centroids[:, np.newaxis])**2).sum(axis=2))
return np.argmin(distances, axis=0)
```
这个实现的k-means算法使用了随机初始化的质心,并且在每次迭代中计算每个样本点到每个质心的距离,然后将每个样本点分配到最近的质心。然后,根据新的样本点分配更新质心位置。最终,算法会收敛到一些质心,这些质心用于预测新的样本点所属的聚类。
阅读全文