-
Notifications
You must be signed in to change notification settings - Fork 7
/
kcell.py
352 lines (290 loc) · 9.77 KB
/
kcell.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# kcell.py
# python3
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import model_selection, svm, metrics, cluster, tree
import kkeras
import numpy as np
import seaborn as sns
def GET_clsf2_by_clst( nb_classes):
def clsf2_by_clst( Xpart_cf, Xpart_ct):
"""
Clustering is performed and then, classification performed by clustered indices.
"""
cl_model = cluster.KMeans(n_clusters=nb_classes)
cl_model.fit(Xpart_ct)
yint = cl_model.predict( Xpart_ct)
X_train, X_test, y_train, y_test = \
model_selection.train_test_split( Xpart_cf, yint, test_size = 0.2)
model = tree.DecisionTreeClassifier()
model.fit( X_train, y_train)
dt_score = model.score( X_test, y_test)
print( "DT-C:", dt_score)
model = svm.SVC( kernel = 'linear')
model.fit( X_train, y_train)
sv_score = model.score( X_test, y_test)
print( "SVC:", sv_score)
model = kkeras.MLPC( [Xpart_cf.shape[1], 30, 10, nb_classes])
model.fit( X_train, y_train, X_test, y_test, nb_classes)
mlp_score = model.score( X_test, y_test)
print( "MLP:", mlp_score)
return dt_score, sv_score, mlp_score
return clsf2_by_clst
def GET_clsf2_by_yint( nb_classes):
def clsf2_by_yint( X1part, yint):
"""
classification is performed by yint
"""
X_train, X_test, y_train, y_test = \
model_selection.train_test_split( X1part, yint, test_size = 0.2)
model = tree.DecisionTreeClassifier()
model.fit( X_train, y_train)
dt_score = model.score( X_test, y_test)
print( "DT:", dt_score)
model = svm.SVC( kernel = 'linear')
model.fit( X_train, y_train)
sv_score = model.score( X_test, y_test)
print( "SVC:", sv_score)
model = kkeras.MLPC( [X1part.shape[1], 30, 10, nb_classes])
model.fit( X_train, y_train, X_test, y_test, nb_classes)
mlp_score = model.score( X_test, y_test)
print( "MLP:", mlp_score)
return dt_score, sv_score, mlp_score
return clsf2_by_yint
def pd_clsf2_by_clst( ix, Xpart_ct, Xpart_cf, nb_classes):
VI = {1:"Velocity", 2:"Intensity", 12:"Combined"}
print( "Type", ix, "- Clustering:", ix[1], "Classification:", ix[0])
s_l = GET_clsf2_by_clst(nb_classes)(Xpart_cf, Xpart_ct)
df_i = pd.DataFrame()
df_i["Type"] = ["KMenas: " + str( ix)] * 3
df_i["Clustering"] = [ VI[ix[0]]] * 3
df_i["Classification"] = [ VI[ix[1]]] * 3
df_i["Clustering method"] = [ "KMeans"] * 3
df_i["Classification method"] = [ "DT", "SVC", "DNN"]
df_i["Pc"] = s_l
return df_i
def pd_clsf2_by_yint( ix, yint, Xpart_cf, nb_classes):
VI = {1:"Velocity", 2:"Intensity", 12:"Combined"}
print( "Type", ix, "- Clustering:", ix[1], "Classification:", ix[0])
s_l = GET_clsf2_by_yint(nb_classes)(Xpart_cf, yint)
df_i = pd.DataFrame()
df_i["Type"] = ["Science: "+str( ix)] * 3
df_i["Clustering"] = [ VI[ix[0]]] * 3
df_i["Classification"] = [ VI[ix[1]]] * 3
df_i["Clustering method"] = [ "Sceince method"] * 3
df_i["Classification method"] = [ "DT", "SVC", "DNN"]
df_i["Pc"] = s_l
return df_i
class _Subclustering_r0():
def __init__(self, X1part, X2part, y, cell,
X1_ylim = [-1.5, 1.5], X2_ylim = [-2, 4],
cmethod = "KMenas",
cparam_d = {"n_clusters": 2}):
self.X1part = X1part
self.X2part = X2part
self.y = y
self.cell = cell
self.X1_ylim = X1_ylim
self.X2_ylim = X2_ylim
self.cmethod = cmethod
self.cparam_d = cparam_d
def show_both( self, c):
X1part = self.X1part
X2part = self.X2part
y = self.y
cell = self.cell
X1_ylim = self.X1_ylim
X2_ylim = self.X2_ylim
cmethod = self.cmethod
cparam_d = self.cparam_d
#print("Cluster:", c)
X3_int = X2part[ np.where(y==c)[0],:]
X3_vel = X1part[ np.where(y==c)[0],:]
#km = cluster.KMeans(2)
#km = getattr(cluster, cmethod)(2)
km = getattr(cluster, cmethod)(**cparam_d)
y3 = km.fit_predict( X3_int)
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
#print("Intensity")
n_0 = X3_int[ np.where( y3==0)[0]].shape[0]
n_1 = X3_int[ np.where( y3==1)[0]].shape[0]
sns.tsplot( X3_int[ np.where( y3==0)[0],:], color="blue")
sns.tsplot( X3_int[ np.where( y3==1)[0],:], color="green")
plt.ylim(X2_ylim)
plt.title("Cluster{0}:X2 {1}:{2}".format(c, n_0, n_1))
#plt.show()
plt.subplot(1,2,2)
#print("Velocity")
sns.tsplot( X3_vel[ np.where( y3==0)[0],:], color="blue")
sns.tsplot( X3_vel[ np.where( y3==1)[0],:], color="green")
plt.ylim(X1_ylim)
plt.title("Cluster{0}:X1 {1}:{2}".format(c, n_0, n_1))
plt.show()
cell3 = cell[ np.where(y==c)[0]]
plt.subplot(1,2,1)
plt.stem( cell3[np.where( y3==0)[0]], linefmt='b-', markerfmt='bo')
plt.title("Cell Index - Subcluster 1")
plt.subplot(1,2,2)
plt.stem( cell3[np.where( y3==1)[0]], linefmt='g-', markerfmt='go')
plt.title("Cell Index - Subcluster 2")
plt.show()
return y3
def show_both_cell( self, c, cell_id):
X1part = self.X1part
X2part = self.X2part
y = self.y
cell = self.cell
X1_ylim = self.X1_ylim
X2_ylim = self.X2_ylim
cmethod = self.cmethod
X3_int = X2part[ np.where(y==c)[0],:]
X3_vel = X1part[ np.where(y==c)[0],:]
cell3 = cell[ np.where(y==c)[0]]
#km = cluster.KMeans(2)
#km = getattr(cluster, cmethod)(2)
km = getattr(cluster, cmethod)(**cparam_d)
y3 = km.fit_predict( X3_int)
# redefine based on cell_id
X3_int = X3_int[ np.where(cell3==cell_id)[0],:]
X3_vel = X3_vel[ np.where(cell3==cell_id)[0],:]
y3 = y3[np.where(cell3==cell_id)[0]]
n_0 = X3_int[ np.where( y3==0)[0]].shape[0]
n_1 = X3_int[ np.where( y3==1)[0]].shape[0]
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
if n_0 > 0: sns.tsplot( X3_int[ np.where( y3==0)[0],:], color="blue")
if n_1 > 0: sns.tsplot( X3_int[ np.where( y3==1)[0],:], color="green")
plt.ylim(X2_ylim)
plt.title("Cluster{0}:Intensity {1}:{2}".format(c, n_0, n_1))
#plt.show()
plt.subplot(1,2,2)
#print("Velocity")
if n_0 > 0: sns.tsplot( X3_vel[ np.where( y3==0)[0],:], color="blue")
if n_1 > 0: sns.tsplot( X3_vel[ np.where( y3==1)[0],:], color="green")
plt.ylim(X1_ylim)
plt.title("Cluster{0}:Velocity {1}:{2}".format(c, n_0, n_1))
plt.show()
class Subclustering():
def __init__(self, X1part, X2part, y, cell,
X1_ylim = [-1.5, 1.5], X2_ylim = [-2, 4],
cmethod = "KMenas",
cparam_d = {"n_clusters": 2}):
self.X1part = X1part
self.X2part = X2part
self.y = y
self.cell = cell
self.X1_ylim = X1_ylim
self.X2_ylim = X2_ylim
self.cmethod = cmethod
self.cparam_d = cparam_d
def show_both( self, c):
X1part = self.X1part
X2part = self.X2part
y = self.y
cell = self.cell
X1_ylim = self.X1_ylim
X2_ylim = self.X2_ylim
cmethod = self.cmethod
cparam_d = self.cparam_d
#print("Cluster:", c)
X3_int = X2part[ np.where(y==c)[0],:]
X3_vel = X1part[ np.where(y==c)[0],:]
#km = cluster.KMeans(2)
#km = getattr(cluster, cmethod)(2)
km = getattr(cluster, cmethod)(**cparam_d)
y3 = km.fit_predict( X3_int)
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
#print("Intensity")
n_0 = X3_int[ np.where( y3==0)[0]].shape[0]
n_1 = X3_int[ np.where( y3==1)[0]].shape[0]
sns.tsplot( X3_int[ np.where( y3==0)[0],:], color="blue")
sns.tsplot( X3_int[ np.where( y3==1)[0],:], color="green")
plt.ylim(X2_ylim)
plt.title("Cluster{0}:X2 {1}:{2}".format(c, n_0, n_1))
#plt.show()
plt.subplot(1,2,2)
#print("Velocity")
sns.tsplot( X3_vel[ np.where( y3==0)[0],:], color="blue")
sns.tsplot( X3_vel[ np.where( y3==1)[0],:], color="green")
plt.ylim(X1_ylim)
plt.title("Cluster{0}:X1 {1}:{2}".format(c, n_0, n_1))
plt.show()
cell3 = cell[ np.where(y==c)[0]]
plt.subplot(1,2,1)
plt.stem( cell3[np.where( y3==0)[0]], linefmt='b-', markerfmt='bo')
plt.title("Cell Index - Subcluster 1")
plt.subplot(1,2,2)
plt.stem( cell3[np.where( y3==1)[0]], linefmt='g-', markerfmt='go')
plt.title("Cell Index - Subcluster 2")
plt.show()
return y3
def show_both_cell( self, c, cell_id):
X1part = self.X1part
X2part = self.X2part
y = self.y
cell = self.cell
X1_ylim = self.X1_ylim
X2_ylim = self.X2_ylim
cmethod = self.cmethod
X3_int = X2part[ np.where(y==c)[0],:]
X3_vel = X1part[ np.where(y==c)[0],:]
cell3 = cell[ np.where(y==c)[0]]
km = getattr(cluster, cmethod)(**cparam_d)
y3 = km.fit_predict( X3_int)
# redefine based on cell_id
X3_int = X3_int[ np.where(cell3==cell_id)[0],:]
X3_vel = X3_vel[ np.where(cell3==cell_id)[0],:]
y3 = y3[np.where(cell3==cell_id)[0]]
n_0 = X3_int[ np.where( y3==0)[0]].shape[0]
n_1 = X3_int[ np.where( y3==1)[0]].shape[0]
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
if n_0 > 0: sns.tsplot( X3_int[ np.where( y3==0)[0],:], color="blue")
if n_1 > 0: sns.tsplot( X3_int[ np.where( y3==1)[0],:], color="green")
plt.ylim(X2_ylim)
plt.title("Cluster{0}:Intensity {1}:{2}".format(c, n_0, n_1))
#plt.show()
plt.subplot(1,2,2)
#print("Velocity")
if n_0 > 0: sns.tsplot( X3_vel[ np.where( y3==0)[0],:], color="blue")
if n_1 > 0: sns.tsplot( X3_vel[ np.where( y3==1)[0],:], color="green")
plt.ylim(X1_ylim)
plt.title("Cluster{0}:Velocity {1}:{2}".format(c, n_0, n_1))
plt.show()
def show_both_kmeans( self, c):
X1part = self.X1part
X2part = self.X2part
y = self.y
cell = self.cell
X1_ylim = self.X1_ylim
X2_ylim = self.X2_ylim
cmethod = self.cmethod
cparam_d = self.cparam_d
nc = cparam_d["n_clusters"]
#print("Cluster:", c)
X3_int = X2part[ y==c,:]
X3_vel = X1part[ y==c,:]
#km = cluster.KMeans(2)
#km = getattr(cluster, cmethod)(2)
assert cmethod == "KMeans"
km = cluster.KMeans( nc)
y3 = km.fit_predict( X3_int)
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
#print("Intensity")
n_l = [ X3_int[ y3==i].shape[0] for i in range(nc)]
for i in range(nc):
sns.tsplot( X3_int[ y3==i,:], color=plt.cm.rainbow(i/nc))
plt.ylim(X2_ylim)
plt.title("Cluster{0}:X2 {1}".format(c, n_l))
#plt.show()
plt.subplot(1,2,2)
#print("Velocity")
for i in range(nc):
sns.tsplot( X3_vel[ y3==i,:], color=plt.cm.rainbow(i/nc))
plt.ylim(X1_ylim)
plt.title("Cluster{0}:X1 {1}".format(c, n_l))
plt.show()
return y3