python的K-means代码
时间: 2023-06-18 14:08:22 浏览: 90
以下是Python实现K-means算法的基本代码:
```python
import numpy as np
import random
# 计算两个向量之间的欧几里得距离
def euclidean_distance(x1, x2):
return np.sqrt(np.sum((x1 - x2)**2))
class KMeans:
def __init__(self, K=2, max_iters=100, plot_steps=False):
self.K = K
self.max_iters = max_iters
self.plot_steps = plot_steps
# 初始化聚类中心
self.centroids = []
# 每个数据点所属的簇
self.clusters = [[] for _ in range(self.K)]
def predict(self, X):
self.X = X
self.n_samples, self.n_features = X.shape
# 随机选择K个样本作为初始聚类中心
random_sample_idxs = random.sample(range(self.n_samples), self.K)
self.centroids = [self.X[idx] for idx in random_sample_idxs]
# 开始迭代
for _ in range(self.max_iters):
self.clusters = self._create_clusters(self.centroids)
if self.plot_steps:
self._plot()
old_centroids = self.centroids
self.centroids = self._get_centroids(self.clusters)
# 如果聚类中心不再变化,停止迭代
if self._is_converged(old_centroids, self.centroids):
break
if self.plot_steps:
self._plot()
# 返回每个数据点所属的簇
return self._get_cluster_labels(self.clusters)
def _create_clusters(self, centroids):
clusters = [[] for _ in range(self.K)]
for idx, sample in enumerate(self.X):
centroid_idx = self._closest_centroid(sample, centroids)
clusters[centroid_idx].append(idx)
return clusters
def _closest_centroid(self, sample, centroids):
distances = [euclidean_distance(sample, point) for point in centroids]
closest_idx = np.argmin(distances)
return closest_idx
def _get_centroids(self, clusters):
centroids = np.zeros((self.K, self.n_features))
for idx, cluster in enumerate(clusters):
cluster_mean = np.mean(self.X[cluster], axis=0)
centroids[idx] = cluster_mean
return centroids
def _is_converged(self, old_centroids, centroids):
distances = [euclidean_distance(old_centroids[i], centroids[i]) for i in range(self.K)]
return sum(distances) == 0
def _get_cluster_labels(self, clusters):
labels = np.empty(self.n_samples)
for cluster_idx, cluster in enumerate(clusters):
for sample_idx in cluster:
labels[sample_idx] = cluster_idx
return labels
def _plot(self):
fig, ax = plt.subplots(figsize=(12, 8))
for i, index in enumerate(self.clusters):
point = self.X[index].T
ax.scatter(*point)
for point in self.centroids:
ax.scatter(*point, marker="x", color="black", linewidth=2)
plt.show()
```
使用示例:
```python
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
X, y = make_blobs(centers=3, n_samples=500, random_state=42)
kmeans = KMeans(K=3, max_iters=150, plot_steps=True)
y_pred = kmeans.predict(X)
fig, ax = plt.subplots(figsize=(12, 8))
for i in range(len(X)):
ax.scatter(X[i][0], X[i][1], color='C{}'.format(y_pred[i]))
plt.show()
```
阅读全文