资源简介
python实现k-means算法,将结果保存,同时一画图的方式显示,完成python2.7.12, numpy,scipy,matplotlib的基本配置

代码片段和文件信息
#k-means
#coding utf-8
from numpy import *
import time
import matplotlib.pyplot as plt
#calculate Euclidean
def euclDistance(vector1 vector2):
return sqrt(sum(power(vector2 - vector1 2)))
#init centroids with random sample
def initCentroids(dataSet k):
row col = dataSet.shape
centroids = zeros((k col))
for i in range(k):
index = int(random.uniform(0 row))
centroids[i :] = dataSet[index :]
return centroids
#k-means cluster
def kmeans(dataSet k):
row col = dataSet.shape
#first column stores which cluster this sample belongs to
#sencond column stores the error between this sample and its centroid
clusterAssment = mat(zeros((row 2)))
clusterChanged = True
# step1: init centroids
centroids = initCentroids(dataSet k)
while clusterChanged:
clusterChanged = False
#foreach each sample
for i in xrange(row):
minDistance = 1000
minIndex = 0
#for each centroid
## step2: find the centroid who is the closest
for j in range(k):
distance = euclDistance(centroids[j:] dataSet[i:])
if distance < minDistance:
minDistance = distance
minIndex = j
##step3:update its cluster
if clusterAssment[i0] != minIndex:
clusterChanged = True
clusterAssment[i:] = minIndex minDistance**2
#step4 update its centroids
for j in range(k):
pointsInCluster = dataSet[nonzero(clusterAssment[: 0].A == j)[0]]
centroids[j:] = mean(pointsInCluster axis = 0)
print ‘Congratulations k-means cluster is completed!!!!!!!!!!!!!!!‘
return centroids clusterAssment
#show your cluster only avaliable with 2D data
def showCluster(dataSet k centroids clusterAssment):
row col = dataSet.shape
if col != 2:
print “Sorry! I can not draw because the dimension of your data is not 2“
return 1
mark = [‘or‘ ‘ob‘ ‘og‘ ‘ok‘ ‘^r‘ ‘+r‘ ‘sr‘ ‘dr‘ ‘ if(k > len(mark)):
print “Sorry! Your k is too large! Please choose smaller k“
return 1
#draw all samples
for i in xrange(row):
markIndex = int(clusterAssment[i0])
plt.plot(dataSet[i 0] dataSet[i1] mark[markIndex])
mark = [‘Dr‘ ‘Db‘ ‘Dg‘ ‘Dk‘ ‘^b‘ ‘+b‘ ‘sb‘ ‘db‘ ‘ #draw the centroids
for j in range(k):
plt.plot(centroids[j0] centroids[j1] mark[j] markersize = 12)
plt.show()
#write centroids and clusterAssment in txt
def writeResult(dataSet k centroids clusterAssment):
fileOut = open(“C:\Users\Administrator\Desktop\kmeans-python\centroids.txt“ “w“)
out = open(“C:\Users\Administrator\Desktop\kmeans-python\clusterAssment.txt“ “w“)
for i in range(k):
fileOut.write(str(c
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 93 2016-12-06 17:01 kmeans-python\centroids.txt
文件 3229 2016-12-06 17:01 kmeans-python\clusterAssment.txt
文件 237 2016-12-06 15:41 kmeans-python\data.txt
文件 1930 2016-12-06 15:40 kmeans-python\data2.txt
文件 27 2016-12-06 16:35 kmeans-python\haha.txt
文件 3283 2016-12-06 17:01 kmeans-python\kmeans.py
文件 3393 2016-12-06 17:01 kmeans-python\kmeans.pyc
文件 234 2016-12-06 16:35 kmeans-python\test.py
文件 750 2016-12-06 16:47 kmeans-python\test_kmeans.py
目录 0 2016-12-06 17:01 kmeans-python
----------- --------- ---------- ----- ----
13176 10
- 上一篇:Kmeans算法python实现
- 下一篇:Maya Python游戏与影视编程指南
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论