-
Notifications
You must be signed in to change notification settings - Fork 0
/
Experiment2.py
711 lines (518 loc) · 23.7 KB
/
Experiment2.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# ## Python 3.8
# ############################33
# import numpy as np
# import pandas as pd
# from sklearn.model_selection import train_test_split
# from sklearn import preprocessing
# import matplotlib.pyplot as plt
# from os import path
# from sklearn.preprocessing import StandardScaler
# from sklearn.decomposition import PCA, FastICA
# from sklearn.random_projection import GaussianRandomProjection
# from sklearn.metrics import mean_squared_error
# from sklearn.preprocessing import OneHotEncoder
#############################33
# import mlrose_hiive
import numpy as np
import pandas as pd
# from mlrose_hiive import QueensGenerator, MaxKColorGenerator, TSPGenerator, FlipFlopGenerator, KnapsackGenerator,ContinuousPeaksGenerator
# from mlrose_hiive import SARunner, GARunner, NNGSRunner, MIMICRunner, RHCRunner
# # import itertools as it
# import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
from imblearn.over_sampling import RandomOverSampler
from sklearn import preprocessing
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
# # from sklearn.model_selection import train_test_split, StratifiedKFold, RandomizedSearchCV, KFold
# from sklearn.tree import DecisionTreeClassifier
from sklearn.neural_network import MLPClassifier
# from sklearn.ensemble import AdaBoostClassifier
# from sklearn.ensemble import GradientBoostingClassifier
# from sklearn import svm
# # from sklearn.neighbors import NearestNeighbors
# from sklearn.neighbors import KNeighborsClassifier
#
from sklearn.model_selection import cross_validate
from sklearn.model_selection import learning_curve
from sklearn.model_selection import validation_curve
from sklearn.metrics import accuracy_score
from sklearn.metrics import recall_score
from sklearn.metrics import log_loss
# from sklearn.metrics import confusion_matrix
#
# from warnings import simplefilter
# from sklearn.exceptions import ConvergenceWarning
import time as tm
# # from sklearn import metrics
from os import path
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
from sklearn.metrics import silhouette_samples, silhouette_score
import matplotlib.cm as cm
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA, FastICA
from sklearn.random_projection import GaussianRandomProjection
from sklearn.naive_bayes import GaussianNB
from sklearn.datasets import load_wine
from sklearn.pipeline import make_pipeline
from sklearn.datasets import fetch_openml
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectFromModel # <<< ***
from scipy.spatial.distance import cdist
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import mean_squared_error
def km(X,cl=0): # k-means clustering
if cl == 2:
clusterer = KMeans(n_clusters=2, random_state=10)
cluster_labels = clusterer.fit_predict(X)
return cluster_labels
elif cl == 4:
clusterer = KMeans(n_clusters=4, random_state=10)
cluster_labels = clusterer.fit_predict(X)
return cluster_labels
distortions = []
inertias = []
for n_clusters in range(1, 11):
clusterer = KMeans(n_clusters=n_clusters, random_state=10)
clusterer.fit(X)
distortions.append(sum(np.min(cdist(X, clusterer.cluster_centers_,'euclidean'), axis=1)) / X.shape[0])
inertias.append(clusterer.inertia_)
plt.plot(range(1, 11), distortions, 'og-')
plt.xlabel('The number of clusters K')
plt.ylabel('Distortion')
plt.title('The Elbow plot analysis for the various clusters using Distortion')
# plt.show()
plt.savefig('Customer_KM_Elbow_dis.png') # save plot
plt.close()
plt.plot(range(1, 11), inertias, 'og-')
plt.xlabel('The number of clusters K')
plt.ylabel('Inertia')
plt.title('The Elbow plot analysis for the various clusters using Inertia')
# plt.show()
plt.savefig('Customer_KM_Elbow_in.png') # save plot
range_n_clusters = [2, 3, 4, 5]
for n_clusters in range_n_clusters:
# Create a subplot with 1 row and 2 columns
# fig, (ax1, ax2) = plt.subplots(1, 2) # <<<<<<<<
fig, (ax1) = plt.subplots(1)
# fig.set_size_inches(18, 7)
# The 1st subplot is the silhouette plot
# The silhouette coefficient can range from -1, 1 but in this example all
# lie within [-0.1, 1]
ax1.set_xlim([-0.1, 1])
# The (n_clusters+1)*10 is for inserting blank space between silhouette
# plots of individual clusters, to demarcate them clearly.
ax1.set_ylim([0, len(X) + (n_clusters + 1) * 10])
# Initialize the clusterer with n_clusters value and a random generator
# seed of 10 for reproducibility.
clusterer = KMeans(n_clusters=n_clusters, random_state=10)
cluster_labels = clusterer.fit_predict(X)
# The silhouette_score gives the average value for all the samples.
# This gives a perspective into the density and separation of the formed
# clusters
silhouette_avg = silhouette_score(X, cluster_labels)
print(
"For n_clusters =",
n_clusters,
"The average silhouette_score is :",
silhouette_avg,
)
# Compute the silhouette scores for each sample
sample_silhouette_values = silhouette_samples(X, cluster_labels)
y_lower = 10
for i in range(n_clusters):
# Aggregate the silhouette scores for samples belonging to
# cluster i, and sort them
ith_cluster_silhouette_values = sample_silhouette_values[cluster_labels == i]
ith_cluster_silhouette_values.sort()
size_cluster_i = ith_cluster_silhouette_values.shape[0]
y_upper = y_lower + size_cluster_i
color = cm.nipy_spectral(float(i) / n_clusters)
ax1.fill_betweenx(
np.arange(y_lower, y_upper),
0,
ith_cluster_silhouette_values,
facecolor=color,
edgecolor=color,
alpha=0.7,
)
# Label the silhouette plots with their cluster numbers at the middle
ax1.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i))
# Compute the new y_lower for next plot
y_lower = y_upper + 10 # 10 for the 0 samples
ax1.set_title("The silhouette plot for the various clusters.")
ax1.set_xlabel("The silhouette coefficient values")
ax1.set_ylabel("Cluster label")
# The vertical line for average silhouette score of all the values
ax1.axvline(x=silhouette_avg, color="red", linestyle="--")
ax1.set_yticks([]) # Clear the yaxis labels / ticks
ax1.set_xticks([-0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1])
# ax1.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1])
# # 2nd Plot showing the actual clusters formed
# colors = cm.nipy_spectral(cluster_labels.astype(float) / n_clusters)
# ax2.scatter(
# X[:, 0], X[:, 1], marker=".", s=30, lw=0, alpha=0.7, c=colors, edgecolor="k"
# )
#
# # Labeling the clusters
# centers = clusterer.cluster_centers_
# # Draw white circles at cluster centers
# ax2.scatter(
# centers[:, 0],
# centers[:, 1],
# marker="o",
# c="white",
# alpha=1,
# s=200,
# edgecolor="k",
# )
#
# for i, c in enumerate(centers):
# ax2.scatter(c[0], c[1], marker="$%d$" % i, alpha=1, s=50, edgecolor="k")
#
# ax2.set_title("The visualization of the clustered data.")
# ax2.set_xlabel("Feature space for the 1st feature")
# ax2.set_ylabel("Feature space for the 2nd feature")
plt.suptitle(
"Silhouette analysis for KMeans clustering with n_clusters = %d"
# "Silhouette analysis for KMeans clustering on sample data with n_clusters = %d"
% n_clusters,
# fontsize=14,
# fontweight="bold",
)
# plt.show()
plt.savefig('Customer_KM_Silhouette_'+ str(n_clusters) +'.png') # save plot
# plt.show()
def em(X, cl=0): # Expectation Maximization
if cl == 2:
clusterer = GaussianMixture(n_components=2, random_state=10)
cluster_labels = clusterer.fit_predict(X)
return cluster_labels
elif cl == 4:
clusterer = GaussianMixture(n_components=4, random_state=10)
cluster_labels = clusterer.fit_predict(X)
return cluster_labels
range_n_clusters = [2, 3, 4, 5]
for n_clusters in range_n_clusters:
# Create a subplot with 1 row and 2 columns
# fig, (ax1, ax2) = plt.subplots(1, 2) # <<<<<<<<
fig, (ax1) = plt.subplots(1)
# fig.set_size_inches(18, 7)
# The 1st subplot is the silhouette plot
# The silhouette coefficient can range from -1, 1 but in this example all
# lie within [-0.1, 1]
ax1.set_xlim([-0.1, 1])
# The (n_clusters+1)*10 is for inserting blank space between silhouette
# plots of individual clusters, to demarcate them clearly.
ax1.set_ylim([0, len(X) + (n_clusters + 1) * 10])
# Initialize the clusterer with n_clusters value and a random generator
# seed of 10 for reproducibility.
# clusterer = KMeans(n_clusters=n_clusters, random_state=10)
# cluster_labels = clusterer.fit_predict(X)
gmm = GaussianMixture(n_components=n_clusters, random_state=10)
# gmm.fit(X)
cluster_labels = gmm.fit_predict(X)
# gm = GaussianMixture(n_components=2, random_state=0).fit(X)
# gm.means_
# gm.predict([[0, 0], [12, 3]])
#
# gmm = GaussianMixture(n_components=4)
# gmm.fit(X)
# labels = gmm.predict(X)
# plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis');
# The silhouette_score gives the average value for all the samples.
# This gives a perspective into the density and separation of the formed
# clusters
silhouette_avg = silhouette_score(X, cluster_labels)
print(
"For n_clusters =",
n_clusters,
"The average silhouette_score is :",
silhouette_avg,
)
# Compute the silhouette scores for each sample
sample_silhouette_values = silhouette_samples(X, cluster_labels)
y_lower = 10
for i in range(n_clusters):
# Aggregate the silhouette scores for samples belonging to
# cluster i, and sort them
ith_cluster_silhouette_values = sample_silhouette_values[cluster_labels == i]
ith_cluster_silhouette_values.sort()
size_cluster_i = ith_cluster_silhouette_values.shape[0]
y_upper = y_lower + size_cluster_i
color = cm.nipy_spectral(float(i) / n_clusters)
ax1.fill_betweenx(
np.arange(y_lower, y_upper),
0,
ith_cluster_silhouette_values,
facecolor=color,
edgecolor=color,
alpha=0.7,
)
# Label the silhouette plots with their cluster numbers at the middle
ax1.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i))
# Compute the new y_lower for next plot
y_lower = y_upper + 10 # 10 for the 0 samples
ax1.set_title("The silhouette plot for the various clusters.")
ax1.set_xlabel("The silhouette coefficient values")
ax1.set_ylabel("Cluster label")
# The vertical line for average silhouette score of all the values
ax1.axvline(x=silhouette_avg, color="red", linestyle="--")
ax1.set_yticks([]) # Clear the yaxis labels / ticks
ax1.set_xticks([-1, -0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1])
# ax1.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1])
plt.suptitle(
"Silhouette analysis for Expectation Maximization with n_components = %d"
# "Silhouette analysis for KMeans clustering on sample data with n_clusters = %d"
% n_clusters,
# fontsize=14,
# fontweight="bold",
)
# plt.show()
plt.savefig('Customer_EM_Silhouette_'+ str(n_clusters) +'.png') # save plot
def pca(X):
X_train =X
# X, y = fetch_openml("mnist_784", version=1, return_X_y=True, as_frame=False)
# train_img, test_img, train_lbl, test_lbl = train_test_split(mnist.data, mnist.target, test_size=1 / 7.0, random_state=0)
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=3240)
scaler = StandardScaler() # Fit on training set only.
scaler.fit(X_train) # Apply transform to both the training set and the test set.
# X_train = scaler.transform(X_train)
# X_test = scaler.transform(X_test)
# scale std
X_train_std = scaler.transform(X_train)
# X_test_std = scaler.transform(X_test)
# Note:
######## A pca 0.95 on unscaled data reduced to 1 dimension, while
## standardized data reduced to 3 dimensions from a total of 8.
# pca = PCA(.95) #<<<<<<<<<<<<<< <<<<<<<<<<<<
# pca_std = PCA(.95)
pca = PCA(11)
pca_std = PCA(11)
pca.fit(X_train)
pca_std.fit(X_train_std)
# Show first principal components
print(f"\nPC 1 without scaling:\n{pca.components_[0]}")
print(f"\nPC 1 with scaling:\n{pca_std.components_[0]}")
# Unscaled
X_train_transformed = pca.transform(X_train)
# X_test = pca.transform(X_test)
# Standard scaled
X_train_std_transformed = pca_std.transform(X_train_std)
# X_test_std_transformed = pca_std.transform(X_test_std)
# Eigen values
print('Explained variance ratio:',pca_std.explained_variance_ratio_)
cummulative = np.cumsum(pca_std.explained_variance_ratio_)
# plt.plot(range(1, 12), pca_std.explained_variance_ratio_, 'og-')
plt.plot(range(1, 12), cummulative, 'og-')
plt.xlabel('n-th Principal Component')
# plt.ylabel('Explained variance ratio ')
plt.ylabel('Explained variance ratio (cummulative)')
# plt.ylabel('Explained variance ratio')
plt.title('The Eigen value analysis of components generated by PCA')
# plt.show()
plt.savefig('Customer_PCA_Eigen_.png') # save plot
plt.close()
return X_train_std_transformed
def ica(X):
X_train = X
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=3240)
# scale std
scaler = StandardScaler() # Fit on training set only.
scaler.fit(X_train) # Apply transform to both the training set and the test set.
X_train_std = scaler.transform(X_train)
# X_test_std = scaler.transform(X_test)
# ica_std = FastICA(n_components=2)
ica_std = FastICA(n_components=11)
ica_std.fit(X_train_std)
X_train_std_transformed = ica_std.transform(X_train_std)
# X_test_std_transformed = ica_std.transform(X_test_std)
A_ = ica_std.mixing_ # Get estimated mixing matrix
# Kurtosis
from scipy.stats import norm, kurtosis
kurt_orignal = kurtosis(X_train_std, fisher=True)
kurt = kurtosis(X_train_std_transformed, fisher=True)
# tips = sns.load_dataset("tips")
# sns.kdeplot(data=tips, x="total_bill")
print('ICA Kurtosis original:',kurt_orignal)
print('ICA Kurtosis:',kurt)
plt.plot(range(0, 11), kurt_orignal, 'og-')
plt.plot(range(0, 11), kurt, 'xb-')
ax = plt.gca()
ax.legend(['Original', 'ICA Tranformed'])
plt.xlabel('Component')
plt.ylabel('Kurtosis')
plt.title('The Kurtosis of components generated by ICA')
# plt.show()
plt.savefig('Customer_ICA_kurtosis.png')
plt.close()
return X_train_std_transformed
def rp(X): # Randomized Projections
X_train =X
# scale std
scaler = StandardScaler() # Fit on training set only.
scaler.fit(X_train) # Apply transform to both the training set and the test set.
X_train_std = scaler.transform(X_train)
# X_test_std = scaler.transform(X_test)
#############################33
rmse_all=[]
rmse_all.append(0.0) # Dummy val to shift
for n_component in range(1, 12):
# rp_std = GaussianRandomProjection(n_components=2)
rp_std = GaussianRandomProjection(n_components=n_component,compute_inverse_components=True)
rp_std.fit(X_train_std)
X_train_std_transformed = rp_std.transform(X_train_std)
X_train_std_transformed_inversed = rp_std.inverse_transform(X_train_std_transformed)
# rmse_comp = mean_squared_error(X_train_std, X_train_std_transformed_inversed, multioutput='raw_values',squared=False)
rmse = mean_squared_error(X_train_std, X_train_std_transformed_inversed, squared=False)
rmse_all.append(rmse)
print('RMSE All:',rmse_all)
plt.plot(range(1, 12), rmse_all[1:], 'og-')
ax = plt.gca()
plt.xlabel('Components')
plt.ylabel('RMSE')
plt.title('The RMSE of orignal vs inverse of feature generated by RP')
# plt.show()
plt.savefig('Customer_RP_rmse.png')
plt.close()
# X_train_std_transformed_again = rp_std.transform(X_train_std_transformed_inversed)
# # test1 = np.allclose(X_train_std_transformed, X_train_std_transformed_again)
# # # test = np.allclose(X_train_std, X_train_std_transformed_inversed)
#############################33
return X_train_std_transformed
def ofs(X): # other feature selection algorithm
y = X['Channel1']
X = X.drop(['Channel1'], axis=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=3240)
# scale std
scaler = StandardScaler() # Fit on training set only.
scaler.fit(X_train) # Apply transform to both the training set and the test set.
X_train_std = scaler.transform(X_train)
X_test_std = scaler.transform(X_test)
#
# X, y = load_iris(return_X_y=True)
# X.shape
clf = ExtraTreesClassifier(n_estimators=50)
clf = clf.fit(X_train_std, y_train) # clf = clf.fit(X, y)
print(clf.feature_importances_, clf.feature_importances_.argsort())
first = clf.feature_importances_.argsort()[-1:] #6
second = clf.feature_importances_.argsort()[-2:-1] #1
# [0.1481483 0.19205667 0.08683144 0.11135282 0.13380407 0.07531707
# 0.25248963] [5 2 3 4 0 1 6]
ofs_std = SelectFromModel(clf, prefit=True) # <<< ***
X_train_std_transformed = ofs_std.transform(X_train_std)
X_test_std_transformed = ofs_std.transform(X_test_std)
# visualize standardized vs. untouched dataset with PCA performed
FIG_SIZE = (10, 7)
fig, (ax2) = plt.subplots(ncols=1, figsize=FIG_SIZE)
target_classes = range(0, 2)
colors = ("blue", "red")
markers = ("s", "o")
for target_class, color, marker in zip(target_classes, colors, markers):
# x = X_train_std[y_train == target_class, first] # 0],
# y = X_train_std[y_train == target_class, second] # 1],
ax2.scatter(
x=X_train_std[y_train == target_class, first], #0],
y=X_train_std[y_train == target_class, second], #1],
color=color,
label=f"class {target_class}",
alpha=0.5,
marker=marker,
)
ax2.set_title("Standardized training dataset after RP")
ax2.set_xlabel("1st principal component")
ax2.set_ylabel("2nd principal component")
ax2.legend(loc="upper right")
ax2.grid()
plt.tight_layout()
plt.savefig('Customer_OFS_.png') # save plot
# plt.show()
return X_train_std_transformed, X_test_std_transformed, y_train, y_test
#######################################
#######################################
#######################################
#######################################
def pca_full(X):
scaler = StandardScaler() # Fit on training set only.
scaler.fit(X) # Apply transform to both the training set and the test set.
X_std = scaler.transform(X)
pca_std = PCA(0.95)
pca_std.fit(X_std)
X_std_transformed = pca_std.transform(X_std) # 3 components out
return X_std_transformed
def ica_full(X):
scaler = StandardScaler() # Fit on training set only.
scaler.fit(X) # Apply transform to both the training set and the test set.
X_std = scaler.transform(X)
ica_std = FastICA(n_components=3)
# ica_std = FastICA()
ica_std.fit(X_std)
X_std_transformed = ica_std.transform(X_std)
return X_std_transformed
def rp_full(X): # Randomized Projections
scaler = StandardScaler() # Fit on training set only.
scaler.fit(X) # Apply transform to both the training set and the test set.
X_std = scaler.transform(X)
rp_std = GaussianRandomProjection(n_components=3)
rp_std.fit(X_std)
X_std_transformed = rp_std.transform(X_std) # Out Dime > 7 (~167) Error on eps [0.0 - 0.999]
return X_std_transformed
def ofs_full(X,y): # other feature selection algorithm
scaler = StandardScaler() # Fit on training set only.
scaler.fit(X) # Apply transform to both the training set and the test set.
X_std = scaler.transform(X)
clf = ExtraTreesClassifier(n_estimators=50)
clf = clf.fit(X_std,y)
ofs_std = SelectFromModel(clf, prefit=True) # <<< ***
X_std_transformed = ofs_std.transform(X_std) # 4 components out
return X_std_transformed
def main():
#################################################################################
# Data set #2
#################################################################################
# Channel Region Fresh Milk Grocery Frozen Detergents_Paper Delicassen
customer_df = pd.read_csv(path.join('data','Wholesale_customers_data.csv'))
# customer_df.rename(columns={'Class':'Class_category'}, inplace=True)
dataset = 2
enc = OneHotEncoder(handle_unknown='ignore')
# Channel
enc_df = pd.DataFrame(enc.fit_transform(customer_df[['Channel']]).toarray())
enc_df.rename(columns={0: 'Channel1', 1: 'Channel2'}, inplace=True)
customer_df = pd.concat([customer_df, enc_df], axis=1)
# Region
enc_df = pd.DataFrame(enc.fit_transform(customer_df[['Region']]).toarray())
enc_df.rename(columns={0: 'Region1', 1: 'Region2', 2: 'Region3'}, inplace=True)
customer_df = pd.concat([customer_df, enc_df], axis=1)
X = customer_df.drop(['Channel', 'Region'], axis=1)
# Step 1 ##################
# Clustering
km(X) # k-means clustering
# print('em:')
em(X) # Expectation Maximization
# Step 2 ##################
# Dimentionality reduction
pca(X)
ica(X)
rp(X) # Randomized Projections
ofs(X) # other feature selection algorithm
# pca_full(X)
# # ica_full(X)
# rp_full(X)
# # ofs_full(X,y)
# # Step 3 ##################
km(pca_full(X))
km(ica_full(X))
km(rp_full(X))
km(ofs_full(X))
em(pca_full(X))
em(ica_full(X))
em(rp_full(X))
em(ofs_full(X))
if __name__ == "__main__":
main()