forked from UTMS/SOMPYforLVQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codebook.py
executable file
·210 lines (169 loc) · 7.06 KB
/
codebook.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import numpy as np
from sklearn.decomposition import RandomizedPCA
from decorators import timeit
class InvalidNodeIndexError(Exception):
pass
class InvalidMapsizeError(Exception):
pass
class Codebook(object):
def __init__(self, mapsize, lattice='rect'):
self.lattice = lattice
if 2 == len(mapsize):
_size = [1, np.max(mapsize)] if 1 == np.min(mapsize) else mapsize
# mapsize has 2 dimentions,[27,27],
elif 1 == len(mapsize):
_size = [1, mapsize[0]]
print('input was considered as the numbers of nodes')
print('map size is [{dlen},{dlen}]'.format(dlen=int(mapsize[0]/2)))
else:
raise InvalidMapsizeError(
"Mapsize is expected to be a 2 element list or a single int")
self.mapsize = _size
self.nnodes = mapsize[0]*mapsize[1]
# if mapsize has 2 dimentions,[27,27], nnode is 729
self.matrix = np.asarray(self.mapsize)
self.initialized = False
@timeit()
def random_initialization(self, data):
"""
:param data: data to use for the initialization
:returns: initialized matrix with same dimension as input data
"""
mn = np.tile(np.min(data, axis=0), (self.nnodes, 1))
# repeat the smallest 9 values for nnodes times.
mx = np.tile(np.max(data, axis=0), (self.nnodes, 1))
self.matrix = mn + (mx-mn)*(np.random.rand(self.nnodes, data.shape[1]))
#random initialize nnodes values within min and max
self.initialized = True
@timeit()
def pca_linear_initialization(self, data):
"""
We initialize the map, just by using the first two first eigen vals and
eigenvectors
Further, we create a linear combination of them in the new map by
giving values from -1 to 1 in each
X = UsigmaWT
XTX = Wsigma^2WT
T = XW = Usigma
// Transformed by W EigenVector, can be calculated by multiplication
// PC matrix by eigenval too
// Further, we can get lower ranks by using just few of the eigen
// vevtors
T(2) = U(2)sigma(2) = XW(2) ---> 2 is the number of selected
eigenvectors
(*) Note that 'X' is the covariance matrix of original data
:param data: data to use for the initialization
:returns: initialized matrix with same dimension as input data
"""
cols = self.mapsize[1]
coord = None
pca_components = None
if np.min(self.mapsize) > 1:
coord = np.zeros((self.nnodes, 2))
pca_components = 2
for i in range(0, self.nnodes):
coord[i, 0] = int(i / cols) # x
coord[i, 1] = int(i % cols) # y
elif np.min(self.mapsize) == 1:
coord = np.zeros((self.nnodes, 1))
pca_components = 1
for i in range(0, self.nnodes):
coord[i, 0] = int(i % cols) # y
mx = np.max(coord, axis=0)
mn = np.min(coord, axis=0)
coord = (coord - mn)/(mx-mn)
coord = (coord - .5)*2
me = np.mean(data, 0)
data = (data - me)
tmp_matrix = np.tile(me, (self.nnodes, 1))
# Randomized PCA is scalable
pca = RandomizedPCA(n_components=pca_components)
pca.fit(data)
eigvec = pca.components_
eigval = pca.explained_variance_
norms = np.sqrt(np.einsum('ij,ij->i', eigvec, eigvec))
eigvec = ((eigvec.T/norms)*eigval).T
for j in range(self.nnodes):
for i in range(eigvec.shape[0]):
tmp_matrix[j, :] = tmp_matrix[j, :] + coord[j, i]*eigvec[i, :]
self.matrix = np.around(tmp_matrix, decimals=6)
self.initialized = True
def grid_dist(self, node_ind):
"""
Calculates grid distance based on the lattice type.
:param node_ind: number between 0 and number of nodes-1. Depending on
the map size, starting from top left
:returns: matrix representing the distance matrix
"""
if self.lattice == 'rect':
return self._rect_dist(node_ind)
elif self.lattice == 'mirror':
return self._mirror(node_ind)
elif self.lattice == 'hexa':
return self._hexa_dist(node_ind)
def _hexa_dist(self, node_ind):
raise NotImplementedError()
def _rect_dist(self, node_ind):
"""
Calculates the distance of the specified node to the other nodes in the
matrix, generating a distance matrix
Ej. The distance matrix for the node_ind=5, that corresponds to
the_coord (1,1)
array([[2, 1, 2, 5],
[1, 0, 1, 4],
[2, 1, 2, 5],
[5, 4, 5, 8]])
:param node_ind: number between 0 and number of nodes-1. Depending on
the map size, starting from top left
:returns: matrix representing the distance matrix
"""
rows = self.mapsize[0]
cols = self.mapsize[1]
dist = None
# bmu should be an integer between 0 to no_nodes
if 0 <= node_ind <= (rows*cols):
# node square must be smaller than mapsize
node_col = int(node_ind % cols)
node_row = int(node_ind / cols)
else:
raise InvalidNodeIndexError(
"Node index '%s' is invalid" % node_ind)
if rows > 0 and cols > 0:
r = np.arange(0, rows, 1)[:, np.newaxis]
c = np.arange(0, cols, 1)
dist2 = (r-node_row)**2 + (c-node_col)**2
# a horizontal array adds a vertical array, equals a square array
dist = dist2.ravel()
else:
raise InvalidMapsizeError(
"One or both of the map dimensions are invalid. "
"Cols '%s', Rows '%s'".format(cols=cols, rows=rows))
return dist
def _mirror(self, node_ind):
"distance in a mirror world, leftmost is rightmost, uppermost is bottommost."
rows = self.mapsize[0]
cols = self.mapsize[1]
dist = None
# bmu should be an integer between 0 to no_nodes
if 0 <= node_ind <= (rows*cols):
# node square must be smaller than mapsize
node_col = int(node_ind % cols)
node_row = int(node_ind / cols)
else:
raise InvalidNodeIndexError(
"Node index '%s' is invalid" % node_ind)
dist4 = []
if rows > 0 and cols > 0:
r = np.arange(0, rows, 1)[:, np.newaxis]
c = np.arange(0, cols, 1)
for i in range(3):
for j in range(3):
dist3 = (r-node_row+(i-1)*rows)**2 + (c-node_col+(j-1)*cols)**2
# a horizontal array adds a vertical array, equals a square array
dist4.append(dist3.ravel())
dist = np.amin(dist4, axis=0)
else:
raise InvalidMapsizeError(
"One or both of the map dimensions are invalid. "
"Cols '%s', Rows '%s'".format(cols=cols, rows=rows))
return dist