实现三支k-means
时间: 2024-04-29 07:23:03 浏览: 107
k-means实现
以下是三支k-means的Python代码实现:
1. 基础k-means算法
```
import numpy as np
def kmeans(X, k, max_iterations=100):
# 随机选择k个中心点
centroids = X[np.random.choice(range(len(X)), k, replace=False)]
for i in range(max_iterations):
# 计算每个样本到中心点的距离
distances = np.sqrt(((X - centroids[:, np.newaxis])**2).sum(axis=2))
# 将每个样本分配到最近的中心点
labels = np.argmin(distances, axis=0)
# 计算新的中心点位置
new_centroids = np.array([X[labels == j].mean(axis=0) for j in range(k)])
# 如果中心点位置没有变化,则退出循环
if np.allclose(new_centroids, centroids):
break
centroids = new_centroids
return labels, centroids
```
2. 带权重的k-means算法
```
import numpy as np
def weighted_kmeans(X, k, weights, max_iterations=100):
# 根据权重随机选择k个中心点
centroids = X[np.random.choice(range(len(X)), k, replace=False, p=weights)]
for i in range(max_iterations):
# 计算每个样本到中心点的距离
distances = np.sqrt(((X - centroids[:, np.newaxis])**2).sum(axis=2))
# 将每个样本分配到最近的中心点
labels = np.argmin(distances, axis=0)
# 计算新的中心点位置
new_centroids = np.array([np.average(X[labels == j], axis=0, weights=weights[labels == j]) for j in range(k)])
# 如果中心点位置没有变化,则退出循环
if np.allclose(new_centroids, centroids):
break
centroids = new_centroids
return labels, centroids
```
3. 带距离权重的k-means算法
```
import numpy as np
def distance_weighted_kmeans(X, k, max_iterations=100):
# 随机选择k个中心点
centroids = X[np.random.choice(range(len(X)), k, replace=False)]
for i in range(max_iterations):
# 计算每个样本到中心点的距离
distances = np.sqrt(((X - centroids[:, np.newaxis])**2).sum(axis=2))
# 计算距离权重
distance_weights = 1 / (distances + 0.001)
distance_weights /= distance_weights.sum(axis=0)
# 将每个样本分配到最近的中心点
labels = np.argmin(distances * distance_weights, axis=0)
# 计算新的中心点位置
new_centroids = np.array([np.average(X[labels == j], axis=0, weights=distance_weights[:, j]) for j in range(k)])
# 如果中心点位置没有变化,则退出循环
if np.allclose(new_centroids, centroids):
break
centroids = new_centroids
return labels, centroids
```
阅读全文