我需要一段不引用sklearn对高维数据进行KMeans聚类的python代码
时间: 2023-03-08 22:16:40 浏览: 136
这段代码可以帮助你实现高维数据的KMeans聚类:from numpy import random# 随机生成数据点
data_points = random.rand(number_of_samples, number_of_dimensions)# 随机选择K个中心点
centroids = random.rand(number_of_clusters, number_of_dimensions)# 开始聚类,计算每个数据点到每个中心点的距离
distances = []
for point in data_points:
distances.append([distance.euclidean(point, c) for c in centroids])# 将每个数据点分配到最近的中心点
cluster_assignments = [distances.index(min(distances)) for point in data_points]# 重新计算中心点
centroids = [data_points[cluster_assignments.index(i)].mean(axis=0) for i in range(number_of_clusters)]# 重复上述步骤,直到中心点不再变化
while True:
distances = []
for point in data_points:
distances.append([distance.euclidean(point, c) for c in centroids]) cluster_assignments = [distances.index(min(distances)) for point in data_points] new_centroids = [data_points[cluster_assignments.index(i)].mean(axis=0) for i in range(number_of_clusters)]
if new_centroids == centroids:
break
else:
centroids = new_centroids
阅读全文