博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Maciej Pacula » k-means clustering example (Python)
阅读量:5746 次
发布时间:2019-06-18

本文共 1388 字,大约阅读时间需要 4 分钟。

I had to illustrate a k-means algorithm for my thesis, but I could not find any existing examples that were both simple and looked good on paper. See below for Python code that does just what I wanted.

#!/usr/bin/python # Adapted from http://hackmap.blogspot.com/2007/09/k-means-clustering-in-scipy.html import numpyimport matplotlibmatplotlib.use('Agg')from scipy.cluster.vq import *import pylabpylab.close() # generate 3 sets of normally distributed points around# different means with different variancespt1 = numpy.random.normal(1, 0.2, (100,2))pt2 = numpy.random.normal(2, 0.5, (300,2))pt3 = numpy.random.normal(3, 0.3, (100,2)) # slightly move sets 2 and 3 (for a prettier output)pt2[:,0] += 1pt3[:,0] -= 0.5 xy = numpy.concatenate((pt1, pt2, pt3)) # kmeans for 3 clustersres, idx = kmeans2(numpy.array(zip(xy[:,0],xy[:,1])),3) colors = ([([0.4,1,0.4],[1,0.4,0.4],[0.1,0.8,1])[i] for i in idx]) # plot colored pointspylab.scatter(xy[:,0],xy[:,1], c=colors) # mark centroids as (X)pylab.scatter(res[:,0],res[:,1], marker='o', s = 500, linewidths=2, c='none')pylab.scatter(res[:,0],res[:,1], marker='x', s = 500, linewidths=2) pylab.savefig('/tmp/kmeans.png')

The output looks like this (also available in vector format ):

The X’s mark cluster centers. Feel free to use any of these files for whatever purposes. An attribution would be nice, but is not required :-) .

转载地址:http://fdxzx.baihongyu.com/

你可能感兴趣的文章
Windows API调用外部程序之图片查看
查看>>
《霍乱时期的爱情》
查看>>
Hibernate关联映射
查看>>
CSS技巧(二):CSS hack
查看>>
Mac 命令
查看>>
Hadoop入门简介
查看>>
学习devexpresschartControl控件
查看>>
byte类型取值范围为什么是-128到127?
查看>>
iOS代理模式(delegate)的使用
查看>>
FragmentHelper
查看>>
3-1-局部最小值位置
查看>>
Python 二分查找
查看>>
C++箴言:理解inline化的介入和排除
查看>>
无法与域Active Directory域控制器(AD DC)连接(虚机加域出错问题)
查看>>
MSSQL翻页存储过程
查看>>
OSI七层模型与TCP/IP五层模型
查看>>
Android UI 组件
查看>>
后缀数组(suffix array)详解
查看>>
转 理解Android系统Binder机制
查看>>
关于字符串的分割问题
查看>>