-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_on_simulated_data_partition_stability.py
361 lines (251 loc) · 12.6 KB
/
test_on_simulated_data_partition_stability.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
353
354
355
356
357
358
359
360
# -*- coding: utf-8 -*-
"""
Created on Thu May 20 17:34:17 2021
@author: rfuchs
"""
import re
import os
os.chdir('C:/Users/rfuchs/Documents/GitHub/M1DGMM')
import pandas as pd
from copy import deepcopy
from gower import gower_matrix
from minisom import MiniSom
from kmodes.kmodes import KModes
from kmodes.kprototypes import KPrototypes
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import AgglomerativeClustering
from data_preprocessing import compute_nj
from m1dgmm import M1DGMM
from init_params import dim_reduce_init
import autograd.numpy as np
results_path = 'C:/Users/rfuchs/Documents/These/Experiences/' # Results storage
os.chdir('C:/Users/rfuchs/Documents/These/Stats/mixed_dgmm/datasets')
datasets = os.listdir('simulated')
nb_trials = 30
n_clusters = 4
###############################################################################
#### Simulated data: Assess the percentage of partition similarity ##########
###############################################################################
r = np.array([5, 2, 1])
k = [n_clusters, 3]
seed = 1
init_seed = 2
eps = 1E-02
it = 30
maxstep = 100
nb_trials = 30
type_detection_regex = {'yC[0-9]\.[0-9]{1,2}': 'continuous', 'yBer[0-9]\.[0-9]{1,2}': 'bernoulli',\
'yBin[0-9]\.[0-9]{1,2}': 'binomial' , 'yM[0-9]\.[0-9]{1,2}': 'categorical',\
'yOrdi[0-9]\.[0-9]{1,2}': 'ordinal'}
#===========================================#
# M1DGMM
#===========================================#
for dataset in datasets:
simu = pd.read_csv('simulated/' + dataset, sep = ',', decimal = ',').iloc[:,1:]
if simu.shape[1] == 0: # The separator is not constant..
simu = pd.read_csv('simulated/' + dataset, sep = ';', decimal = ',').iloc[:,1:]
y = simu.iloc[:,:-1]
numobs = len(y)
p = y.shape[1]
mdgmm_res = pd.DataFrame(columns = range(0,nb_trials), index = range(numobs))
# Determine the type from the name of the variable (dirty)
var_distrib = pd.Series(y.columns)
[var_distrib.replace(regex, var_type, regex = True, inplace = True)\
for regex, var_type in type_detection_regex.items()]
var_distrib = var_distrib.values
#===========================================#
# Formating the data
#===========================================#
# Encode categorical datas
le = LabelEncoder()
for col_idx, colname in enumerate(y.columns):
if var_distrib[col_idx] == 'categorical':
y[colname] = le.fit_transform(y[colname]).astype(np.str)
# Encode ordinal data
for col_idx, colname in enumerate(y.columns):
if var_distrib[col_idx] == 'ordinal':
if y[colname].min() != 0:
y[colname] = y[colname] - 1
nj, nj_bin, nj_ord, nj_categ = compute_nj(y, var_distrib)
y_np = y.values
nb_cont = np.sum(var_distrib == 'continuous')
cat_features = pd.Series(var_distrib).isin(['categorical', 'bernoulli']).to_list()
dtype = {y.columns[j]: np.str if cat_features[j] else np.float64 for j in range(p)}
y = y.astype(dtype, copy=True)
dm = gower_matrix(y, cat_features = cat_features)
#===========================================#
# Running the M1DGMM
#===========================================#
for i in range(nb_trials):
prince_init = dim_reduce_init(y, n_clusters, k, r, nj, var_distrib, seed = None,\
use_famd=True)
out = M1DGMM(y_np, n_clusters, r, k, prince_init, var_distrib, nj, it,\
eps, maxstep, seed, perform_selec = False, dm = dm)
mdgmm_res.iloc[:,i] = out['classes']
mdgmm_res = mdgmm_res.append({'dataset': dataset, 'it_id': i, 'classes': out['classes']},\
ignore_index=True)
mdgmm_res.to_csv(results_path + 'similar_partition/' + dataset,\
index = False, header = False)
#===========================================#
# Hierarchical clustering
#===========================================#
linkages = ['complete', 'average', 'single']
for dataset in datasets:
#===========================================#
# Formating the data
#===========================================#
simu = pd.read_csv('simulated/' + dataset, sep = ',', decimal = ',').iloc[:,1:]
if simu.shape[1] == 0: # The separator is not constant..
simu = pd.read_csv('simulated/' + dataset, sep = ';', decimal = ',').iloc[:,1:]
y = simu.iloc[:,:-1]
numobs = len(y)
p = y.shape[1]
# Ordinal and continuous are not categorical
cat_features = [re.search('y[CO]', col) == None for col in y.columns]
dtype = {y.columns[j]: np.str if cat_features[j] else np.float64 for j in range(p)}
y = y.astype(dtype, copy=True)
# Defining distances over the non encoded features
dm = gower_matrix(y, cat_features = cat_features)
for linky in linkages:
hierarch_res = pd.DataFrame(columns = range(0,nb_trials), index = range(numobs))
for i in range(nb_trials):
aglo = AgglomerativeClustering(n_clusters = 4, affinity ='precomputed',\
linkage = linky)
aglo_preds = aglo.fit_predict(dm)
hierarch_res.iloc[:,i] = aglo_preds
hierarch_res.to_csv(results_path + 'similar_partition/data/Hierarchical/' +\
dataset[:-4] + '_' + linky + '.csv', index = False, header = False)
#===========================================#
# DBSCAN clustering
#===========================================#
for dataset in datasets:
#===========================================#
# Formating the data
#===========================================#
simu = pd.read_csv('simulated/' + dataset, sep = ',', decimal = ',').iloc[:,1:]
if simu.shape[1] == 0: # The separator is not constant..
simu = pd.read_csv('simulated/' + dataset, sep = ';', decimal = ',').iloc[:,1:]
y = simu.iloc[:,:-1]
numobs = len(y)
y_nenc_typed = deepcopy(y.astype(np.object))
p = y.shape[1]
# Ordinal and continuous are not categorical
cat_features = [re.search('y[CO]', col) == None for col in y.columns]
dtype = {y.columns[j]: np.str if cat_features[j] else np.float64 for j in range(p)}
y = y.astype(dtype, copy=True)
# Defining distances over the non encoded features
dm = gower_matrix(y, cat_features = cat_features)
# Scale the continuous variables
cont_features = [re.search('yC', col) != None for col in y.columns]
y_scale = y.values
ss = StandardScaler()
y_scale[:, cont_features] = ss.fit_transform(y_scale[:, cont_features])
lf_size = np.arange(1,6) * 10
epss = np.linspace(0.01, 5, 5)
min_ss = np.arange(1, 5)
data_to_fit = ['scaled', 'gower']
for lfs in lf_size:
print("Leaf size:", lfs)
for eps in epss:
for min_s in min_ss:
for data in data_to_fit:
dbs_res = pd.DataFrame(columns = range(0,nb_trials), index = range(numobs))
for i in range(nb_trials):
if data == 'gower':
dbs = DBSCAN(eps = eps, min_samples = min_s, \
metric = 'precomputed', leaf_size = lfs).fit(dm)
else:
dbs = DBSCAN(eps = eps, min_samples = min_s, leaf_size = lfs).fit(y_scale)
dbs_res.iloc[:,i] = dbs.labels_
dbs_res.to_csv(results_path + 'similar_partition/data/DBSCAN/dbscan' + \
dataset[:-4] + '_' + str(lfs) + '_' + str(eps) + '_' + str(min_s) + '_' +\
str(data) + '.csv', index = False, header = False)
#===========================================#
# Partitional algorithm
#===========================================#
inits = ['Huang', 'Cao', 'random']
for dataset in datasets:
#===========================================#
# Formating the data
#===========================================#
ss = StandardScaler()
simu = pd.read_csv('simulated/' + dataset, sep = ',', decimal = ',').iloc[:,1:]
if simu.shape[1] == 0: # The separator is not constant..
simu = pd.read_csv('simulated/' + dataset, sep = ';', decimal = ',').iloc[:,1:]
y = simu.iloc[:,:-1]
numobs = len(y)
cont_features = [re.search('yC', col) != None for col in y.columns]
y_scale = y.values
y_scale[:, cont_features] = ss.fit_transform(y_scale[:, cont_features])
for init in inits:
print(init)
part_res_modes = pd.DataFrame(columns = range(0,nb_trials), index = range(numobs))
for i in range(nb_trials):
km = KModes(n_clusters= n_clusters, init=init, n_init=10, verbose=0)
kmo_labels = km.fit_predict(y_scale)
part_res_modes.iloc[:,i] = kmo_labels
part_res_modes.to_csv(results_path + 'similar_partition/data/KMODES/' + \
dataset[:-4] + '_' + init + '.csv',\
index = False, header = False)
#===========================================#
# K prototypes
#===========================================#
inits = ['Huang', 'Cao', 'random']
for dataset in datasets:
#===========================================#
# Formating the data
#===========================================#
ss = StandardScaler()
simu = pd.read_csv('simulated/' + dataset, sep = ',', decimal = ',').iloc[:,1:]
if simu.shape[1] == 0: # The separator is not constant..
simu = pd.read_csv('simulated/' + dataset, sep = ';', decimal = ',').iloc[:,1:]
y = simu.iloc[:,:-1]
numobs = len(y)
cont_features = [re.search('yC', col) != None for col in y.columns]
cat_features = [not(el) for el in cont_features]
y_scale = y.values
y_scale[:, cont_features] = ss.fit_transform(y_scale[:, cont_features])
for init in inits:
print(init)
part_res_proto = pd.DataFrame(columns = range(0,nb_trials), index = range(numobs))
for i in range(nb_trials):
km = KPrototypes(n_clusters = n_clusters, init = init, n_init=10, verbose=0)
kmo_labels = km.fit_predict(y_scale, categorical = np.where(cat_features)[0].tolist())
part_res_proto.iloc[:,i] = kmo_labels
part_res_proto.to_csv(results_path + 'similar_partition/data/KPROTOTYPES/' + \
dataset[:-4] + '_' + init + '.csv',\
index = False, header = False)
#****************************
# Neural-network based
#****************************
sigmas = np.linspace(0.001, 3, 5)
lrs = np.linspace(0.0001, 0.5, 10)
for dataset in datasets:
#===========================================#
# Formating the data
#===========================================#
ss = StandardScaler()
simu = pd.read_csv('simulated/' + dataset, sep = ',', decimal = ',').iloc[:,1:]
if simu.shape[1] == 0: # The separator is not constant..
simu = pd.read_csv('simulated/' + dataset, sep = ';', decimal = ',').iloc[:,1:]
y = simu.iloc[:,:-1]
numobs = len(y)
y_scale = y.values
cont_features = [re.search('yC', col) != None for col in y.columns]
y_scale[:, cont_features] = ss.fit_transform(y_scale[:, cont_features])
for sig in sigmas:
for lr in lrs:
som_res = pd.DataFrame(columns = range(0,nb_trials), index = range(numobs))
for i in range(nb_trials):
#try:
som = MiniSom(n_clusters, 1, y_scale.shape[1], sigma = sig, learning_rate = lr) # initialization of 6x6 SOM
som.train(y_scale.astype(float), 100) # trains the SOM with 100 iterations
som_labels = [som.winner(y_scale.astype(float)[i])[0] for i in range(numobs)]
som_res.iloc[:,i] = som_labels
#except TypeError:
#som_res.iloc[:,i] = np.nan
som_res.to_csv(results_path + 'similar_partition/data/SOM/' + \
dataset[:-4] + '_' + str(sig) + '_' + str(lr) + '.csv',\
index = False, header = False)