Python聚类算法之DBSACN实例分析


Posted in Python onNovember 20, 2015

本文实例讲述了Python聚类算法之DBSACN。分享给大家供大家参考,具体如下:

DBSCAN:是一种简单的,基于密度的聚类算法。本次实现中,DBSCAN使用了基于中心的方法。在基于中心的方法中,每个数据点的密度通过对以该点为中心以边长为2*EPs的网格(邻域)内的其他数据点的个数来度量。根据数据点的密度分为三类点:

核心点:该点在邻域内的密度超过给定的阀值MinPs。
边界点:该点不是核心点,但是其邻域内包含至少一个核心点。
噪音点:不是核心点,也不是边界点。

有了以上对数据点的划分,聚合可以这样进行:各个核心点与其邻域内的所有核心点放在同一个簇中,把边界点跟其邻域内的某个核心点放在同一个簇中。

# scoding=utf-8
import pylab as pl
from collections import defaultdict,Counter
points = [[int(eachpoint.split("#")[0]), int(eachpoint.split("#")[1])] for eachpoint in open("points","r")]
# 计算每个数据点相邻的数据点,邻域定义为以该点为中心以边长为2*EPs的网格
Eps = 10
surroundPoints = defaultdict(list)
for idx1,point1 in enumerate(points):
  for idx2,point2 in enumerate(points):
    if (idx1 < idx2):
      if(abs(point1[0]-point2[0])<=Eps and abs(point1[1]-point2[1])<=Eps):
        surroundPoints[idx1].append(idx2)
        surroundPoints[idx2].append(idx1)
# 定义邻域内相邻的数据点的个数大于4的为核心点
MinPts = 5
corePointIdx = [pointIdx for pointIdx,surPointIdxs in surroundPoints.iteritems() if len(surPointIdxs)>=MinPts]
# 邻域内包含某个核心点的非核心点,定义为边界点
borderPointIdx = []
for pointIdx,surPointIdxs in surroundPoints.iteritems():
  if (pointIdx not in corePointIdx):
    for onesurPointIdx in surPointIdxs:
      if onesurPointIdx in corePointIdx:
        borderPointIdx.append(pointIdx)
        break
# 噪音点既不是边界点也不是核心点
noisePointIdx = [pointIdx for pointIdx in range(len(points)) if pointIdx not in corePointIdx and pointIdx not in borderPointIdx]
corePoint = [points[pointIdx] for pointIdx in corePointIdx] 
borderPoint = [points[pointIdx] for pointIdx in borderPointIdx]
noisePoint = [points[pointIdx] for pointIdx in noisePointIdx]
# pl.plot([eachpoint[0] for eachpoint in corePoint], [eachpoint[1] for eachpoint in corePoint], 'or')
# pl.plot([eachpoint[0] for eachpoint in borderPoint], [eachpoint[1] for eachpoint in borderPoint], 'oy')
# pl.plot([eachpoint[0] for eachpoint in noisePoint], [eachpoint[1] for eachpoint in noisePoint], 'ok')
groups = [idx for idx in range(len(points))]
# 各个核心点与其邻域内的所有核心点放在同一个簇中
for pointidx,surroundIdxs in surroundPoints.iteritems():
  for oneSurroundIdx in surroundIdxs:
    if (pointidx in corePointIdx and oneSurroundIdx in corePointIdx and pointidx < oneSurroundIdx):
      for idx in range(len(groups)):
        if groups[idx] == groups[oneSurroundIdx]:
          groups[idx] = groups[pointidx]
# 边界点跟其邻域内的某个核心点放在同一个簇中
for pointidx,surroundIdxs in surroundPoints.iteritems():
  for oneSurroundIdx in surroundIdxs:
    if (pointidx in borderPointIdx and oneSurroundIdx in corePointIdx):
      groups[pointidx] = groups[oneSurroundIdx]
      break
# 取簇规模最大的5个簇
wantGroupNum = 3
finalGroup = Counter(groups).most_common(3)
finalGroup = [onecount[0] for onecount in finalGroup]
group1 = [points[idx] for idx in xrange(len(points)) if groups[idx]==finalGroup[0]]
group2 = [points[idx] for idx in xrange(len(points)) if groups[idx]==finalGroup[1]]
group3 = [points[idx] for idx in xrange(len(points)) if groups[idx]==finalGroup[2]]
pl.plot([eachpoint[0] for eachpoint in group1], [eachpoint[1] for eachpoint in group1], 'or')
pl.plot([eachpoint[0] for eachpoint in group2], [eachpoint[1] for eachpoint in group2], 'oy')
pl.plot([eachpoint[0] for eachpoint in group3], [eachpoint[1] for eachpoint in group3], 'og')
# 打印噪音点,黑色
pl.plot([eachpoint[0] for eachpoint in noisePoint], [eachpoint[1] for eachpoint in noisePoint], 'ok')  
pl.show()

运行效果截图如下:

Python聚类算法之DBSACN实例分析

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python检测lvs real server状态
Jan 22 Python
使用Python中的cookielib模拟登录网站
Apr 09 Python
Python的Flask框架应用调用Redis队列数据的方法
Jun 06 Python
解决Spyder中图片显示太小的问题
Apr 27 Python
mac安装scrapy并创建项目的实例讲解
Jun 13 Python
浅谈python3.6的tkinter运行问题
Feb 22 Python
Python logging设置和logger解析
Aug 28 Python
Django和Flask框架优缺点对比
Oct 24 Python
Python对Tornado请求与响应的数据处理
Feb 12 Python
python 画图 图例自由定义方式
Apr 17 Python
Django数据结果集序列化并展示实现过程
Apr 22 Python
python如何实现读取并显示图片(不需要图形界面)
Jul 08 Python
Python聚类算法之凝聚层次聚类实例分析
Nov 20 #Python
Python聚类算法之基本K均值实例详解
Nov 20 #Python
Python实现将xml导入至excel
Nov 20 #Python
使用PyCharm配合部署Python的Django框架的配置纪实
Nov 19 #Python
详解在Python程序中解析并修改XML内容的方法
Nov 16 #Python
Python通过DOM和SAX方式解析XML的应用实例分享
Nov 16 #Python
Python的Flask开发框架简单上手笔记
Nov 16 #Python
You might like
PHP 修复未正常关闭的HTML标签实现代码(支持嵌套和就近闭合)
2012/06/07 PHP
php中file_exists函数使用详解
2015/05/08 PHP
PHP dirname功能及原理实例解析
2020/10/28 PHP
利用javascript中的call实现继承
2007/01/22 Javascript
ExtJS 2.0实用简明教程 之获得ExtJS
2009/04/29 Javascript
Javascript笔记一 js以及json基础使用说明
2010/05/22 Javascript
JQuery页面图片切换和新闻列表滚动效果的具体实现
2013/09/26 Javascript
JQuery中serialize() 序列化
2015/03/13 Javascript
关于JSON.parse(),JSON.stringify(),jQuery.parseJSON()的用法
2016/06/30 Javascript
Bootstrap的popover(弹出框)在append后弹不出(失效)
2017/02/27 Javascript
nodejs入门教程三:调用内部和外部方法示例
2017/04/24 NodeJs
深入理解Commonjs规范及Node模块实现
2017/05/17 Javascript
在Swiper内如何制作CSS3动画效果示例代码
2017/12/07 Javascript
layui 表格的属性的显示转换方法
2018/08/14 Javascript
深入理解Puppeteer的入门教程和实践
2019/03/05 Javascript
Layui 解决表格异步调用后台分页的问题
2019/10/26 Javascript
javascript绘制简单钟表效果
2020/04/07 Javascript
Vue实现简单的跑马灯
2020/05/25 Javascript
以一段代码为实例快速入门Python2.7
2015/03/31 Python
通过mod_python配置运行在Apache上的Django框架
2015/07/22 Python
Python基于Tkinter模块实现的弹球小游戏
2018/12/27 Python
Pycharm设置utf-8自动显示方法
2019/01/17 Python
Python微信操控itchat的方法
2019/05/31 Python
python中random.randint和random.randrange的区别详解
2020/09/20 Python
HTML5中通过li-canvas轻松实现单图、多图、圆角图绘制,单行文字、多行文字等
2018/11/30 HTML / CSS
意大利团购网站:Groupon意大利
2016/10/11 全球购物
linux面试题参考答案(8)
2015/08/11 面试题
课程设计心得体会
2013/12/28 职场文书
会计职业生涯规划范文
2014/01/04 职场文书
高中打架检讨书
2014/02/13 职场文书
自荐信模板大全
2015/03/27 职场文书
忠犬八公的故事观后感
2015/06/05 职场文书
欠条格式范本
2015/07/03 职场文书
MySQL InnoDB ReplicaSet(副本集)简单介绍
2021/04/24 MySQL
深入理解以DEBUG方式线程的底层运行原理
2021/06/21 Java/Android
纯CSS打字动画的实现示例
2022/08/05 HTML / CSS