-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
194 lines (149 loc) · 6.34 KB
/
app.py
File metadata and controls
194 lines (149 loc) · 6.34 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
# ============================================================
# 🚀 Regression Model Selection - Advanced Streamlit App
# ============================================================
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.svm import SVR
from sklearn.metrics import r2_score
# ============================================================
# 🎨 Page Configuration
# ============================================================
st.set_page_config(
page_title="Regression Model Selector",
page_icon="📊",
layout="wide"
)
# ============================================================
# 🎨 Custom CSS Styling (Modern UI)
# ============================================================
st.markdown("""
<style>
body {
background: linear-gradient(135deg, #1f4037, #99f2c8);
}
.main-title {
font-size: 40px;
font-weight: bold;
color: #ffffff;
text-align: center;
}
.sub-title {
font-size: 18px;
color: #f0f0f0;
text-align: center;
}
.stButton>button {
background-color: #ff7b00;
color: white;
border-radius: 10px;
height: 3em;
width: 100%;
font-size: 18px;
}
.result-box {
padding: 15px;
border-radius: 10px;
background-color: #ffffff;
margin-bottom: 10px;
}
</style>
""", unsafe_allow_html=True)
# ============================================================
# 🏷️ Title Section
# ============================================================
st.markdown('<div class="main-title">📊 Regression Model Selection App</div>', unsafe_allow_html=True)
st.markdown('<div class="sub-title">Compare Multiple ML Models & Find the Best One</div>', unsafe_allow_html=True)
st.write("")
# ============================================================
# 📂 File Upload
# ============================================================
st.sidebar.header("📂 Upload Dataset")
file = st.sidebar.file_uploader("Upload CSV File", type=["csv"])
if file is not None:
dataset = pd.read_csv(file)
st.subheader("📌 Dataset Preview")
st.dataframe(dataset.head())
# ============================================================
# ⚙️ Feature Selection
# ============================================================
st.sidebar.header("⚙️ Configuration")
target_column = st.sidebar.selectbox("Select Target Column", dataset.columns)
X = dataset.drop(columns=[target_column]).values
y = dataset[target_column].values
# ============================================================
# 📊 Train Test Split
# ============================================================
test_size = st.sidebar.slider("Test Size (%)", 10, 40, 20) / 100
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=0
)
# ============================================================
# 🤖 Model Training Button
# ============================================================
if st.button("🚀 Train & Compare Models"):
# Linear Regression
modelLR = LinearRegression()
modelLR.fit(X_train, y_train)
# Polynomial Regression
poly_reg = PolynomialFeatures(degree=4)
X_poly = poly_reg.fit_transform(X_train)
modelPLR = LinearRegression()
modelPLR.fit(X_poly, y_train)
# Random Forest
modelRFR = RandomForestRegressor(n_estimators=50, random_state=0)
modelRFR.fit(X_train, y_train)
# Decision Tree
modelDTR = DecisionTreeRegressor(random_state=0)
modelDTR.fit(X_train, y_train)
# SVR (with scaling ✅)
sc_X = StandardScaler()
sc_y = StandardScaler()
X_train_scaled = sc_X.fit_transform(X_train)
y_train_scaled = sc_y.fit_transform(y_train.reshape(-1, 1))
modelSVR = SVR(kernel='rbf')
modelSVR.fit(X_train_scaled, y_train_scaled.ravel())
# ============================================================
# 📈 Predictions
# ============================================================
modelLR_pred = modelLR.predict(X_test)
modelPLR_pred = modelPLR.predict(poly_reg.transform(X_test))
modelRFR_pred = modelRFR.predict(X_test)
modelDTR_pred = modelDTR.predict(X_test)
X_test_scaled = sc_X.transform(X_test)
modelSVR_pred = sc_y.inverse_transform(
modelSVR.predict(X_test_scaled).reshape(-1, 1)
)
# ============================================================
# 📊 Accuracy Results
# ============================================================
results = {
"Linear Regression": r2_score(y_test, modelLR_pred),
"Polynomial Regression": r2_score(y_test, modelPLR_pred),
"Random Forest": r2_score(y_test, modelRFR_pred),
"Decision Tree": r2_score(y_test, modelDTR_pred),
"Support Vector Regression": r2_score(y_test, modelSVR_pred)
}
results_df = pd.DataFrame(list(results.items()), columns=["Model", "R2 Score"])
# ============================================================
# 🏆 Best Model Highlight
# ============================================================
best_model = max(results, key=results.get)
st.success(f"🏆 Best Model: {best_model}")
# ============================================================
# 📊 Display Results
# ============================================================
st.subheader("📊 Model Performance Comparison")
st.dataframe(results_df)
# ============================================================
# 📉 Visualization
# ============================================================
st.subheader("📈 Performance Chart")
st.bar_chart(results_df.set_index("Model"))
else:
st.info("👈 Upload a dataset to get started")