DBSCAN——python实现
# -*- coding: utf-8 -*-
from matplotlib.pyplot import *
from collections import defaultdict
import random
import json
"""
计算两点欧式距离的函数
"""
def dist(p1,p2):
return ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** (0.5) all_points = []
index = 1000
#use python build-in library to load the json file
flickr_data = json.load(file("Paris_points.json"))
for i in range(index):
Coord = [flickr_data['latitudes'][i],flickr_data['longitudes'][i]]
all_points.append(Coord) """
设置E和minPts的值
"""
E = 0.001
minPts = 7 """
随机产生100个直角坐标,测试用,测试时用E = 8, minPts = 8
"""
#all_points = []
# for i in range(100):
# randCoord = [random.randint(1,50),random.randint(1,50)]
# if not randCoord in all_points:
# all_points.append(randCoord) """
找出核心点
"""
other_points = []
core_points = []
plotted_points = []
for point in all_points:
point.append(0) #assign initial level 0,即定义核心点的类型,每个核心点作为一个中心
total = 0
for otherPoint in all_points:
distance = dist(otherPoint,point)
if distance <= E:
total += 1 if total > minPts:
core_points.append(point)
plotted_points.append(point)
else:
other_points.append(point) """
找到边界点
"""
border_points = []
for core in core_points:
for other in other_points:
if dist(core,other) <= E:
border_points.append(other)
plotted_points.append(other) """
完成分类的算法,给核心点都贴上标签
"""
cluster_label = 0 for point in core_points:
if point[2] == 0:
cluster_label += 1
point[2] = cluster_label for point2 in plotted_points:
distance = dist(point2, point)
if point2[2] == 0 and distance <= E:
#print point,point2
point2[2] = point[2] """
当所有的点都分配到相应的标签后,我们把同一簇的划分到一起
"""
cluster_list = defaultdict(lambda:[[],[]])
for point in plotted_points:
cluster_list[point[2]][0].append(point[0])
cluster_list[point[2]][1].append(point[1]) markers = ['+','*','.','d','^','v','>','<','p']
#markers = ['b.','g.','r.','c.','m.','y.','k.'] """
画出所有点的图
"""
figure(1)
allx = []
ally = []
for plot_point in all_points:
allx.append(plot_point[0])
ally.append(plot_point[1])
plot(allx, ally,"r.")
title("total points=" + str(len(all_points)) + " E =" + str(E) + " Min Points=" + str(minPts)) """
画出核心点的图
"""
figure(2)
i = 0
print cluster_list
for value in cluster_list:
cluster = cluster_list[value]
plot(cluster[0],cluster[1],markers[i])
i = i % 8 + 1
#i = i % 6 + 1
title(str(len(cluster_list)) + " clusters created with E = "+ str(E) + " Min Points=" + str(minPts)) """
画出噪音点的图
"""
figure(3)
noise_points = []
for point in all_points:
if not point in core_points and not point in border_points:
noise_points.append(point)
noisex = []
noisey = []
for point in noise_points:
noisex.append(point[0])
noisey.append(point[1])
plot(noisex,noisey,"x") title("noise Points = "+ str(len(noise_points)) + " E ="+str(E)+" Min Points="+str(minPts))
#axis((0,60,0,60))
show()
DBSCAN——python实现的更多相关文章
- Python实现DBScan
Python实现DBScan 运行环境 Pyhton3 numpy(科学计算包) matplotlib(画图所需,不画图可不必) 计算过程 st=>start: 开始 e=>end: 结束 ...
- (数据科学学习手札15)DBSCAN密度聚类法原理简介&Python与R的实现
DBSCAN算法是一种很典型的密度聚类法,它与K-means等只能对凸样本集进行聚类的算法不同,它也可以处理非凸集. 关于DBSCAN算法的原理,笔者觉得下面这篇写的甚是清楚练达,推荐大家阅读: ht ...
- Python机器学习——DBSCAN聚类
密度聚类(Density-based Clustering)假设聚类结构能够通过样本分布的紧密程度来确定.DBSCAN是常用的密度聚类算法,它通过一组邻域参数(ϵϵ,MinPtsMinPts)来描述样 ...
- Python实现DBSCAN聚类算法(简单样例测试)
发现高密度的核心样品并从中膨胀团簇. Python代码如下: # -*- coding: utf-8 -*- """ Demo of DBSCAN clustering ...
- Python机器学习笔记:K-Means算法,DBSCAN算法
K-Means算法 K-Means 算法是无监督的聚类算法,它实现起来比较简单,聚类效果也不错,因此应用很广泛.K-Means 算法有大量的变体,本文就从最传统的K-Means算法学起,在其基础上学习 ...
- 挑子学习笔记:DBSCAN算法的python实现
转载请标明出处:https://www.cnblogs.com/tiaozistudy/p/dbscan_algorithm.html DBSCAN(Density-Based Spatial Clu ...
- [MCM] K-mean聚类与DBSCAN聚类 Python
import matplotlib.pyplot as plt X=[56.70466067,56.70466067,56.70466067,56.70466067,56.70466067,58.03 ...
- 吴裕雄 python 机器学习——密度聚类DBSCAN模型
import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics ...
- DBscan算法及其Python实现
DBSCAN简介: 1.简介 DBSCAN 算法是一种基于密度的空间聚类算法.该算法利用基于密度的聚类的概念,即要求聚类空间中的一定区域内所包含对象(点或其它空间对象)的数目不小于某一给定阀值.DBS ...
随机推荐
- js倒计时跳转链接
(function(){ var loadUrl = 'http://www.cnblogs.com/naokr/',//跳转链接 loadTime = 3000,//跳转时间 reTime = 10 ...
- shell单引号中输出参数值
因为在shell的单引号中,所有的特殊字符和变量都会变成文本,那么如果需要在字符串中输出变量值怎么办呢? 这里记录以下两种方法: 使用双引号 shell> X='parameter' shell ...
- 20160405互联网新闻<来自涛涛大产品>
1.滴滴或将收购腾讯地图,打造“滴滴地图”(滴滴与神州.uber之间的争斗,归根到底还是BAT的代理人之战)2.优信二手车否认合并传言 并谴责58同城仿冒优信网站(商战无所不用其极)3.京东旗下的拍拍 ...
- 最小生成树——prim算法
prim算法是选取任意一个顶点作为树的一个节点,然后贪心的选取离这棵树最近的点,直到连上所有的点并且不够成环,它的时间复杂度为o(v^2) #include<iostream>#inclu ...
- Linux CentOs7 下安装 redis
Linux CentOs7 下安装 redis 请将以下命令放入linux命令行中运行 如果安装过程前没有安装GCC请先安装 命令如下 $ yum install gcc-c++ $ wget ht ...
- PHP MVC
学习一个框架之前,基本上我们都需要知道什么是mvc,即model-view-control,说白了就是数据控制以及页面的分离实现,mvc就 是这样应运而生的,mvc分为了三个层次,而且三个层次各司其职 ...
- 终于在cmd窗口里出现了颜色了!!!感动ing……
在窗口的中央打印三行字. 要求: 第一行绿色字 第二行绿底红色 第三行白底蓝色 assume cs:code, ds:data data segment db 'welcome to masm!' d ...
- linux笔记_防止ddos攻击
一.什么是DoS攻击 DoS是Denial of Service的简称,即拒绝服务,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务.最常见的DoS攻击有计算机网络带宽攻 ...
- Codeforces Round #168 (Div. 2)
A. Lights Out 模拟. B. Convex Shape 考虑每个黑色格子作为起点,拐弯次数为0的格子构成十字形,拐弯1次的则是从这些格子出发直走达到的点,显然需要遍历到所有黑色黑色格子. ...
- SPI
SPI的通信原理以主从方式工作,这种模式通常有一个主设备和一个或多个从设备,有4根线,单向传输时只要3根线. (1)MOSI(SDO) – 主设备数据输出,从设备数据输入(Master Out Sla ...