Skip to content

Commit d124f00

Browse files
Create dbscan.py
1 parent ed3d929 commit d124f00

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

Chapter12_DBSCAN/dbscan.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# coding:UTF-8
2+
'''
3+
Date:20160923
4+
@author: zhaozhiyong
5+
'''
6+
7+
import numpy as np
8+
import math
9+
10+
MinPts = 5 # 定义半径内的最少的数据点的个数
11+
12+
def load_data(file_path):
13+
'''导入数据
14+
input: file_path(string):文件名
15+
output: data(mat):数据
16+
'''
17+
f = open(file_path)
18+
data = []
19+
for line in f.readlines():
20+
data_tmp = []
21+
lines = line.strip().split("\t")
22+
for x in lines:
23+
data_tmp.append(float(x.strip()))
24+
data.append(data_tmp)
25+
f.close()
26+
return np.mat(data)
27+
28+
def epsilon(data, MinPts):
29+
'''计算半径
30+
input: data(mat):训练数据
31+
MinPts(int):半径内的数据点的个数
32+
output: eps(float):半径
33+
'''
34+
m, n = np.shape(data)
35+
xMax = np.max(data, 0)
36+
xMin = np.min(data, 0)
37+
eps = ((np.prod(xMax - xMin) * MinPts * math.gamma(0.5 * n + 1)) / (m * math.sqrt(math.pi ** n))) ** (1.0 / n)
38+
return eps
39+
40+
def distance(data):
41+
m, n = np.shape(data)
42+
dis = np.mat(np.zeros((m, m)))
43+
for i in xrange(m):
44+
for j in xrange(i, m):
45+
# 计算i和j之间的欧式距离
46+
tmp = 0
47+
for k in xrange(n):
48+
tmp += (data[i, k] - data[j, k]) * (data[i, k] - data[j, k])
49+
dis[i, j] = np.sqrt(tmp)
50+
dis[j, i] = dis[i, j]
51+
return dis
52+
53+
def find_eps(distance_D, eps):
54+
ind = []
55+
n = np.shape(distance_D)[1]
56+
for j in xrange(n):
57+
if distance_D[0, j] <= eps:
58+
ind.append(j)
59+
return ind
60+
61+
def dbscan(data, eps, MinPts):
62+
m = np.shape(data)[0]
63+
# 区分核心点1,边界点0和噪音点-1
64+
types = np.mat(np.zeros((1, m)))
65+
sub_class = np.mat(np.zeros((1, m)))
66+
# 用于判断该点是否处理过,0表示未处理过
67+
dealed = np.mat(np.zeros((m, 1)))
68+
# 计算每个数据点之间的距离
69+
dis = distance(data)
70+
# 用于标记类别
71+
number = 1
72+
73+
# 对每一个点进行处理
74+
for i in xrange(m):
75+
# 找到未处理的点
76+
if dealed[i, 0] == 0:
77+
# 找到第i个点到其他所有点的距离
78+
D = dis[i, ]
79+
# 找到半径eps内的所有点
80+
ind = find_eps(D, eps)
81+
# 区分点的类型
82+
# 边界点
83+
if len(ind) > 1 and len(ind) < MinPts + 1:
84+
types[0, i] = 0
85+
sub_class[0, i] = 0
86+
# 噪音点
87+
if len(ind) == 1:
88+
types[0, i] = -1
89+
sub_class[0, i] = -1
90+
dealed[i, 0] = 1
91+
# 核心点
92+
if len(ind) >= MinPts + 1:
93+
types[0, i] = 1
94+
for x in ind:
95+
sub_class[0, x] = number
96+
# 判断核心点是否密度可达
97+
while len(ind) > 0:
98+
dealed[ind[0], 0] = 1
99+
D = dis[ind[0], ]
100+
tmp = ind[0]
101+
del ind[0]
102+
ind_1 = find_eps(D, eps)
103+
104+
if len(ind_1) > 1: # 处理非噪音点
105+
for x1 in ind_1:
106+
sub_class[0, x1] = number
107+
if len(ind_1) >= MinPts + 1:
108+
types[0, tmp] = 1
109+
else:
110+
types[0, tmp] = 0
111+
112+
for j in xrange(len(ind_1)):
113+
if dealed[ind_1[j], 0] == 0:
114+
dealed[ind_1[j], 0] = 1
115+
ind.append(ind_1[j])
116+
sub_class[0, ind_1[j]] = number
117+
number += 1
118+
119+
# 最后处理所有未分类的点为噪音点
120+
ind_2 = ((sub_class == 0).nonzero())[1]
121+
for x in ind_2:
122+
sub_class[0, x] = -1
123+
types[0, x] = -1
124+
125+
return types, sub_class
126+
127+
def save_result(file_name, source):
128+
f = open(file_name, "w")
129+
n = np.shape(source)[1]
130+
tmp = []
131+
for i in xrange(n):
132+
tmp.append(str(source[0, i]))
133+
f.write("\n".join(tmp))
134+
f.close()
135+
136+
if __name__ == "__main__":
137+
# 1、导入数据
138+
print "----------- 1、load data ----------"
139+
data = load_data("data.txt")
140+
# 2、计算半径
141+
print "----------- 2、calculate eps ----------"
142+
eps = epsilon(data, MinPts)
143+
# 3、利用DBSCAN算法进行训练
144+
print "----------- 3、DBSCAN -----------"
145+
types, sub_class = dbscan(data, eps, MinPts)
146+
# 4、保存最终的结果
147+
print "----------- 4、save result -----------"
148+
save_result("types", types)
149+
save_result("sub_class", sub_class)

0 commit comments

Comments
 (0)