-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
151 lines (136 loc) · 3.78 KB
/
utils.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python3.4
# Utility functions
# (c) Mohammad H. Mofrad, 2017
# (e) hasanzadeh@cs.pitt.edu
import numpy as np
import os
# Debug level
VERBOSE = False
# Read input text file and
# create x matrix as the input data and
# create y column vector as data labels
def read(FILE):
if os.path.isfile(FILE):
file = open(FILE, 'r')
lines = tuple(file)
file.close()
data = []
for line in lines:
data.append(line.rstrip().split(","))
if VERBOSE:
print(data[-1])
else:
print(FILE, 'does not exist')
exit(0)
data = np.array(data)
x = data[:,0:-1]
x = x.astype(np.float)
y = np.zeros(len(data))
uniq = np.unique(data[:,-1])
for i in range(0,len(uniq)):
idx = (data[:,-1] == uniq[i])
if any(idx):
y[idx] = i
return(x, y)
# Calculate the accuracy of centroid-based clusting algorithms
def accuracy(c, y, k):
if VERBOSE:
print(c)
print(y)
n = len(y)
kk = np.zeros(k)
o = 0
e = 0
idxx = []
for i in range(k):
a = np.arange(n)
idxa = a[y == i]
for j in range(len(idxa)):
kk[int(c[j+o])] = kk[int(c[j+o])] + 1
if idxx:
for l in idxx:
kk[l] = 0
o = o + len(idxa)
idx = np.argmax(kk)
idxx.append(idx)
val = kk[idx]
e = e + (val/len(y[y == i]))
kk = np.zeros(k)
e = e/k
return(e)
# Calculate the accuracy of density-based clusting algorithms
# @param c: Cluster asignment
# @param y: Data labels
# @param k1: True number of clusters
# @param k2: Estimated number of clusters
def accuracy_(c, y, k1, k2):
if VERBOSE:
print(c)
print(y)
n = len(y)
kk = np.zeros(k2)
o = 0
e = 0
idxx = []
for i in range(k1):
a = np.arange(n)
idxa = a[y == i]
for j in range(len(idxa)):
kk[int(c[j+o])] = kk[int(c[j+o])] + 1
if idxx:
for l in idxx:
kk[l] = 0
o = o + len(idxa)
idx = np.argmax(kk)
idxx.append(idx)
val = kk[idx]
e = e + (val/len(y[y == i]))
kk = np.zeros(k2)
e = e/k1
return(e)
# Silhouette Coefficient
def silhouette(x, c, me):
if VERBOSE:
print(c)
print(y)
n = len(c)
s = np.zeros((n,3))
for i in range(n):
dist = np.sqrt(np.sum(np.power(x[i,:] - me,2), axis=1))
dd = np.argsort(dist)
aa = np.arange(n)
for j in range(2):
aa = np.arange(n)
idx = aa[c == dd[j]]
l = len(idx)
if l:
for o in idx:
s[o,j] = s[o,j] + np.sqrt(np.sum(np.power(x[i,:] - x[o,:] ,2)))
s[o,j] = s[o,j]/l
s = np.mean((s[:,0] - s[:,1])/np.amax(s[:,0:2], axis=1))
return(s)
# Learning Automata action selection
def actionselection(action, probability, numactions, numdims):
for i in range(numdims):
a = np.random.choice(np.arange(0, numactions), p = probability[:,i])
mask = np.zeros(numactions,dtype=bool)
#print(i, a, mask)
mask[a] = True
action[mask,i] = 1
action[~mask,i] = 0
return(action)
# Learning Automata probability update
def probabilityupdate(action, probability, numactions, numdims, signal, alpha, beta):
if(numactions > 1):
for i in range(numdims):
a = np.where(action[:,i] == 1)
mask = np.zeros(numactions,dtype=bool)
mask[a] = True
#if not signal: # for single value reinforcement signals
if not signal[i]:
probability[mask,i] = probability[mask,i] + alpha * (1 - probability[mask,i])
probability[~mask,i] = (1 - alpha) * probability[~mask,i]
else:
probability[mask,i] = (1 - beta) * probability[mask,i]
probability[~mask,i] = (beta/(numactions-1)) + (1-beta) * probability[~mask,i]
return(probability)