【问题描述】编程实现K-means算法,对平面坐标上的点进行聚类 【输入形式】输入有多行,第1行3个整数,依次是N,M,K,N为点的个数,M为坐标数,K为聚类数,下面N行输入N个点的坐标,最后1行输入K个点的编号,这K个点作为初始的聚类中心 【输出形式】输出有K行,每行输出该类中的点的编号 【样例输入】 11 2 3 2 10 2 5 8 4 5 8 7 5 6 4 1 2 4 9 7 3 1 3 3 9 1 4 7 【样例输出】 cluster1:1,4,8,11 cluster2:3,5,6,9 cluster3:2,7,10
时间: 2024-01-04 19:02:06 浏览: 72
这是一个算法实现问题,需要用 Python 等编程语言进行编写。以下是 Python 代码实现 K-means 算法,可以参考一下:
```python
import numpy as np
def kmeans(data, k, centroids=None):
"""
K-means clustering algorithm
:param data: input data, shape (n_samples, n_features)
:param k: number of clusters
:param centroids: initial centroids, shape (k, n_features)
:return: cluster labels, shape (n_samples,)
"""
n_samples, n_features = data.shape
# Initialize centroids randomly if not given
if centroids is None:
centroids = data[np.random.choice(n_samples, k, replace=False)]
# Initialize cluster labels
labels = np.zeros(n_samples, dtype=int)
while True:
# Assign each data point to nearest centroid
distances = np.linalg.norm(data[:, np.newaxis] - centroids, axis=2)
new_labels = np.argmin(distances, axis=1)
# Check for convergence
if np.array_equal(new_labels, labels):
break
# Update centroids
for i in range(k):
centroids[i] = np.mean(data[new_labels == i], axis=0)
labels = new_labels
return labels
# Example usage
if __name__ == '__main__':
# Read input data
n, m, k = map(int, input().split())
data = np.zeros((n, m))
for i in range(n):
data[i] = list(map(float, input().split()))
centroids = np.array(list(map(int, input().split())))
centroids = data[centroids - 1]
# Run K-means algorithm
labels = kmeans(data, k, centroids)
# Print cluster labels
for i in range(k):
cluster_indices = np.where(labels == i)[0] + 1
print('cluster{}:{}'.format(i+1, ','.join(map(str, cluster_indices))))
```
该程序读入输入数据,包括点的个数、坐标数、聚类数、所有点的坐标和初始聚类中心的编号,然后使用 K-means 算法进行聚类,输出每个聚类中的点的编号。注意,这里使用 numpy 库进行矩阵计算,需要先安装该库。
阅读全文