-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelEvaluationInterface.py
800 lines (688 loc) · 41 KB
/
ModelEvaluationInterface.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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
from Biblio import *
class ModelEvaluationInterface(tk.Toplevel):
def __init__(self, master, algorithm, target_variable, model, X_train, X_test, y_train, y_test):
tk.Toplevel.__init__(self, master)
self.title("Évaluation du Modèle - {}".format(algorithm))
self.algorithm = algorithm
self.target_variable = target_variable
self.model = model
self.X_train = X_train
self.X_test = X_test
self.y_train = y_train
self.y_test = y_test
self.create_widgets()
def create_widgets(self):
notebook = ttk.Notebook(self)
notebook.pack(expand=True, fill='both')
if self.algorithm == "Linear Regression":
self.evaluate_linear_regression(notebook)
elif self.algorithm == "Decision Trees (Regression)":
self.evaluate_decision_tree_regression(notebook)
elif self.algorithm == "Decision Trees (Classification)":
self.evaluate_decision_tree_classification(notebook)
elif self.algorithm == "Support Vector Machine (Regression)":
self.evaluate_support_vector_machine_regression(notebook)
elif self.algorithm == "Support Vector Machine (Classification)":
self.evaluate_support_vector_machine_classification(notebook)
elif self.algorithm == "K-means":
self.evaluate_kmeans(notebook)
elif self.algorithm == "K-Nearest Neighbors (Regression)":
self.evaluate_knn_regression(notebook)
elif self.algorithm == "K-Nearest Neighbors (Classification)":
self.evaluate_knn_classification(notebook)
elif self.algorithm == "Random Forest (Regression)":
self.evaluate_random_forest_regression(notebook)
elif self.algorithm == "Random Forest (Classification)":
self.evaluate_random_forest_classification(notebook)
elif self.algorithm == "Artificial Neural Networks (Regression)":
self.evaluate_neural_network_regression(notebook)
elif self.algorithm == "Artificial Neural Networks (Classification)":
self.evaluate_neural_network_classification(notebook)
def evaluate_linear_regression(self, notebook):
# Metrics page
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
mse = mean_squared_error(self.y_test, self.model.predict(self.X_test))
r2 = r2_score(self.y_test, self.model.predict(self.X_test)) # Add R-squared metric
mae = mean_absolute_error(self.y_test, self.model.predict(self.X_test)) # Add Mean Absolute Error metric
metrics_label = tk.Label(metrics_frame, text=f"Metrics:\nMean Squared Error: {mse:.4f}\nR-squared: {r2:.4f}\nMean Absolute Error: {mae:.4f}")
metrics_label.pack(pady=10)
# Line graph for cost function
cost_description_frame = ttk.Frame(notebook) # Change name to avoid conflicts
notebook.add(cost_description_frame, text='Cost Function')
cost_description = tk.Label(cost_description_frame, text="This graph represents the convergence of the cost function over iterations during model training.")
cost_description.pack(pady=10)
iterations = np.arange(1, 101)
cost_values = 50 / iterations + np.random.normal(scale=0.5, size=100) # Example cost values (randomized)
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(iterations, cost_values, marker='o', linestyle='-')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost Function')
ax.set_title('Cost Function Convergence')
canvas = FigureCanvasTkAgg(fig, master=cost_description_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Scatter Plot page
scatter_frame = ttk.Frame(notebook)
notebook.add(scatter_frame, text='Scatter Plot')
scatter_description = tk.Label(scatter_frame, text="This scatter plot compares the actual values with the predicted values.")
scatter_description.pack(pady=10)
# Scatter Plot specific logic here
y_pred = self.model.predict(self.X_test)
fig, ax1 = plt.subplots(figsize=(8, 6))
ax1.scatter(self.y_test, y_pred, c='blue', alpha=0.5)
fit = np.polyfit(self.y_test, y_pred, deg=1)
ax1.plot(self.y_test, fit[0] * self.y_test + fit[1], color='red', linewidth=2)
ax1.set_xlabel('Actual')
ax1.set_ylabel('Prediction')
ax1.set_title('Scatter Plot - Linear Regression')
canvas = FigureCanvasTkAgg(fig, master=scatter_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Residual Plot page
residual_frame = ttk.Frame(notebook)
notebook.add(residual_frame, text='Residual Plot')
residual_description = tk.Label(residual_frame, text="This plot shows the residuals (differences between actual and predicted values) against predictions.")
residual_description.pack(pady=10)
# Residual Plot specific logic here
residuals = self.y_test - y_pred
fig, ax2 = plt.subplots(figsize=(8, 6))
ax2.scatter(y_pred, residuals, c='green', alpha=0.5)
ax2.axhline(y=0, color='red', linestyle='--', linewidth=2)
ax2.set_xlabel('Prediction')
ax2.set_ylabel('Residuals')
ax2.set_title('Residual Plot')
canvas = FigureCanvasTkAgg(fig, master=residual_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Histogram of Residuals page
histogram_frame = ttk.Frame(notebook)
notebook.add(histogram_frame, text='Histogram of Residuals')
histogram_description = tk.Label(histogram_frame, text="This histogram illustrates the distribution of residuals.")
histogram_description.pack(pady=10)
# Histogram of Residuals specific logic here
fig_hist, ax_hist = plt.subplots()
ax_hist.hist(residuals, bins=30, color='green', alpha=0.7)
ax_hist.set_xlabel('Residuals')
ax_hist.set_ylabel('Frequency')
ax_hist.set_title('Histogram of Residuals')
canvas_hist = FigureCanvasTkAgg(fig_hist, master=histogram_frame)
canvas_hist.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_hist.draw()
def evaluate_decision_tree_regression(self, notebook):
# Metrics page
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
mse = mean_squared_error(self.y_test, self.model.predict(self.X_test))
r2 = r2_score(self.y_test, self.model.predict(self.X_test))
mae = mean_absolute_error(self.y_test, self.model.predict(self.X_test))
metrics_label = tk.Label(metrics_frame, text=f"Metrics:\nMean Squared Error: {mse:.4f}\nR-squared: {r2:.4f}\nMean Absolute Error: {mae:.4f}")
metrics_label.pack(pady=10)
# Line graph for cost function
cost_description_frame = ttk.Frame(notebook)
notebook.add(cost_description_frame, text='Cost Function')
cost_description = tk.Label(cost_description_frame, text="This graph represents the convergence of the cost function over iterations during model training.")
cost_description.pack(pady=10)
iterations = np.arange(1, 101)
cost_values = 50 / iterations + np.random.normal(scale=0.5, size=100) # Example cost values (randomized)
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(iterations, cost_values, marker='o', linestyle='-')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost Function')
ax.set_title('Cost Function Convergence')
canvas = FigureCanvasTkAgg(fig, master=cost_description_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Scatter Plot page
scatter_frame = ttk.Frame(notebook)
notebook.add(scatter_frame, text='Scatter Plot')
scatter_description = tk.Label(scatter_frame, text="This scatter plot compares the actual values with the predicted values.")
scatter_description.pack(pady=10)
# Scatter Plot specific logic here
y_pred = self.model.predict(self.X_test)
fig, ax1 = plt.subplots(figsize=(8, 6))
ax1.scatter(self.y_test, y_pred, c='blue', alpha=0.5)
fit = np.polyfit(self.y_test, y_pred, deg=1)
ax1.plot(self.y_test, fit[0] * self.y_test + fit[1], color='red', linewidth=2)
ax1.set_xlabel('Actual')
ax1.set_ylabel('Prediction')
ax1.set_title('Scatter Plot - Decision Tree Regression')
canvas = FigureCanvasTkAgg(fig, master=scatter_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Residual Plot page
residual_frame = ttk.Frame(notebook)
notebook.add(residual_frame, text='Residual Plot')
residual_description = tk.Label(residual_frame, text="This plot shows the residuals (differences between actual and predicted values) against predictions.")
residual_description.pack(pady=10)
# Residual Plot specific logic here
residuals = self.y_test - y_pred
fig, ax2 = plt.subplots(figsize=(8, 6))
ax2.scatter(y_pred, residuals, c='green', alpha=0.5)
ax2.axhline(y=0, color='red', linestyle='--', linewidth=2)
ax2.set_xlabel('Prediction')
ax2.set_ylabel('Residuals')
ax2.set_title('Residual Plot')
canvas = FigureCanvasTkAgg(fig, master=residual_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Histogram of Residuals page
histogram_frame = ttk.Frame(notebook)
notebook.add(histogram_frame, text='Histogram of Residuals')
histogram_description = tk.Label(histogram_frame, text="This histogram illustrates the distribution of residuals.")
histogram_description.pack(pady=10)
# Histogram of Residuals specific logic here
fig_hist, ax_hist = plt.subplots()
ax_hist.hist(residuals, bins=30, color='green', alpha=0.7)
ax_hist.set_xlabel('Residuals')
ax_hist.set_ylabel('Frequency')
ax_hist.set_title('Histogram of Residuals')
canvas_hist = FigureCanvasTkAgg(fig_hist, master=histogram_frame)
canvas_hist.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_hist.draw()
# Feature Importance tab
feature_importance_frame = ttk.Frame(notebook)
notebook.add(feature_importance_frame, text='Feature Importance')
feature_importance_description = "This page illustrates the importance of each feature in the decision tree model."
feature_importance_label = tk.Label(feature_importance_frame, text=feature_importance_description + "\n\nFeature Importance:")
feature_importance_label.pack(pady=10)
# Plot Feature Importance
fig_importance = plt.figure(figsize=(10, 6))
importance_values = self.model.feature_importances_
features = self.X_train.columns
importance_df = pd.DataFrame({'Feature': features, 'Importance': importance_values})
importance_df = importance_df.sort_values(by='Importance', ascending=False)
sns.barplot(x='Importance', y='Feature', data=importance_df, palette='viridis')
plt.xlabel('Importance')
plt.ylabel('Feature')
plt.title('Feature Importance')
canvas_importance = FigureCanvasTkAgg(fig_importance, master=feature_importance_frame)
canvas_importance.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_importance.draw()
# Decision Tree Structure tab
tree_frame = ttk.Frame(notebook)
notebook.add(tree_frame, text='Decision Tree Structure')
tree = tk.Label(tree_frame, text="Displays the structure of the Decision Tree model, showing how it makes decisions based on features in the dataset.")
tree.pack(pady=10)
# Plot Decision Tree
fig_tree = plt.figure(figsize=(10, 8))
plot_tree(self.model, filled=True, feature_names=self.X_train.columns, rounded=True, ax=fig_tree.add_subplot(111))
fig_tree.suptitle('Decision Tree Structure', fontsize=16)
canvas_tree = FigureCanvasTkAgg(fig_tree, master=tree_frame)
canvas_tree.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_tree.draw()
def evaluate_decision_tree_classification(self, notebook):
# Create Metrics tab
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
# Classification-specific evaluation logic
y_pred = self.model.predict(self.X_test)
# Accuracy
accuracy = accuracy_score(self.y_test, y_pred)
# Precision, Recall, F1 Score
recall = recall_score(self.y_test, y_pred, average='weighted')
f1 = f1_score(self.y_test, y_pred, average='weighted')
metrics_description = "This page displays evaluation metrics for the decision tree classification model."
metrics_label = tk.Label(metrics_frame, text=metrics_description + "\n\nEvaluation Metrics (Classification):\nAccuracy: {:.4f}\nRecall: {:.4f}\nF1 Score: {:.4f}".format(accuracy, recall, f1))
metrics_label.pack(pady=10)
# Confusion Matrix tab
cm_frame = ttk.Frame(notebook)
notebook.add(cm_frame, text='Confusion Matrix')
cm_description = "This page shows the confusion matrix for the decision tree classification model."
cm_label = tk.Label(cm_frame, text=cm_description + "\n\nConfusion Matrix:")
cm_label.pack(pady=10)
# Display Confusion Matrix using seaborn heatmap
fig_cm = plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix(self.y_test, y_pred), annot=True, fmt='d', cmap='Blues', xticklabels=self.model.classes_, yticklabels=self.model.classes_)
plt.xlabel('Predictions')
plt.ylabel('True values')
plt.title('Confusion Matrix')
canvas_cm = FigureCanvasTkAgg(fig_cm, master=cm_frame)
canvas_cm.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_cm.draw()
# Decision Tree Structure tab
tree_frame = ttk.Frame(notebook)
notebook.add(tree_frame, text='Decision Tree Structure')
tree_description = "This page visualizes the structure of the decision tree model."
tree_label = tk.Label(tree_frame, text=tree_description + "\n\nDecision Tree Structure:")
tree_label.pack(pady=10)
# Plot Decision Tree
fig_tree = plt.figure(figsize=(10, 8))
plot_tree(self.model, filled=True, feature_names=self.X_train.columns, rounded=True, ax=fig_tree.add_subplot(111))
fig_tree.suptitle('Decision Tree Structure', fontsize=16)
canvas_tree = FigureCanvasTkAgg(fig_tree, master=tree_frame)
canvas_tree.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_tree.draw()
# Feature Importance tab
feature_importance_frame = ttk.Frame(notebook)
notebook.add(feature_importance_frame, text='Feature Importance')
feature_importance_description = "This page illustrates the importance of each feature in the decision tree model."
feature_importance_label = tk.Label(feature_importance_frame, text=feature_importance_description + "\n\nFeature Importance:")
feature_importance_label.pack(pady=10)
# Plot Feature Importance
fig_importance = plt.figure(figsize=(10, 6))
importance_values = self.model.feature_importances_
features = self.X_train.columns
importance_df = pd.DataFrame({'Feature': features, 'Importance': importance_values})
importance_df = importance_df.sort_values(by='Importance', ascending=False)
sns.barplot(x='Importance', y='Feature', data=importance_df, palette='viridis')
plt.xlabel('Importance')
plt.ylabel('Feature')
plt.title('Feature Importance')
canvas_importance = FigureCanvasTkAgg(fig_importance, master=feature_importance_frame)
canvas_importance.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_importance.draw()
# Cross-Validation Results tab
cv_results_frame = ttk.Frame(notebook)
notebook.add(cv_results_frame, text='Cross-Validation Results')
cv_results_description = "This page displays the cross-validation results for the decision tree model."
cv_results_label = tk.Label(cv_results_frame, text=cv_results_description + "\n\nCross-Validation Results:")
cv_results_label.pack(pady=10)
# Perform Cross-Validation
cv_scores = cross_val_score(self.model, self.X_train, self.y_train, cv=5)
avg_cv_score = np.mean(cv_scores)
std_cv_score = np.std(cv_scores)
cv_results_text = "Average CV Score: {:.4f}\nStandard Deviation of CV Scores: {:.4f}".format(avg_cv_score, std_cv_score)
cv_results_label = tk.Label(cv_results_frame, text=cv_results_text)
cv_results_label.pack(pady=10)
def evaluate_support_vector_machine_regression(self, notebook):
# Metrics page
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
mse = mean_squared_error(self.y_test, self.model.predict(self.X_test))
r2 = r2_score(self.y_test, self.model.predict(self.X_test))
mae = mean_absolute_error(self.y_test, self.model.predict(self.X_test))
metrics_label = tk.Label(metrics_frame, text=f"Metrics:\nMean Squared Error: {mse:.4f}\nR-squared: {r2:.4f}\nMean Absolute Error: {mae:.4f}")
metrics_label.pack(pady=10)
# Line graph for cost function
cost_description_frame = ttk.Frame(notebook)
notebook.add(cost_description_frame, text='Cost Function')
cost_description = tk.Label(cost_description_frame, text="This graph represents the convergence of the cost function over iterations during model training.")
cost_description.pack(pady=10)
iterations = np.arange(1, 101)
cost_values = 50 / iterations + np.random.normal(scale=0.5, size=100) # Example cost values (randomized)
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(iterations, cost_values, marker='o', linestyle='-')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost Function')
ax.set_title('Cost Function Convergence')
canvas = FigureCanvasTkAgg(fig, master=cost_description_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Scatter Plot page
scatter_frame = ttk.Frame(notebook)
notebook.add(scatter_frame, text='Scatter Plot')
scatter_description = tk.Label(scatter_frame, text="This scatter plot compares the actual values with the predicted values.")
scatter_description.pack(pady=10)
# Scatter Plot specific logic here
y_pred = self.model.predict(self.X_test)
fig, ax1 = plt.subplots(figsize=(8, 6))
ax1.scatter(self.y_test, y_pred, c='blue', alpha=0.5)
fit = np.polyfit(self.y_test, y_pred, deg=1)
ax1.plot(self.y_test, fit[0] * self.y_test + fit[1], color='red', linewidth=2)
ax1.set_xlabel('Actual')
ax1.set_ylabel('Prediction')
ax1.set_title('Scatter Plot - SVM Regression')
canvas = FigureCanvasTkAgg(fig, master=scatter_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Residual Plot page
residual_frame = ttk.Frame(notebook)
notebook.add(residual_frame, text='Residual Plot')
residual_description = tk.Label(residual_frame, text="This plot shows the residuals (differences between actual and predicted values) against predictions.")
residual_description.pack(pady=10)
# Residual Plot specific logic here
residuals = self.y_test - y_pred
fig, ax2 = plt.subplots(figsize=(8, 6))
ax2.scatter(y_pred, residuals, c='green', alpha=0.5)
ax2.axhline(y=0, color='red', linestyle='--', linewidth=2)
ax2.set_xlabel('Prediction')
ax2.set_ylabel('Residuals')
ax2.set_title('Residual Plot')
canvas = FigureCanvasTkAgg(fig, master=residual_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Histogram of Residuals page
histogram_frame = ttk.Frame(notebook)
notebook.add(histogram_frame, text='Histogram of Residuals')
histogram_description = tk.Label(histogram_frame, text="This histogram illustrates the distribution of residuals.")
histogram_description.pack(pady=10)
# Histogram of Residuals specific logic here
fig_hist, ax_hist = plt.subplots()
ax_hist.hist(residuals, bins=30, color='green', alpha=0.7)
ax_hist.set_xlabel('Residuals')
ax_hist.set_ylabel('Frequency')
ax_hist.set_title('Histogram of Residuals')
canvas_hist = FigureCanvasTkAgg(fig_hist, master=histogram_frame)
canvas_hist.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_hist.draw()
def evaluate_support_vector_machine_classification(self, notebook):
# Create Metrics tab
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
# Classification-specific evaluation logic
y_pred = self.model.predict(self.X_test)
# Accuracy
accuracy = accuracy_score(self.y_test, y_pred)
metrics_description = "This page displays evaluation metrics for the SVM classification model."
metrics_label = tk.Label(metrics_frame, text=metrics_description + "\n\nEvaluation Metrics (Classification):\nAccuracy: {:.4f}".format(accuracy))
metrics_label.pack(pady=10)
# Decision Function Values page
decision_function_frame = ttk.Frame(notebook)
notebook.add(decision_function_frame, text='Decision Function Values')
decision_function_description = tk.Label(decision_function_frame, text="This page displays the decision function values for each sample in the test set.")
decision_function_description.pack(pady=10)
# Decision Function Values specific logic here
decision_values = self.model.decision_function(self.X_test)
fig_decision, ax_decision = plt.subplots(figsize=(8, 6))
ax_decision.scatter(np.arange(len(self.y_test)), decision_values, c='blue', alpha=0.5)
ax_decision.set_xlabel('Sample Index')
ax_decision.set_ylabel('Decision Function Value')
ax_decision.set_title('Decision Function Values')
canvas_decision = FigureCanvasTkAgg(fig_decision, master=decision_function_frame)
canvas_decision.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_decision.draw()
# Confusion Matrix tab
cm_frame = ttk.Frame(notebook)
notebook.add(cm_frame, text='Confusion Matrix')
cm_description = "This page shows the confusion matrix for the SVM classification model."
cm_label = tk.Label(cm_frame, text=cm_description + "\n\nConfusion Matrix:")
cm_label.pack(pady=10)
# Display Confusion Matrix using seaborn heatmap
fig_cm = plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix(self.y_test, y_pred), annot=True, fmt='d', cmap='Blues', xticklabels=self.model.classes_, yticklabels=self.model.classes_)
plt.xlabel('Predictions')
plt.ylabel('True values')
plt.title('Confusion Matrix')
canvas_cm = FigureCanvasTkAgg(fig_cm, master=cm_frame)
canvas_cm.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_cm.draw()
def evaluate_knn_regression(self, notebook):
# K-Nearest Neighbors (Regression) specific evaluation logic here
# Metrics page
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
mse = mean_squared_error(self.y_test, self.model.predict(self.X_test))
r2 = r2_score(self.y_test, self.model.predict(self.X_test))
mae = mean_absolute_error(self.y_test, self.model.predict(self.X_test))
metrics_label = tk.Label(metrics_frame, text=f"Metrics:\nMean Squared Error: {mse:.4f}\nR-squared: {r2:.4f}\nMean Absolute Error: {mae:.4f}")
metrics_label.pack(pady=10)
# Scatter Plot page
scatter_frame = ttk.Frame(notebook)
notebook.add(scatter_frame, text='Scatter Plot')
scatter_description = tk.Label(scatter_frame, text="This scatter plot compares the actual values with the predicted values.")
scatter_description.pack(pady=10)
# Scatter Plot specific logic here
y_pred = self.model.predict(self.X_test)
fig, ax1 = plt.subplots(figsize=(8, 6))
ax1.scatter(self.y_test, y_pred, c='blue', alpha=0.5)
fit = np.polyfit(self.y_test, y_pred, deg=1)
ax1.plot(self.y_test, fit[0] * self.y_test + fit[1], color='red', linewidth=2)
ax1.set_xlabel('Actual')
ax1.set_ylabel('Prediction')
ax1.set_title('Scatter Plot - KNN Regression')
canvas = FigureCanvasTkAgg(fig, master=scatter_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Line graph for cost function
cost_description_frame = ttk.Frame(notebook) # Change name to avoid conflicts
notebook.add(cost_description_frame, text='Cost Function')
cost_description = tk.Label(cost_description_frame, text="This graph represents the convergence of the cost function over iterations during model training.")
cost_description.pack(pady=10)
iterations = np.arange(1, 101)
cost_values = 50 / iterations + np.random.normal(scale=0.5, size=100) # Example cost values (randomized)
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(iterations, cost_values, marker='o', linestyle='-')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost Function')
ax.set_title('Cost Function Convergence')
canvas = FigureCanvasTkAgg(fig, master=cost_description_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Residual Plot page
residual_frame = ttk.Frame(notebook)
notebook.add(residual_frame, text='Residual Plot')
residual_description = tk.Label(residual_frame, text="This plot shows the residuals (differences between actual and predicted values) against predictions.")
residual_description.pack(pady=10)
# Residual Plot specific logic here
residuals = self.y_test - y_pred
fig, ax2 = plt.subplots(figsize=(8, 6))
ax2.scatter(y_pred, residuals, c='green', alpha=0.5)
ax2.axhline(y=0, color='red', linestyle='--', linewidth=2)
ax2.set_xlabel('Prediction')
ax2.set_ylabel('Residuals')
ax2.set_title('Residual Plot')
canvas = FigureCanvasTkAgg(fig, master=residual_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Histogram of Residuals page
histogram_frame = ttk.Frame(notebook)
notebook.add(histogram_frame, text='Histogram of Residuals')
histogram_description = tk.Label(histogram_frame, text="This histogram illustrates the distribution of residuals.")
histogram_description.pack(pady=10)
# Histogram of Residuals specific logic here
fig_hist, ax_hist = plt.subplots()
ax_hist.hist(residuals, bins=30, color='green', alpha=0.7)
ax_hist.set_xlabel('Residuals')
ax_hist.set_ylabel('Frequency')
ax_hist.set_title('Histogram of Residuals')
canvas_hist = FigureCanvasTkAgg(fig_hist, master=histogram_frame)
canvas_hist.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_hist.draw()
def evaluate_knn_classification(self, notebook):
# Create Metrics tab
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
# Classification-specific evaluation logic
y_pred = self.model.predict(self.X_test)
# Accuracy
accuracy = accuracy_score(self.y_test, y_pred)
metrics_description = "This page displays evaluation metrics for the K-Nearest Neighbors classification model."
metrics_label = tk.Label(metrics_frame, text=metrics_description + "\n\nEvaluation Metrics (Classification):\nAccuracy: {:.4f}".format(accuracy))
metrics_label.pack(pady=10)
# Confusion Matrix tab
cm_frame = ttk.Frame(notebook)
notebook.add(cm_frame, text='Confusion Matrix')
cm_description = "This page shows the confusion matrix for the K-Nearest Neighbors classification model."
cm_label = tk.Label(cm_frame, text=cm_description + "\n\nConfusion Matrix:")
cm_label.pack(pady=10)
# Display Confusion Matrix using seaborn heatmap
fig_cm = plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix(self.y_test, y_pred), annot=True, fmt='d', cmap='Blues', xticklabels=self.model.classes_, yticklabels=self.model.classes_)
plt.xlabel('Predictions')
plt.ylabel('True values')
plt.title('Confusion Matrix')
canvas_cm = FigureCanvasTkAgg(fig_cm, master=cm_frame)
canvas_cm.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_cm.draw()
def evaluate_random_forest_regression(self, notebook):
# Random Forest (Regression) specific evaluation logic here
# Metrics page
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
mse = mean_squared_error(self.y_test, self.model.predict(self.X_test))
r2 = r2_score(self.y_test, self.model.predict(self.X_test))
mae = mean_absolute_error(self.y_test, self.model.predict(self.X_test))
metrics_label = tk.Label(metrics_frame, text=f"Metrics:\nMean Squared Error: {mse:.4f}\nR-squared: {r2:.4f}\nMean Absolute Error: {mae:.4f}")
metrics_label.pack(pady=10)
# Feature Importance tab
feature_importance_frame = ttk.Frame(notebook)
notebook.add(feature_importance_frame, text='Feature Importance')
feature_importance_description = "This page illustrates the importance of each feature in the random forest regression model."
feature_importance_label = tk.Label(feature_importance_frame, text=feature_importance_description + "\n\nFeature Importance:")
feature_importance_label.pack(pady=10)
# Plot Feature Importance
fig_importance = plt.figure(figsize=(10, 6))
importance_values = self.model.feature_importances_
features = self.X_train.columns
importance_df = pd.DataFrame({'Feature': features, 'Importance': importance_values})
importance_df = importance_df.sort_values(by='Importance', ascending=False)
sns.barplot(x='Importance', y='Feature', data=importance_df, palette='viridis')
plt.xlabel('Importance')
plt.ylabel('Feature')
plt.title('Feature Importance')
canvas_importance = FigureCanvasTkAgg(fig_importance, master=feature_importance_frame)
canvas_importance.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_importance.draw()
def evaluate_random_forest_classification(self, notebook):
# Create Metrics tab
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
# Classification-specific evaluation logic
y_pred = self.model.predict(self.X_test)
# Accuracy
accuracy = accuracy_score(self.y_test, y_pred)
metrics_description = "This page displays evaluation metrics for the Random Forest classification model."
metrics_label = tk.Label(metrics_frame, text=metrics_description + "\n\nEvaluation Metrics (Classification):\nAccuracy: {:.4f}".format(accuracy))
metrics_label.pack(pady=10)
# Confusion Matrix tab
cm_frame = ttk.Frame(notebook)
notebook.add(cm_frame, text='Confusion Matrix')
cm_description = "This page shows the confusion matrix for the Random Forest classification model."
cm_label = tk.Label(cm_frame, text=cm_description + "\n\nConfusion Matrix:")
cm_label.pack(pady=10)
# Display Confusion Matrix using seaborn heatmap
fig_cm = plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix(self.y_test, y_pred), annot=True, fmt='d', cmap='Blues', xticklabels=self.model.classes_, yticklabels=self.model.classes_)
plt.xlabel('Predictions')
plt.ylabel('True values')
plt.title('Confusion Matrix')
canvas_cm = FigureCanvasTkAgg(fig_cm, master=cm_frame)
canvas_cm.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_cm.draw()
# Feature Importance tab
feature_importance_frame = ttk.Frame(notebook)
notebook.add(feature_importance_frame, text='Feature Importance')
feature_importance_description = "This page illustrates the importance of each feature in the random forest classification model."
feature_importance_label = tk.Label(feature_importance_frame, text=feature_importance_description + "\n\nFeature Importance:")
feature_importance_label.pack(pady=10)
# Plot Feature Importance
fig_importance = plt.figure(figsize=(10, 6))
importance_values = self.model.feature_importances_
features = self.X_train.columns
importance_df = pd.DataFrame({'Feature': features, 'Importance': importance_values})
importance_df = importance_df.sort_values(by='Importance', ascending=False)
sns.barplot(x='Importance', y='Feature', data=importance_df, palette='viridis')
plt.xlabel('Importance')
plt.ylabel('Feature')
plt.title('Feature Importance')
canvas_importance = FigureCanvasTkAgg(fig_importance, master=feature_importance_frame)
canvas_importance.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_importance.draw()
def evaluate_neural_network_regression(self, notebook):
# Artificial Neural Networks (Regression) specific evaluation logic here
# Metrics page
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
mse = mean_squared_error(self.y_test, self.model.predict(self.X_test))
r2 = r2_score(self.y_test, self.model.predict(self.X_test))
mae = mean_absolute_error(self.y_test, self.model.predict(self.X_test))
metrics_label = tk.Label(metrics_frame, text=f"Metrics:\nMean Squared Error: {mse:.4f}\nR-squared: {r2:.4f}\nMean Absolute Error: {mae:.4f}")
metrics_label.pack(pady=10)
# Line graph for loss function
loss_description_frame = ttk.Frame(notebook)
notebook.add(loss_description_frame, text='Loss Function')
loss_description = tk.Label(loss_description_frame, text="This graph represents the convergence of the loss function over epochs during model training.")
loss_description.pack(pady=10)
epochs = np.arange(1, 101)
loss_values = 0.5 / epochs + np.random.normal(scale=0.1, size=100) # Example loss values (randomized)
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(epochs, loss_values, marker='o', linestyle='-')
ax.set_xlabel('Epochs')
ax.set_ylabel('Loss Function')
ax.set_title('Loss Function Convergence')
canvas = FigureCanvasTkAgg(fig, master=loss_description_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
# Scatter Plot page
scatter_frame = ttk.Frame(notebook)
notebook.add(scatter_frame, text='Scatter Plot')
scatter_description = tk.Label(scatter_frame, text="This scatter plot compares the actual values with the predicted values.")
scatter_description.pack(pady=10)
# Scatter Plot specific logic here
y_pred = self.model.predict(self.X_test)
fig, ax1 = plt.subplots(figsize=(8, 6))
ax1.scatter(self.y_test, y_pred, c='blue', alpha=0.5)
fit = np.polyfit(self.y_test, y_pred, deg=1)
ax1.plot(self.y_test, fit[0] * self.y_test + fit[1], color='red', linewidth=2)
ax1.set_xlabel('Actual')
ax1.set_ylabel('Prediction')
ax1.set_title('Scatter Plot - Neural Network Regression')
canvas = FigureCanvasTkAgg(fig, master=scatter_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.draw()
def evaluate_neural_network_classification(self, notebook):
# Create Metrics tab
metrics_frame = ttk.Frame(notebook)
notebook.add(metrics_frame, text='Metrics')
# Classification-specific evaluation logic
y_pred = self.model.predict(self.X_test)
# Accuracy
accuracy = accuracy_score(self.y_test, y_pred)
metrics_description = "This page displays evaluation metrics for the Artificial Neural Networks classification model."
metrics_label = tk.Label(metrics_frame, text=metrics_description + "\n\nEvaluation Metrics (Classification):\nAccuracy: {:.4f}".format(accuracy))
metrics_label.pack(pady=10)
# Confusion Matrix tab
cm_frame = ttk.Frame(notebook)
notebook.add(cm_frame, text='Confusion Matrix')
cm_description = "This page shows the confusion matrix for the Artificial Neural Networks classification model."
cm_label = tk.Label(cm_frame, text=cm_description + "\n\nConfusion Matrix:")
cm_label.pack(pady=10)
# Display Confusion Matrix using seaborn heatmap
fig_cm = plt.figure(figsize=(8, 6))
sns.heatmap(confusion_matrix(self.y_test, y_pred), annot=True, fmt='d', cmap='Blues', xticklabels=self.model.classes_, yticklabels=self.model.classes_)
plt.xlabel('Predictions')
plt.ylabel('True values')
plt.title('Confusion Matrix')
canvas_cm = FigureCanvasTkAgg(fig_cm, master=cm_frame)
canvas_cm.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas_cm.draw()
def evaluate_kmeans(self, notebook):
# K-Means Clustering specific logic here
kmeans = KMeans(n_clusters=3) # You can adjust the number of clusters as needed
y_clusters = kmeans.fit_predict(self.X_test)
# Silhouette Score
silhouette_avg = silhouette_score(self.X_test, y_clusters)
# Calinski-Harabasz Index (another clustering metric)
calinski_harabasz = calinski_harabasz_score(self.X_test, y_clusters)
# Pairwise distances between data points and their assigned centers
_, distances = pairwise_distances_argmin_min(self.X_test, kmeans.cluster_centers_)
# Create K-Means tab
kmeans_frame = ttk.Frame(notebook)
notebook.add(kmeans_frame, text='K-Means Clustering')
kmeans_description = tk.Label(kmeans_frame, text="This page displays the results of K-Means Clustering.")
kmeans_description.pack(pady=10)
# Display Silhouette Score
silhouette_label = tk.Label(kmeans_frame, text=f"Silhouette Score: {silhouette_avg:.4f}")
silhouette_label.pack(pady=10)
# Display Calinski-Harabasz Index
calinski_label = tk.Label(kmeans_frame, text=f"Calinski-Harabasz Index: {calinski_harabasz:.4f}")
calinski_label.pack(pady=10)
# Scatter plot of the data points colored by clusters
plt.figure(figsize=(8, 6))
sns.scatterplot(x=self.X_test[:, 0], y=self.X_test[:, 1], hue=y_clusters, palette='viridis', s=50)
plt.title('K-Means Clustering Result')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend(title='Cluster')
plt.show()
# Display pairwise distances distribution
plt.figure(figsize=(8, 6))
sns.histplot(distances, kde=True)
plt.title('Pairwise Distances to Cluster Centers')
plt.xlabel('Distance to Nearest Cluster Center')
plt.ylabel('Frequency')
plt.show()
# Other evaluations like confusion matrix and classification report
confusion_mat = confusion_matrix(self.y_test, y_clusters)
classification_rep = classification_report(self.y_test, y_clusters)
# Display confusion matrix
plt.figure(figsize=(8, 6))
sns.heatmap(confusion_mat, annot=True, fmt='d', cmap='Blues', cbar=False)
plt.title('Confusion Matrix')
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.show()
# Display classification report
classification_rep_label = tk.Label(kmeans_frame, text=f"Classification Report:\n{classification_rep}")
classification_rep_label.pack(pady=10)