-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathmodel_comparison.py
More file actions
104 lines (86 loc) · 3.18 KB
/
model_comparison.py
File metadata and controls
104 lines (86 loc) · 3.18 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
import streamlit as st
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import (
accuracy_score,
precision_score,
recall_score,
f1_score,
mean_absolute_error,
r2_score,
root_mean_squared_error
)
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
def show():
st.subheader("📊 Model Comparison")
if "df" not in st.session_state or st.session_state.df is None:
st.info("Upload or generate a dataset from the sidebar.")
return
df = st.session_state.df.copy()
st.dataframe(df.head(), use_container_width=True)
target_col = st.selectbox(
"Select Target Column",
df.columns,
key="model_comp_target"
)
X = df.drop(columns=[target_col])
y = df[target_col]
# Convert features
X = pd.get_dummies(X, drop_first=True)
# Detect task type
is_regression = pd.api.types.is_numeric_dtype(y) and y.nunique() > 20
task_type = "Regression" if is_regression else "Classification"
st.success(f"Detected task type: **{task_type}**")
# Encode target if classification
if task_type == "Classification" and y.dtype == "object":
y = LabelEncoder().fit_transform(y)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
if task_type == "Classification":
models = {
"KNN": KNeighborsClassifier(),
"Decision Tree": DecisionTreeClassifier(),
"SVM": SVC(),
"Logistic Regression": LogisticRegression(max_iter=1000),
"Random Forest": RandomForestClassifier()
}
else:
models = {
"Linear Regression": LinearRegression(),
"Random Forest Regressor": RandomForestRegressor()
}
selected_models = st.multiselect(
"Select models",
list(models.keys()),
default=list(models.keys()),
key="model_select"
)
results = []
for name in selected_models:
model = models[name]
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
if task_type == "Classification":
results.append({
"Model": name,
"Accuracy": accuracy_score(y_test, y_pred),
"Precision": precision_score(y_test, y_pred, average="weighted", zero_division=0),
"Recall": recall_score(y_test, y_pred, average="weighted", zero_division=0),
"F1 Score": f1_score(y_test, y_pred, average="weighted", zero_division=0),
})
else:
results.append({
"Model": name,
"RMSE": root_mean_squared_error(y_test, y_pred),
"MAE": mean_absolute_error(y_test, y_pred),
"R²": r2_score(y_test, y_pred),
})
st.dataframe(pd.DataFrame(results), use_container_width=True)