kmeans代码实现聚类
时间: 2023-08-25 15:07:48 浏览: 75
以下是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):
self.centroids = X[np.random.choice(range(len(X)), self.n_clusters, replace=False)]
for i in range(self.max_iter):
clusters = [[] for _ in range(self.n_clusters)]
for x in X:
distances = [np.linalg.norm(x-c) for c in self.centroids]
cluster_idx = np.argmin(distances)
clusters[cluster_idx].append(x)
prev_centroids = self.centroids.copy()
for j, cluster in enumerate(clusters):
if len(cluster) == 0:
continue
self.centroids[j] = np.mean(cluster, axis=0)
if np.allclose(prev_centroids, self.centroids):
break
def predict(self, X):
distances = [[np.linalg.norm(x-c) for c in self.centroids] for x in X]
cluster_indices = np.argmin(distances, axis=1)
return cluster_indices
```
使用方法如下:
```python
from sklearn.datasets import make_blobs
from sklearn.metrics import silhouette_score
X, y = make_blobs(n_samples=1000, centers=8, n_features=2, random_state=42)
kmeans = KMeans(n_clusters=8)
kmeans.fit(X)
labels = kmeans.predict(X)
print("Silhouette Score: ", silhouette_score(X, labels))
```
这里使用make_blobs函数生成了一个聚类数据集,并计算了Silhouette Score来评估聚类效果。
阅读全文