导入必要的库

import numpy as np from sklearn.cluster import KMeans

定义数据

X = np.array([[1], [2], [4], [5]])

定义kmeans模型,设置聚类数量为2

kmeans = KMeans(n_clusters=2, random_state=0)

拟合数据

kmeans.fit(X)

输出聚类标签和聚类中心

print(kmeans.labels_) print(kmeans.cluster_centers_)

Python KMeans 聚类算法示例:将餐厅客户分类为 VIP 和普通客户

原文地址: https://www.cveoy.top/t/topic/ozTe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录