-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
351 lines (306 loc) · 12.6 KB
/
app.py
File metadata and controls
351 lines (306 loc) · 12.6 KB
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
import os
import time
import pandas as pd
import sys
from preprocessing import preprocessing
from cluster_algorithms import optics
from cluster_algorithms.k_means import KMeans
from cluster_algorithms.fuzzy_c_means import CMeans
from cluster_algorithms.g_means import run_gmeans_experiment
from cluster_algorithms.spectral import SpectralClusteringAnalysis
from summary.summary_spectral import SpectralClusteringAnalyzer
from summary.summary_optics import OPTICSAnalyzer
from summary.summary_gmeans import GMeansClusteringAnalyzer
from summary.summary_cmeans import CMeansAnalyzer
from plotting.spaghetti_plot import CreatesSpaghettiPlot
from utils import load_datasets
def display_menu():
print("\nClustering Algorithm Selection:")
print("1) OPTICS algorithm")
print("2) Spectral algorithm")
print("3) K-means")
print("4) K-means++")
print("5) G-Means")
print("6) Fuzzy C-means")
print(
"7) Generate LateX Code/ Confusion matrix for Optics | Spectral | G-Means | C-Means"
)
print("8) Create Spaggetti Plots for C-Means | K-Means | K-Means++ | G-Means")
print("9) Exit")
return input("\nEnter your choice (1-9): ")
def display_menu_dataset_choice():
print("Please select one of these options:")
print("1) I would like to use the MinMax Scaled dataset")
print("2) I would like to use the Robust Scaled dataset")
print("3) I would like to use the Standard Scaled dataset")
return input("\nEnter your choice (1-3): ")
if __name__ == "__main__":
script_dir = os.path.dirname(os.path.abspath(__file__))
dataset_dir = os.path.join(script_dir, "datasets")
try:
dataset_name = (
input("Enter the name of the dataset ['cmc', 'hepatitis', 'pen-based']: ")
or "cmc"
)
if dataset_name not in ["cmc", "hepatitis", "pen-based"]:
raise ValueError(f"Dataset must be one of: 'cmc', 'hepatitis', 'pen-based'")
except ValueError as e:
print(f"You might have misspelled the dataset name: {e}")
exit(1)
start_time = time.time()
preprocessor = preprocessing.Preprocessing()
# Load dataset
df = pd.DataFrame(load_datasets(dataset_dir, dataset_name))
# Preprocess the data
X, X_robust, X_standard, y_true = preprocessor.generous_preprocessing(df)
binary_vars = preprocessor.binary_vars
categorical_vars = preprocessor.categorical_vars
while True:
choice = display_menu()
choice = int(choice)
if choice == 9:
print("Exiting the program...")
sys.exit()
elif choice == 1: # optics
optics_clustering = optics.Optics()
dataset_choice = display_menu_dataset_choice()
dataset_choice = int(dataset_choice)
if dataset_choice == 1:
optics_clustering.perform_optics(
dataset_name, dataset_choice, X, y_true
)
elif dataset_choice == 2:
optics_clustering.perform_optics(
dataset_name, dataset_choice, X, y_true
)
elif dataset_choice == 3:
optics_clustering.perform_optics(
dataset_name, dataset_choice, X, y_true
)
elif choice == 2: # Spectral
dataset_choice = (
display_menu_dataset_choice()
) # Call the function to get the choice
dataset_choice = int(dataset_choice)
if dataset_choice == 1:
spectral_analysis = SpectralClusteringAnalysis(dataset_name)
results = spectral_analysis.perform_spectral_clustering(
X,
y_true,
preprocessor.binary_vars,
preprocessor.categorical_vars,
dataset_choice,
dataset_name=dataset_name,
)
elif dataset_choice == 2:
spectral_analysis = SpectralClusteringAnalysis(dataset_name)
results = spectral_analysis.perform_spectral_clustering(
X_robust,
y_true,
preprocessor.binary_vars,
preprocessor.categorical_vars,
dataset_choice,
dataset_name=dataset_name,
)
elif dataset_choice == 3:
spectral_analysis = SpectralClusteringAnalysis(dataset_name)
results = spectral_analysis.perform_spectral_clustering(
X_standard,
y_true,
preprocessor.binary_vars,
preprocessor.categorical_vars,
dataset_choice,
dataset_name=dataset_name,
)
elif choice == 3:
# K-means implementation
perform_kmeans = KMeans()
dataset_choice = display_menu_dataset_choice() # get the choice
dataset_choice = int(dataset_choice)
clustering_algo = "K-Means"
if dataset_choice == 1:
results = perform_kmeans.run_k_means_experiments(
X,
dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
y_true=y_true,
)
elif dataset_choice == 2:
results = perform_kmeans.run_k_means_experiments(
X_robust,
dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
y_true=y_true,
)
elif dataset_choice == 3:
results = perform_kmeans.run_k_means_experiments(
X_standard,
dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
y_true=y_true,
)
# End of Simple K - means
#
elif choice == 4:
# K ++
perform_kmeans = KMeans()
dataset_choice = display_menu_dataset_choice() # get the choice
dataset_choice = int(dataset_choice)
clustering_algo = "K-Means++"
if dataset_choice == 1:
results = perform_kmeans.run_k_means_experiments(
X,
dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
y_true=y_true,
plus_plus=True,
)
elif dataset_choice == 2:
results = perform_kmeans.run_k_means_experiments(
X_robust,
dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
y_true=y_true,
plus_plus=True,
)
elif dataset_choice == 3:
results = perform_kmeans.run_k_means_experiments(
X_standard,
dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
y_true=y_true,
plus_plus=True,
)
# End of K ++
elif choice == 5:
# G-Means
clustering_algo = "G-Means"
dataset_choice = (
display_menu_dataset_choice()
) # Call the function to get the choice
dataset_choice = int(dataset_choice)
if dataset_choice == 1:
run_gmeans_experiment(
data=X,
y_true=y_true,
dataset_choice=dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
)
elif dataset_choice == 2:
run_gmeans_experiment(
data=X,
y_true=y_true,
dataset_choice=dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
)
elif dataset_choice == 3:
run_gmeans_experiment(
data=X,
y_true=y_true,
dataset_choice=dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
)
# End of G-means
elif choice == 6: # C++
# C-means implementation
perform_cmeans = CMeans()
dataset_choice = display_menu_dataset_choice() # get the choice
dataset_choice = int(dataset_choice)
clustering_algo = "C-Means"
if dataset_choice == 1:
results = perform_cmeans.run_c_means_experiments(
X,
dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
y_true=y_true,
)
elif dataset_choice == 2:
results = perform_cmeans.run_c_means_experiments(
X_robust,
dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
y_true=y_true,
)
elif dataset_choice == 3:
results = perform_cmeans.run_c_means_experiments(
X_standard,
dataset_choice,
dataset_name=dataset_name,
clustering_algo=clustering_algo,
y_true=y_true,
)
# End of C-means
elif choice == 7:
algorithm = input(
"Choose clustering algorithm (1: OPTICS, 2: Spectral, 3: G-Means, 4: C-Means): "
)
if algorithm not in ["1", "2", "3", "4"]:
print("Invalid algorithm choice")
continue
while True:
try:
weight = float(input("Enter external weight (0-1): "))
if 0 <= weight <= 1:
break
print("Weight must be between 0 and 1")
except ValueError:
print("Please enter a valid number")
if algorithm == "1":
analyzer = OPTICSAnalyzer(true_labels=y_true, features=X)
print("Creating LaTeX code and confusion matrix...")
results, latex = analyzer.analyze_dataset(
dataset_name, external_weight=weight
)
print("Finished")
elif algorithm == "2":
analyzer = SpectralClusteringAnalyzer(true_labels=y_true, features=X)
print("Creating LaTeX code and confusion matrix...")
results, latex = analyzer.analyze_dataset(
dataset_name, external_weight=weight
)
print("Finished")
elif algorithm == "3":
analyzer = GMeansClusteringAnalyzer(true_labels=y_true, features=X)
print("Creating LaTeX code...")
results, latex = analyzer.analyze_dataset(
dataset_name, external_weight=weight
)
print("Finished")
elif algorithm == "4":
analyzer = CMeansAnalyzer(true_labels=y_true, features=X)
print("Creating LaTeX code...")
results, latex = analyzer.analyze_dataset(
dataset_name, external_weight=weight
)
print("Finished")
elif choice == 8:
algorithm = input(
"Choose clustering algorithm (1: K-Means, 2: K-Means++, 3: G-Means, 4: C-means): "
)
if algorithm not in ["1", "2", "3", "4"]:
print("Invalid algorithm choice")
continue
# Map numeric choice to algorithm name
algo_map = {
"1": "K-Means",
"2": "K-Means++",
"3": "G-Means",
"4": "C-Means",
}
selected_algorithm = algo_map[algorithm]
print(f"Creating spaghetti plots for {selected_algorithm}...")
plotter = CreatesSpaghettiPlot(algorithm=selected_algorithm)
plotter.create_visualization(dataset_name)
print("Finished creating spaghetti plots")
print(f"Time taken: {time.time() - start_time:.2f} seconds")