-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboard.py
More file actions
681 lines (549 loc) · 32.5 KB
/
Dashboard.py
File metadata and controls
681 lines (549 loc) · 32.5 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
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
from sklearn.model_selection import train_test_split
from Model_Registry import MODEL_REGISTRY
from Accuracy import calculate_metrics
import streamlit as st
import pandas as pd
import numpy as np
from Visualization import Visualization
from Data_Cleaning import DataCleaner
from Model_Training import split_data
from Standardization import Standardization
from model_io import save_model,load_model,list_saved_model
from sklearn.metrics import silhouette_score
page1,page2= st.tabs(["Model Training Dashboard","About"])
with page1:
st.write("# Model Training Dashboard")
st.markdown("""
Welcome to the Model Training Dashboard! This application allows you to upload your dataset, select a machine learning model from our registry, and train it directly within the dashboard. You can also visualize your data and evaluate model performance using various metrics.
""")
sidebar=st.sidebar
sidebar.title("Model Dashboard")
mode = sidebar.radio(
"Select Mode",
["Train New Model", "Use Existing Model"]
)
if "Algorithm_Type" not in st.session_state:
st.session_state[f"Algorithm_Type"]=None
if mode == "Use Existing Model":
sidebar.subheader("Select Existing Model")
saved_models = list_saved_model()
if not saved_models:
sidebar.warning("No saved models available")
st.stop()
selected_model = sidebar.selectbox(
"Available Models",
saved_models
)
if mode == "Use Existing Model":
if sidebar.button("Activate Model"):
bundle = load_model(selected_model)
st.session_state['active_model'] = bundle['model']
st.session_state['active_preprocessor'] = bundle['preprocessor']
sidebar.success("Model activated and ready")
# if mode == "Use Existing Model":
# pred_file = sidebar.file_uploader(
# "Upload data for prediction (CSV)",
# type=["csv"]
# )
# if pred_file:
# st.session_state['prediction_data'] = pd.read_csv(pred_file)
sidebar.title("Model Training Dashboard")
sidebar.write("Upload your dataset, select a model, and train it right here!")
sidebar.divider()
if mode == "Train New Model":
if 'data' not in st.session_state:
uploaded_file = sidebar.file_uploader(
"Upload your dataset (CSV format)",
type=["csv"]
)
if uploaded_file:
st.session_state['data'] = pd.read_csv(uploaded_file)
st.rerun()
else:
st.warning("Upload dataset to continue")
st.stop()
if sidebar.button("Reset and Upload New File"):
del st.session_state['data']
st.rerun()
if 'data' in st.session_state:
data=st.session_state['data']
else:
data=None
tab1, tab2, tab3,tab4 ,tab5,tab6,tab7= st.tabs(["Data Preview","Cleaning", "Visualization","Standarization" ,"Training","Test Model","Prediction"])
with tab1:
st.subheader("Data Preview")
if mode == "Train New Model":
if 'data' not in st.session_state:
st.info("Upload a dataset to preview")
st.stop()
head=st.button("Show Data head")
if head:
st.dataframe(data.head())
info=st.button("Show Data Summary")
if info:
st.dataframe(data.describe())
# else:
# if 'prediction_data' not in st.session_state:
# st.info("Upload data for prediction to preview")
# st.stop()
# st.dataframe(st.session_state['prediction_data'].head())
with tab2:
st.subheader("Data Cleaning Options")
if mode != "Train New Model":
st.info("Data cleaning disabled for existing models")
else:
if mode == "Train New Model":
if 'cleaner' not in st.session_state:
st.session_state['cleaner'] = DataCleaner(data)
cleaner = st.session_state['cleaner']
if st.button("Remove Duplicates"):
data=cleaner.remove_duplicates()
st.session_state['data']=data
st.dataframe(data.head())
st.success("Duplicates removed.")
st.rerun()
st.divider()
num_col=st.multiselect("Select Numerical Columns to fill Missing Values:", options=data.select_dtypes(include=[np.number]).columns.tolist())
strategy=st.selectbox("Select Strategy for Missing Values:", options=['mean', 'median', 'mode'])
if st.button("Fill Missing Values"):
if num_col:
data=cleaner.fill_missing_values(num_col,strategy=strategy)
st.session_state['data']=data
st.dataframe(data.head())
st.success("Missing values filled.")
st.divider()
cat_col=st.multiselect("Select the categorical columns to encode:", options=data.select_dtypes(include=['object']).columns.tolist())
if st.button("Encode Categorical Variables"):
if cat_col:
data=cleaner.encoding_coategorical(cat_col)
st.session_state['data']=data
st.dataframe(data.head())
st.success(f"Categorical column '{cat_col}' encoded.")
st.divider()
columns=st.multiselect("Select columns to drop:", options=data.columns.tolist())
if st.button("Drop Columns"):
if columns:
data=cleaner.drop_columns(columns)
st.session_state['data']=data
st.dataframe(data.head())
st.success(f"ID columns '{columns}' dropped.")
st.divider()
if st.button("Trim String Columns"):
data=cleaner.data_clipped_str()
st.session_state['data']=data
st.dataframe(data.head())
st.success("String columns trimmed.")
if st.button("Show Cleaned Data"):
st.dataframe(data.head())
st.divider()
st.subheader("Standard Feature Transformations")
col=st.selectbox("Select Column for Transformation:", options=data.select_dtypes(include=[np.number]).columns.tolist())
func=st.selectbox("Select Transformation Function:", options=['Log Transformation','Square Root Transformation ','Square Transformation','Absolute Transformation'])
if st.button("Apply Transformation"):
if col and func:
data=cleaner.custom_column(col,func)
st.session_state['data']=data
st.dataframe(data.head())
st.success(f"Applied {func} on column '{col}'.")
if mode=='Train New Model':
shape=sidebar.button("Check shape of your data")
if shape:
st.sidebar.info(f"Data Shape: {data.shape}")
if mode == "Train New Model":
X = sidebar.multiselect(
"Select Feature Columns (X)",
options=data.columns.tolist()
)
if st.session_state[f"Algorithm_Type"]!= "Clustering":
y = sidebar.selectbox(
"Select Target Column (y)",
options=data.columns.tolist()
)
else:
y=None
st.session_state['X'] = X
st.session_state['y'] = y
if mode == "Train New Model":
X = st.session_state.get('X')
y = st.session_state.get('y')
if mode == "Train New Model":
Algorithm_Type = st.session_state.get("Algorithm_Type")
split = sidebar.checkbox("Do you want to split the Data?")
if split:
test_size = sidebar.slider(
"Select Test Size (as a fraction):",
min_value=0.1,
max_value=0.5,
value=0.2,
step=0.05
)
random_state = sidebar.number_input(
"Enter Random State (integer):",
min_value=0,
value=42,
step=1
)
if Algorithm_Type == "Clustering":
if not X:
st.warning("Please select feature columns (X) for clustering.")
st.stop()
X_train = data[X]
st.session_state['X_train'] = X_train
st.session_state.pop('X_test', None)
st.session_state.pop('y_test', None)
st.session_state.pop('y_train', None)
sidebar.success("Data prepared for clustering (no train/test split).")
else:
if not X or not y:
st.warning("Please select both feature (X) and target (y) columns.")
st.stop()
X_train, X_test, y_train, y_test = split_data(
data,
feature_columns=X,
target_column=y,
test_size=test_size,
random_state=random_state
)
st.session_state['X_train'] = X_train
st.session_state['y_train'] = y_train
st.session_state['X_test'] = X_test
st.session_state['y_test'] = y_test
sidebar.success(
f"Data split completed (test size = {test_size})."
)
cv = sidebar.checkbox("Do you want a Cross-Validation set?")
st.session_state['use_cv'] = cv
if cv:
cv_size = sidebar.slider(
"Select CV set size:",
min_value=0.1,
max_value=0.5,
value=0.2,
step=0.05
)
random_state_cv = sidebar.number_input(
"Enter CV Random State (integer):",
min_value=0,
value=42,
step=1
)
X_test_final, X_cv, y_test_final, y_cv = train_test_split(
X_test,
y_test,
test_size=cv_size,
random_state=random_state_cv
)
st.session_state['X_test'] = X_test_final
st.session_state['y_test'] = y_test_final
st.session_state['X_cv'] = X_cv
st.session_state['y_cv'] = y_cv
with tab3:
st.subheader("Exploratory Data Analysis")
if mode == "Train New Model":
if X and y:
viz=Visualization(data,X,y)
if st.button("Show Scatter Plot"):
viz.scatter_plot()
if st.button("Show Histogram"):
viz.histogram()
if st.button("Show Box Plot"):
viz.box_plot()
if st.button("Show Heatmap"):
viz.heatmap()
else:
st.warning("Please select both feature (X) and target (y) columns from the sidebar to visualize the data.")
with tab4:
st.subheader("Data Standardization")
if mode == "Train New Model":
if 'scaler_object' not in st.session_state:
st.session_state['scaler_object'] = Standardization()
standard=st.session_state['scaler_object']
else:
st.info("Standardization is disabled for existing models")
is_train=mode=='Train New Model'
applies_to_X_train= st.checkbox("Fit_transform to X_train",disabled=not is_train)
if applies_to_X_train:
if 'scaler_object' not in st.session_state:
st.error("Scaler not initialized. Please reload the page.")
standard = st.session_state['scaler_object']
if 'X_train' in st.session_state:
X_train=st.session_state['X_train']
X_train_scaled=standard.fit_transform(X_train)
st.session_state['X_train']=X_train_scaled
st.dataframe(X_train_scaled.head())
st.success("Standardization applied to X_train.")
if 'X_cv' in st.session_state:
X_cv=st.session_state['X_cv']
X_cv_scaled=standard.transform(X_cv)
st.session_state['X_cv']=X_cv_scaled
st.dataframe(X_cv.head())
st.success("Standardization applied to X_cv.")
if 'X_test' in st.session_state:
X_test=st.session_state['X_test']
X_test_scaled=standard.transform(X_test)
st.session_state['X_test']=X_test_scaled
st.dataframe(X_test_scaled.head())
st.success("Standardization applied to X_test.")
else:
st.error("Please split the data and have X_train available before applying standardization.")
st.session_state['scaled']=True
# st.rerun()
with tab5:
if mode != "Train New Model":
st.info("Switch to Train New Model mode to train models.")
disabled = mode=='Use Existing Model'
st.subheader("Model Registry & Training")
compare_model=st.checkbox("Compare Model",disabled=disabled)
if compare_model:
MODEL_SLOT=['Model A','Model B']
else:
MODEL_SLOT=['Model A']
if 'trained_model' not in st.session_state:
st.session_state['trained_model']={}
if 'metrics_model' not in st.session_state:
st.session_state['metrics_model']={}
if not disabled:
for slot in MODEL_SLOT:
st.markdown(f"### {slot}")
algo_options = ["-- Select Algorithm Type --"] + list(MODEL_REGISTRY.keys())
Algorithm_Type=st.selectbox(f"Select Algorithm Type:{slot}", options=algo_options,key=f"{slot}_algo",disabled=disabled)
if Algorithm_Type == "-- Select Algorithm Type --":
Algorithm_Type = None
if Algorithm_Type is not None:
model_options=["-- Select Model Type --"] +list(MODEL_REGISTRY[Algorithm_Type].keys())
Model_Type=st.selectbox(f"Select Model Type: {slot}", options=model_options,key=f"{slot}_model",disabled=disabled)
st.session_state[f"{slot}_Algorithm_Type"]=Algorithm_Type
st.session_state[f"Algorithm_Type"]=Algorithm_Type
if Model_Type == "-- Select Model Type --":
Model_Type = None
if Model_Type is not None:
imp_options=["-- Select Implementation Type --"] +list(MODEL_REGISTRY[Algorithm_Type][Model_Type].keys())
Implementation_Type=st.selectbox(f"Select Implementation Type: {slot}", options=imp_options,key=f"{slot}_impl",disabled=disabled)
if Implementation_Type == "-- Select Implementation Type --":
Implementation_Type = None
if Implementation_Type is not None:
model=MODEL_REGISTRY[Algorithm_Type][Model_Type][Implementation_Type]
st.info(f"You have selected: {Algorithm_Type} > {Model_Type} > {Implementation_Type}")
st.write("Set Parameters:")
params={}
if Model_Type=='Kmeans':
params['k']=st.number_input("Enter No of clusters: ",min_value=2,max_value=10,value=3,step=1,key=f"{slot}_k")
params['max_iters']=st.number_input("Enter Maximum no of iterations: ",min_value=100,max_value=10000,value=500,step=100,key=f"{slot}_kmeans_iters")
if (Model_Type=='Binary_Classification')and Implementation_Type=='scratch':
params['lr']=st.slider("Slide to set learning rate: ",min_value=0.0001,max_value=0.1,value=0.01,step=0.0001,format="%.4f",key=f"{slot}_lr")
params['num_iters']=st.number_input("Enter Maximum no of iterations: ",min_value=100,max_value=10000,value=500,step=100,key=f"{slot}_num_iters")
params['threshold']=st.slider("Set classification threshold: ",min_value=0.0,max_value=1.0,value=0.5,step=0.01,format="%.2f",key=f"{slot}_threshold")
if Model_Type=='Decision_Tree':
params['max_depth']=st.number_input("Enter Maximum Depth of Tree: ",min_value=1,max_value=20,value=5,step=1,key=f"{slot}_dt_max_depth")
params['min_samples_split']=st.number_input("Enter Minimum Samples to Split: ",min_value=2,max_value=10,value=2,step=1,key=f"{slot}_dt_min_samples_split")
params['min_samples_leaf']=st.number_input("Enter Minimum Samples at Leaf Node: ",min_value=1,max_value=10,value=1,step=1,key=f"{slot}_dt_min_samples_leaf")
if Model_Type=='Random_Forest':
params['n_estimators']=st.number_input("Enter Number of Trees: ",min_value=10,max_value=500,value=100,step=10,key=f"{slot}_rf_n_estimators")
params['max_depth']=st.number_input("Enter Maximum Depth of Trees: ",min_value=1,max_value=20,value=5,step=1,key=f"{slot}_rf_max_depth")
params['min_samples_split']=st.number_input("Enter Minimum Samples to Split: ",min_value=2,max_value=10,value=2,step=1,key=f"{slot}_rf_min_samples_split")
params['min_samples_leaf']=st.number_input("Enter Minimum Samples at Leaf Node: ",min_value=1,max_value=10,value=1,step=1,key=f"{slot}_rf_min_samples_leaf")
if Model_Type=='XGBoost':
params['n_estimators']=st.number_input("Enter Number of Trees: ",min_value=10,max_value=500,value=100,step=10,key=f"{slot}_xgb_n_estimators")
params['learning_rate']=st.slider("Slide to set learning rate: ",min_value=0.0001,max_value=0.1,value=0.01,step=0.0001,format="%.4f",key=f"{slot}_xgb_lr")
params['max_depth']=st.number_input("Enter Maximum Depth of Trees: ",min_value=1,max_value=20,value=5,step=1,key=f"{slot}_xgb_max_depth")
if Model_Type=="Multiclass_Classification":
params['max_iters']=st.number_input("Enter Maximum no of iterations: ",min_value=100,max_value=10000,value=500,step=100,key=f"{slot}_multi_max_iters")
params['lr']=st.slider("Slide to set learning rate: ",min_value=0.0001,max_value=0.1,value=0.01,step=0.0001,format="%.4f",key=f"{slot}_multi_lr")
if Model_Type=="Linear_Regression"and Implementation_Type=='scratch':
params['lr']=st.slider("Slide to set learning rate: ",min_value=0.0001,max_value=0.1,value=0.01,step=0.0001,format="%.4f",key=f"{slot}_lin_lr")
params['max_iters']=st.number_input("Enter Maximum no of iterations: ",min_value=100,max_value=10000,value=500,step=100,key=f"{slot}_linreg_max_iters")
train=st.button(f"Train {slot}",key=f"train_{slot}",disabled=disabled)
if train:
if Algorithm_Type=="Clustering" and Model_Type=="Kmeans":
X_train=st.session_state['X_train']
model_instance=model(**params)
labels=model_instance.fit(X_train)
st.session_state['trained_model'][f"{slot}_model"]=model_instance
st.session_state[f"{slot}_labels"]=labels
labels = np.array(labels).ravel()
unique_clusters = np.unique(labels)
if len(unique_clusters) < 2:
st.error(
"Silhouette score requires at least 2 clusters. "
f"Found only {len(unique_clusters)} cluster."
)
score=silhouette_score(X_train,labels)
st.metric(label="Silhouette Score",
value=round(score,4))
X_train=st.session_state['X_train']
y_train=st.session_state['y_train']
with st.spinner("Training the model..."):
if params:
model_instance=model(**params)
else:
model_instance=model()
if Model_Type=='Gaussian Anomaly' and 'X_cv' in st.session_state and 'y_cv' in st.session_state:
X_cv=st.session_state['X_cv']
y_cv=st.session_state['y_cv']
model_instance.fit(X_train, y_train,X_cv,y_cv)
else:
model_instance.fit(X_train, y_train)
st.success("Model trained successfully!")
st.session_state['trained_model'][f"{slot}_model"]=model_instance
st.session_state['preprocessor'] = {
'cleaner':st.session_state['cleaner'] ,
'scaler': st.session_state['scaler_object'],
'features': st.session_state['X_train'].columns.tolist()
}
model_name=f"{slot}_{Algorithm_Type}_{Model_Type}_{Implementation_Type}"
save_path=save_model({
"model":model_instance,
"preprocessor":{
'cleaner':st.session_state['cleaner'] ,
'scaler': st.session_state['scaler_object'],
'features': st.session_state['X_train'].columns.tolist()
},
"algorithm_type":Algorithm_Type
},model_name)
st.success(f"Model saved as {model_name}")
use_cv = st.session_state.get('use_cv', False)
if use_cv and 'X_cv' in st.session_state and 'y_cv' in st.session_state:
X_eval = st.session_state['X_cv']
y_eval = st.session_state['y_cv']
eval_label = "Cross-Validation Set"
else:
X_eval = X_train
y_eval = y_train
eval_label = "Training Set"
model_instance=st.session_state['trained_model'][f"{slot}_model"]
y_eval_pred=model_instance.predict(X_eval)
st.session_state[f'y_eval_pred for {slot}']=y_eval_pred
if Algorithm_Type!="Clustering":
metrics_cv=calculate_metrics(y_eval, y_eval_pred)
st.session_state['metrics_model'][f"{slot}_metrics"]=metrics_cv
with st.expander(f"{slot} Model Performance"):
if f'{slot}_metrics' in st.session_state["metrics_model"]:
metrics_cv = st.session_state['metrics_model'][f"{slot}_metrics"]
tab_summary, tab_details = st.tabs(["Summary", "Detailed Metrics"])
with tab_summary:
if Algorithm_Type=='Regression':
st.subheader(f"{eval_label} Performance: {slot}")
st.write(f"MAE: {metrics_cv['MAE']:.4f}")
st.write(f"MSE: {metrics_cv['MSE']:.4f}")
st.write(f"RMSE: {metrics_cv['RMSE']:.4f}")
st.write(f"R2: {metrics_cv['R2']:.4f}")
else:
st.subheader(f"{eval_label} Performance: {slot}")
st.write(f"Accuracy: {metrics_cv['accuracy']:.4f}")
st.write(f"Precision: {metrics_cv['precision']:.4f}")
st.write(f"Recall: {metrics_cv['recall']:.4f}")
st.write(f"F1 Score: {metrics_cv['f1_score']:.4f}")
with tab_details:
if Algorithm_Type!='Regression':
st.write("Confusion Matrix:")
st.write(metrics_cv['confusion_matrix'])
st.write("Classification Report:")
st.text(metrics_cv['classification_report'])
else:
st.info("Not Valid for Continuous Values.")
if Algorithm_Type!='Regression':
with st.expander("Model Comparison"):
if 'Model A_metrics' in st.session_state["metrics_model"]:
st.divider()
st.subheader("Model Comparison")
col1,col2=st.columns(2)
with col1:
st.markdown("### Model A Performance")
metrics_a=st.session_state['metrics_model']['Model A_metrics']
st.write(f"Accuracy: {metrics_a['accuracy']:.4f}")
st.write(f"Precision: {metrics_a['precision']:.4f}")
st.write(f"Recall: {metrics_a['recall']:.4f}")
st.write(f"F1 Score: {metrics_a['f1_score']:.4f}")
if 'Model B_metrics' in st.session_state["metrics_model"]:
with col2:
st.markdown("### Model B Performance")
metrics_b=st.session_state['metrics_model']['Model B_metrics']
st.write(f"Accuracy: {metrics_b['accuracy']:.4f}")
st.write(f"Precision: {metrics_b['precision']:.4f}")
st.write(f"Recall: {metrics_b['recall']:.4f}")
st.write(f"F1 Score: {metrics_b['f1_score']:.4f}")
with tab6:
st.subheader("Test Set Evaluation")
Test_Model=st.button("Evaluate on Test Set",disabled=disabled)
if Test_Model:
for slot in MODEL_SLOT:
if f"{slot}_model" in st.session_state['trained_model']:
Algorithm_Type=st.session_state[f"{slot}_Algorithm_Type"]
st.markdown(f"### {slot} Test Results")
model=st.session_state['trained_model'][f"{slot}_model"]
X_test=st.session_state['X_test']
y_test=st.session_state['y_test']
y_test_pred=model.predict(X_test)
if Algorithm_Type=='Clustering':
st.write("Clustering models do not have traditional accuracy metrics.")
st.subheader("Test Set Performance")
labels=model.predict(X_test)
st.write("Cluster labels: ")
st.write(labels)
if hasattr(model,"centroids"):
st.write(f"Cluster Centers:")
st.dataframe(model.centroids)
else:
st.subheader("Test Set Performance")
metrics_test=calculate_metrics(y_test, y_test_pred)
if Algorithm_Type=='Regression':
st.write(f"MAE: {metrics_test['MAE']:.4f}")
st.write(f"MSE: {metrics_test['MSE']:.4f}")
st.write(f"RMSE: {metrics_test['RMSE']:.4f}")
st.write(f"R2: {metrics_test['R2']:.4f}")
else:
st.write(f"Accuracy: {metrics_test['accuracy']:.4f}")
st.write(f"Precision: {metrics_test['precision']:.4f}")
st.write(f"Recall: {metrics_test['recall']:.4f}")
st.write(f"F1 Score: {metrics_test['f1_score']:.4f}")
st.write("Confusion Matrix:")
st.write(metrics_test['confusion_matrix'])
st.write("Classification Report:")
st.text(metrics_test['classification_report'])
else:
st.warning("Please train a model first in the 'Training' tab before evaluating on the test set.")
with tab7:
st.subheader("Make predictions and Download")
pred_file=st.file_uploader("Upload CSV file for prediction",type=['csv'])
if pred_file:
pred_data=pd.read_csv(pred_file)
st.write("Prediction Data Preview")
st.dataframe(pred_data.head())
if st.button("Run Prediction"):
if mode == "Use Existing Model":
if 'active_model' not in st.session_state:
st.warning("Activate a model first")
st.stop()
model = st.session_state['active_model']
pre = st.session_state['active_preprocessor']
else:
trained_models = st.session_state.get('trained_model', {})
if not trained_models:
st.error("No trained model available. Train a model first.")
st.stop()
model = next(iter(trained_models.values()))
pre = st.session_state['preprocessor']
df = pre['cleaner'].transform(pred_data)
df = df.reindex(columns=pre['features'], fill_value=0)
scaler = pre.get("scaler")
if scaler is not None and getattr(scaler, "scaled_data", None) is not None:
df_scaled = scaler.transform(df)
else:
df_scaled = df
preds = model.predict(df_scaled)
result = pred_data.copy()
if Algorithm_Type=="Clustering":
result["Clusters"] = preds
else:
result["Prediction"] = preds
st.session_state['prediction'] = result
st.success("Prediction Complete")
st.dataframe(result.head())
if 'prediction' in st.session_state:
csv = result.to_csv(index=False).encode("utf-8")
st.download_button(
"Download Prediction",
csv,
"prediction.csv",
"text/csv"
)