import numpy as np
import matplotlib.pyplot as plt def kmeans(data, cluster_num, method='mean'):
points = np.array(data)
labels = []
random_idx = []
while True:
t = np.random.randint(points.shape[0])
if t not in random_idx:
random_idx.append(t)
if len(random_idx) == cluster_num:
break seeds = []
for i in range(cluster_num):
seeds.append(points[random_idx[i]])
seeds = np.array(seeds)
prev_seeds = seeds + 10
colors = ["red", "blue", "yellow", "cyan", "purple"]
points_labeled = []
for i in range(cluster_num):
points_labeled.append([]) while sum(abs((prev_seeds - seeds).ravel())) > 0:
prev_seeds = seeds.copy()
for i in range(points.shape[0]):
t_min = 10000
for j in range(seeds.shape[0]):
t_current = np.sqrt((points[i][0] - seeds[j][0]) ** 2 + (points[i][1] - seeds[j][1]) ** 2)
if t_current < t_min:
t_min = t_current
try:
labels.pop(i)
except IndexError:
pass
labels.append(j)
for i in range(len(labels)):
points_labeled[labels[i]].append(points[i].tolist())
points_labeled = np.array(points_labeled)
for i in range(points_labeled.shape[0]):
if points_labeled[i]:
plt.scatter(np.array(points_labeled[i])[:, 1], np.array(points_labeled[i])[:, 0], c=colors[i])
for i in seeds:
plt.scatter(i[1], i[0], c="black", linewidths=3)
plt.show()
for i in range(seeds.shape[0]):
if method == 'mean':
seeds[i] = np.array(
[np.mean(np.array(points_labeled[i])[:, 0]), np.mean(np.array(points_labeled[i])[:, 1])])
elif method == 'median':
seeds[i] = np.array(
[np.median(np.array(points_labeled[i])[:, 0]), np.median(np.array(points_labeled[i])[:, 1])])
points_labeled = []
for i in range(cluster_num):
points_labeled.append([])
labels = [] return seeds, labels, sum(abs((prev_seeds - seeds).ravel())) if __name__ == "__main__":
points = [
[2, 4], [4, 2], [6, 2], [5, 3], [5, 5], [7, 5],
[5, 15], [6, 17], [4, 14], [5, 13], [9, 15], [3, 14], [7, 13],
[20, 16], [19, 15], [17, 15], [16, 14], [14, 18], [22, 10], [17, 17], [16, 13], [18, 14], [17, 13],
[22, 26], [24, 23], [25, 25], [26, 22], [26, 26], [26, 28], [28, 18], [28, 28]
]
cluster_num = 4
[centroids, labels, interia] = kmeans(points, cluster_num)

最新文章

  1. [python]set集合学习
  2. 10分钟学会前端调试利器——FireBug
  3. Charles
  4. c#隐藏和重写基类方法的异同
  5. sql: sq_helptext
  6. Java递归实现操作系统文件的复制、粘贴和删除功能
  7. PHP使用COM 获取RTF内容
  8. demo_05HTML5+CSS3绘制小鸟
  9. 【爬虫入门01】我第一只由Reuests和BeautifulSoup4供养的Spider
  10. ionic2 开始第一个App(二)
  11. [Sdoi2017]相关分析 [线段树]
  12. Docker 入门之创建service(一)
  13. SQLServer之锁定数据库表
  14. 在chrome上隐藏video的option按钮
  15. anoconda包管理汇总
  16. CAD版本 注册表信息
  17. Intellij IDEA 解决 Maven 依赖下载慢的问题
  18. 深度学习基础(一)LeNet_Gradient-Based Learning Applied to Document Recognition
  19. Unity3d和Android之间互相调用
  20. python爬取股票信息

热门文章

  1. css百分比参照大总结
  2. Unity Shader - 消融效果原理与变体
  3. Ubuntu15.04 网站服务器环境搭建,php/html/css等学习环境搭建教程
  4. emacs 配置
  5. [maven(1)]myeclipse2014下如何配置maven
  6. Windows下使用nginx搭建反向代理服务器
  7. GET_DDL提取目标元数据:ddl
  8. Yii2之组件的注册与创建
  9. 一起来学linux:SSH远程登陆
  10. Kotlin——最详细的接口使用、介绍