python kmeans聚类简单介绍和实现代码


Posted in Python onFebruary 23, 2018

一、k均值聚类的简单介绍

假设样本分为c类,每个类均存在一个中心点,通过随机生成c个中心点进行迭代,计算每个样本点到类中心的距离(可以自定义、常用的是欧式距离)  

将该样本点归入到最短距离所在的类,重新计算聚类中心,进行下次的重新划分样本,最终类中心不改变时,聚类完成   

二、伪代码  

三、python代码实现  

#!/usr/bin/env python 
# coding=utf-8 
 
import numpy as np 
import random 
import matplotlib.pyplot as plt 
 
#data:numpy.array dataset 
#k the number of cluster 
def k_means(data,k): 
   
  #random generate cluster_center 
  sample_num=data.shape[0] 
  center_index=random.sample(range(sample_num),k) 
  cluster_cen=data[center_index,:] 
 
  is_change=1 
  cat=np.zeros(sample_num) 
   
 
  while is_change: 
    is_change=0 
 
    for i in range(sample_num): 
      min_distance=100000 
      min_index=0 
 
      for j in range(k): 
        sub_data=data[i,:]-cluster_cen[j,:] 
        distance=np.inner(sub_data,sub_data) 
        if distance<min_distance: 
          min_distance=distance 
          min_index=j+1 
 
      if cat[i]!=min_index: 
        is_change=1 
        cat[i]=min_index 
    for j in range(k): 
      cluster_cen[j]=np.mean(data[cat==(j+1)],axis=0) 
 
  return cat,cluster_cen 
 
 
if __name__=='__main__': 
 
  #generate data 
  cov=[[1,0],[0,1]] 
  mean1=[1,-1] 
  x1=np.random.multivariate_normal(mean1,cov,200) 
 
  mean2=[5.5,-4.5] 
  x2=np.random.multivariate_normal(mean2,cov,200) 
 
  mean3=[1,4] 
  x3=np.random.multivariate_normal(mean3,cov,200) 
 
  mean4=[6,4.5] 
  x4=np.random.multivariate_normal(mean4,cov,200) 
 
  mean5=[9,0.0] 
  x5=np.random.multivariate_normal(mean5,cov,200) 
   
  X=np.vstack((x1,x2,x3,x4,x5)) 
   
  #data distribution 
  fig1=plt.figure(1) 
  p1=plt.scatter(x1[:,0],x1[:,1],marker='o',color='r',label='x1') 
  p2=plt.scatter(x2[:,0],x2[:,1],marker='+',color='m',label='x2') 
  p3=plt.scatter(x3[:,0],x3[:,1],marker='x',color='b',label='x3') 
  p4=plt.scatter(x4[:,0],x4[:,1],marker='*',color='g',label='x4') 
  p5=plt.scatter(x5[:,0],x4[:,1],marker='+',color='y',label='x5') 
  plt.title('original data') 
  plt.legend(loc='upper right') 
   
  cat,cluster_cen=k_means(X,5)    
 
  print 'the number of cluster 1:',sum(cat==1) 
  print 'the number of cluster 2:',sum(cat==2) 
  print 'the number of cluster 3:',sum(cat==3) 
  print 'the number of cluster 4:',sum(cat==4) 
  print 'the number of cluster 5:',sum(cat==5) 
 
   
  fig2=plt.figure(2) 
  for i,m,lo,label in zip(range(5),['o','+','x','*','+'],['r','m','b','g','y'],['x1','x2','x3','x4','x5']): 
 
    p=plt.scatter(X[cat==(i+1),0],X[cat==(i+1),1],marker=m,color=lo,label=label) 
  plt.legend(loc='upper right') 
  plt.title('the clustering result') 
  plt.show()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用win32com在百度空间插入html元素示例
Feb 20 Python
Python中处理unchecked未捕获异常实例
Jan 17 Python
python3.4用函数操作mysql5.7数据库
Jun 23 Python
详解Python中for循环是如何工作的
Jun 30 Python
Zookeeper接口kazoo实例解析
Jan 22 Python
基于python3 OpenCV3实现静态图片人脸识别
May 25 Python
Python之用户输入的实例
Jun 22 Python
Python 中的lambda函数介绍
Oct 10 Python
将pandas.dataframe的数据写入到文件中的方法
Dec 07 Python
Python3+Appium安装使用教程
Jul 05 Python
tensorflow 大于某个值为1,小于为0的实例
Jun 30 Python
一文搞懂python异常处理、模块与包
Jun 26 Python
python MysqlDb模块安装及其使用详解
Feb 23 #Python
Python实现k-means算法
Feb 23 #Python
python语言中with as的用法使用详解
Feb 23 #Python
python实现定时自动备份文件到其他主机的实例代码
Feb 23 #Python
Python机器学习算法之k均值聚类(k-means)
Feb 23 #Python
python3调用R的示例代码
Feb 23 #Python
python中kmeans聚类实现代码
Feb 23 #Python
You might like
弄了个检测传输的参数是否为数字的Function
2006/12/06 PHP
PR值查询 | PageRank 查询
2006/12/20 PHP
使用PHP Socket写的POP3类
2013/10/30 PHP
destoon实现底部添加你是第几位访问者的方法
2014/07/15 PHP
javascript学习笔记(十三) js闭包介绍(转)
2012/06/20 Javascript
jquery获取tr中控件值并操作tr实现思路
2013/03/27 Javascript
Jquery增加鼠标中间功能mousewheel的实例代码
2013/09/05 Javascript
jquery实现非叠加式的搜索框提示效果
2014/01/07 Javascript
红米手机抢购的js代码
2014/03/10 Javascript
jquery实现通用的内容渐显Tab选项卡效果
2015/09/07 Javascript
AngularJS基础 ng-href 指令用法
2016/08/01 Javascript
全面解析Bootstrap表单样式的使用
2016/09/09 Javascript
JS扩展类,克隆对象与混合类实例分析
2016/11/26 Javascript
Angular工具方法学习
2016/12/26 Javascript
微信小程序 form组件详解及简单实例
2017/01/10 Javascript
jQuery插件FusionCharts实现的Marimekko图效果示例【附demo源码】
2017/03/24 jQuery
nodejs处理图片的中间件node-images详解
2017/05/08 NodeJs
Angular中$state.go页面跳转并传递参数的方法
2017/05/09 Javascript
nodejs使用http模块发送get与post请求的方法示例
2018/01/08 NodeJs
使用vue如何构建一个自动建站项目
2018/02/05 Javascript
vue axios 给生产环境和发布环境配置不同的接口地址(推荐)
2018/05/08 Javascript
vue代码分割的实现(codesplit)
2018/11/13 Javascript
python中zip和unzip数据的方法
2015/05/27 Python
python调用百度语音识别实现大音频文件语音识别功能
2018/08/30 Python
face++与python实现人脸识别签到(考勤)功能
2019/08/28 Python
python语音识别指南终极版(有这一篇足矣)
2020/09/09 Python
python 数据类型强制转换的总结
2021/01/25 Python
美国嘻哈首饰购物网站:Hip Hop Bling
2016/12/30 全球购物
小学生作文评语大全
2014/04/21 职场文书
2014年教师业务学习材料
2014/05/12 职场文书
巴西世界杯32强口号
2014/06/05 职场文书
2014年安置帮教工作总结
2014/12/11 职场文书
小鞋子观后感
2015/06/05 职场文书
幼儿园保育员随笔
2015/08/14 职场文书
那些美到让人窒息的诗句,值得你收藏!
2019/08/20 职场文书
Python sklearn分类决策树方法详解
2022/09/23 Python