Python聚类算法之DBSACN实例分析
作者:intergret 发布时间:2021-03-26 00:11:10
标签:Python,算法
本文实例讲述了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程序设计有所帮助。


猜你喜欢
- 进程什么是进程进程指的是一个程序的运行过程,或者说一个正在执行的程序所以说进程一种虚拟的概念,该虚拟概念起源操作系统一个CPU 同一时刻只能
- 一、初始递归递归函数:在一个函数里在调用这个函数本身。递归的最大深度:998正如你们刚刚看到的,递归函数如果不受到外力的阻止会一直执行下去。
- 删除字符串中不需要的内容1、strip()方法strip:默认是去掉首尾的空白字符,但是也可以指定其他字符;lstrip:只去掉左边的;rs
- 一、需求来源:如果用户在文本框中填了一段<script>alert(xxx);</script>代码,然后我们还保存
- 在Vista IIS 7 中用 vs2005 调试 Web 项目核心是要解决以下几个问题:1、Vista 自身在安全性方面的User Acc
- 一.局部变量、全局变量1.什么是局部变量作用范围在函数内部,在函数外部无法使用2.什么是全局变量在函数内部和外部均可使用3.如何将函数内定义
- 最新MySql8.27主从复制以及SpringBoot项目中的读写分离实战1、MySql主从复制MySQL主从复制是一个异步的复制过程,底层
- 本文实例讲述了python计算书页码的统计数字问题,是Python程序设计中一个比较典型的应用实例。分享给大家供大家参考。具体如下:问题描述
- 如下所示:在组件上添加属性 this.$set(this.data,"obj",value');删除属性this
- Python3安装后SSL问题问题编译安装时已经指定了–with-openssl的参数并且指向了你的openssl的源码
- 大概在2004年初的时候,我第一次买了一本很厚的书,名字或许叫《Dreamweaver MX从入门到精通》,很认真看着书并实践操作大约三分之
- 程序中经常需要使用excel文件,批量读取文件中的数据python读取excel文件可以使用xlrd模块pip install xlrd安装
- 在JavaScript的世界里,定义函数的方法多种多样,这正是JavaScript灵活性的体现,但是正是这个原因让初学者摸不着头脑,尤其对于
- numpy.amin()和numpy.amax()numpy.amin()用于计算数组中元素沿着指定轴的最小值。numpy.amax()用于
- 一般采用的方法:self.window = Qdialog() # 实例化self.window.show() # 显示界面用这种方法只能打
- 我是在做行人检测中需要将一段视频变为图片数据集,然后想将视频每秒钟的图片提取出来。语言:python所需要的库:cv2,numpy (自行安
- 目录实现加权轮询负载均衡思路加权轮询负载均衡代码测试代码实现加权轮询负载均衡思路代码实现一个加权负载均衡Weight
- 本文实例讲述了Python数据类型之Set集合。分享给大家供大家参考,具体如下:set集合1.概述set与dict类似,但set是一组key
- pytorch中为什么要用 zero_grad() 将梯度清零调用backward()函数之前都要将梯度清零,因为如果梯度不清零,pytor
- 简介EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False。EXISTS 指定一个