forked from mrkschan/py-dbscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_glass.py
executable file
·56 lines (45 loc) · 1.77 KB
/
run_glass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
import dbscan, measure
import copy
cls_idx = 10
dataset = []
with open('dataset/glass/glass.data') as data_f:
for l in data_f.readlines():
r = l.strip().split(',')
for i in xrange(1, cls_idx):
r[i] = float(r[i])
r = r[1:]
dataset.append(dbscan.data(r, cls_idx-1))
data_f.close()
# normalize all dimension scale to range float(0, 1)
mins = [copy.deepcopy(min(dataset, key=lambda d: d.tuple[i])) for i in xrange(0, cls_idx-1)]
maxs = [copy.deepcopy(max(dataset, key=lambda d: d.tuple[i])) for i in xrange(0, cls_idx-1)]
for d in dataset:
for i in xrange(0, cls_idx-1):
d.tuple[i] = float(d.tuple[i] - mins[i].tuple[i]) / (maxs[i].tuple[i] - mins[i].tuple[i])
# print all combination of k and sd
for k in xrange(2, 5):
for sd_away in xrange(0, 5):
try:
kdist = dbscan.k_distance(dataset, k, sd_away)
except:
print 'kdist anchor out of range, skipped'
print
continue
cluster = dbscan.dbscan(dataset, kdist, k)
if len(cluster) == 0:
print 'k:', k, 'sd:', sd_away, 'kdist:', kdist, 'no. of cluster:', len(cluster)
print
continue
cc = [measure.cls_err(c) for c in cluster]
errs = []
clss = []
for err, cls in cc:
errs.append(err)
clss.append(cls)
cp = [len(c) for c in cluster]
print 'k:', k, 'sd:', sd_away, 'kdist:', kdist, 'no. of cluster:', len(cluster)
for i in xrange(0, len(cluster)):
print 'cluster:', i, 'no. of pt. in cluster:', cp[i], 'impurity (classification error):', errs[i], 'majority:', clss[i]
print 'mean impurity:', float(sum(errs)) / len(errs), 'sum of pt.:', sum(cp)
print