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单例模式实例详解
Mar 01 Python
Python中is与==判断的区别
Mar 28 Python
详解python的webrtc库实现语音端点检测
May 31 Python
Python使用Tkinter实现机器人走迷宫
Jan 22 Python
教你用一行Python代码实现并行任务(附代码)
Feb 02 Python
10 行 Python 代码教你自动发送短信(不想回复工作邮件妙招)
Oct 11 Python
Opencv+Python 色彩通道拆分及合并的示例
Dec 08 Python
python selenium执行所有测试用例并生成报告的方法
Feb 13 Python
Python字符串通过'+'和join函数拼接新字符串的性能测试比较
Mar 05 Python
解决Python二维数组赋值问题
Nov 28 Python
Win10下用Anaconda安装TensorFlow(图文教程)
Jun 18 Python
如何获取numpy array前N个最大值
May 14 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 分页类 扩展代码
2009/06/11 PHP
Zend Framework教程之Zend_Db_Table_Row用法实例分析
2016/03/21 PHP
Laravel访问出错提示:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or di解决方法
2019/04/02 PHP
PHPUnit + Laravel单元测试常用技能
2019/11/06 PHP
在vs2010中调试javascript代码方法
2011/02/11 Javascript
兼容IE和FF的图片上传前预览js代码
2013/05/28 Javascript
jquery ui dialog实现弹窗特效的思路及代码
2013/08/03 Javascript
JQuery设置时间段下拉选择实例
2014/12/30 Javascript
js实现有时间限制消失的图片方法
2015/02/27 Javascript
一道常被人轻视的web前端常见面试题(JS)
2016/02/15 Javascript
jQuery遍历json的方法(推荐)
2016/06/12 Javascript
微信小程序  生命周期详解
2016/10/27 Javascript
bootstrap表格内容过长时用省略号表示的解决方法
2017/11/21 Javascript
angular.js实现列表orderby排序的方法
2018/10/02 Javascript
Nodejs实现图片上传、压缩预览、定时删除功能
2019/10/25 NodeJs
Vue+Element实现网页版个人简历系统(推荐)
2019/12/31 Javascript
[07:52]2014DOTA2 TI逗比武士游V社解说背后的故事
2014/07/10 DOTA
Python实现抓取网页并且解析的实例
2014/09/20 Python
python以环状形式组合排列图片并输出的方法
2015/03/17 Python
对json字符串与python字符串的不同之处详解
2018/12/19 Python
Python线程池模块ThreadPoolExecutor用法分析
2018/12/28 Python
python实现图片转字符画
2021/02/19 Python
CSS3实现线性渐变用法示例代码详解
2020/08/07 HTML / CSS
HTML5通用接口详解
2016/06/12 HTML / CSS
美国畅销的跑步机品牌:ProForm
2017/02/06 全球购物
面向对象编程是如何提高软件开发水平的
2014/05/06 面试题
交通安全演讲稿
2014/01/07 职场文书
税务干部鉴定材料
2014/02/11 职场文书
个人收入证明模板
2014/09/18 职场文书
课堂打架检讨书200字
2014/11/21 职场文书
2014年扫黄打非工作总结
2014/12/03 职场文书
节约用电通知
2015/04/25 职场文书
党员志愿者服务倡议书
2015/04/29 职场文书
2015银行年终工作总结范文
2015/05/26 职场文书
郭明义观后感
2015/06/08 职场文书
win10清理dns缓存
2022/04/19 数码科技