Update src/streamlit_app.py
Browse files- src/streamlit_app.py +29 -29
src/streamlit_app.py
CHANGED
|
@@ -7,18 +7,18 @@ from sklearn.metrics import mean_squared_error
|
|
| 7 |
|
| 8 |
st.set_page_config(page_title="Linear Regression Playground", layout="centered")
|
| 9 |
|
| 10 |
-
# FIX:
|
| 11 |
st.markdown("""
|
| 12 |
<style>
|
| 13 |
.eq-box {
|
| 14 |
-
width: 100%;
|
| 15 |
-
background-color: #f7f7f9;
|
| 16 |
-
padding: 18px;
|
| 17 |
-
border-radius: 10px;
|
| 18 |
border: 2px solid #333;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
font-size: 22px;
|
| 20 |
text-align: center;
|
| 21 |
-
margin-top:
|
| 22 |
}
|
| 23 |
</style>
|
| 24 |
""", unsafe_allow_html=True)
|
|
@@ -26,7 +26,9 @@ st.markdown("""
|
|
| 26 |
st.title("π Linear Regression Playground (2D & 3D)")
|
| 27 |
st.write("Experiment with regression, noise, slope, intercept β and visualize the model!")
|
| 28 |
|
|
|
|
| 29 |
# Sidebar Controls
|
|
|
|
| 30 |
st.sidebar.header("βοΈ Controls")
|
| 31 |
|
| 32 |
mode = st.sidebar.radio("Choose Mode", ["2D Regression", "3D Regression"])
|
|
@@ -47,7 +49,10 @@ if mode != st.session_state.current_mode:
|
|
| 47 |
st.session_state.trained = False
|
| 48 |
st.session_state.current_mode = mode
|
| 49 |
|
|
|
|
|
|
|
| 50 |
# Generate dataset
|
|
|
|
| 51 |
if train_btn:
|
| 52 |
|
| 53 |
with st.spinner("β³ Training model..."):
|
|
@@ -82,12 +87,15 @@ if train_btn:
|
|
| 82 |
|
| 83 |
st.session_state.trained = True
|
| 84 |
|
|
|
|
|
|
|
| 85 |
# Visualization
|
|
|
|
| 86 |
if st.session_state.trained:
|
| 87 |
|
| 88 |
st.success("π Model trained successfully!")
|
| 89 |
|
| 90 |
-
# -------- 2D Regression --------
|
| 91 |
if mode == "2D Regression":
|
| 92 |
X, y, y_pred, mse, model = st.session_state.data
|
| 93 |
|
|
@@ -104,17 +112,12 @@ if st.session_state.trained:
|
|
| 104 |
with col2:
|
| 105 |
st.metric("MSE", f"{mse:.4f}")
|
| 106 |
|
| 107 |
-
# FULL
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
""",
|
| 114 |
-
unsafe_allow_html=True
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
# -------- 3D Regression --------
|
| 118 |
else:
|
| 119 |
X1, X2, Z, Z_pred, mse, model = st.session_state.data
|
| 120 |
|
|
@@ -127,8 +130,10 @@ if st.session_state.trained:
|
|
| 127 |
ax = fig.add_subplot(111, projection="3d")
|
| 128 |
|
| 129 |
idx = np.random.choice(len(Z.ravel()), min(350, len(Z.ravel())), replace=False)
|
| 130 |
-
ax.scatter(
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
ax.plot_surface(X1, X2, Z_pred, alpha=0.75, color="blue")
|
| 134 |
ax.set_title("3D Linear Regression")
|
|
@@ -159,15 +164,10 @@ if st.session_state.trained:
|
|
| 159 |
b = model.coef_[1]
|
| 160 |
c = model.intercept_
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
$$z = {a:.3f}x_1 + {b:.3f}x_2 + {c:.3f}$$
|
| 167 |
-
</div>
|
| 168 |
-
""",
|
| 169 |
-
unsafe_allow_html=True
|
| 170 |
-
)
|
| 171 |
|
| 172 |
else:
|
| 173 |
st.info("Click **Generate & Train Model** to begin.")
|
|
|
|
| 7 |
|
| 8 |
st.set_page_config(page_title="Linear Regression Playground", layout="centered")
|
| 9 |
|
| 10 |
+
# FIX: make equation always fully visible + box formatting
|
| 11 |
st.markdown("""
|
| 12 |
<style>
|
| 13 |
.eq-box {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
border: 2px solid #333;
|
| 15 |
+
border-radius: 8px;
|
| 16 |
+
background: #ffffff;
|
| 17 |
+
padding: 14px;
|
| 18 |
+
width: 100%;
|
| 19 |
font-size: 22px;
|
| 20 |
text-align: center;
|
| 21 |
+
margin-top: 14px;
|
| 22 |
}
|
| 23 |
</style>
|
| 24 |
""", unsafe_allow_html=True)
|
|
|
|
| 26 |
st.title("π Linear Regression Playground (2D & 3D)")
|
| 27 |
st.write("Experiment with regression, noise, slope, intercept β and visualize the model!")
|
| 28 |
|
| 29 |
+
# ------------------------------------
|
| 30 |
# Sidebar Controls
|
| 31 |
+
# ------------------------------------
|
| 32 |
st.sidebar.header("βοΈ Controls")
|
| 33 |
|
| 34 |
mode = st.sidebar.radio("Choose Mode", ["2D Regression", "3D Regression"])
|
|
|
|
| 49 |
st.session_state.trained = False
|
| 50 |
st.session_state.current_mode = mode
|
| 51 |
|
| 52 |
+
|
| 53 |
+
# ------------------------------------
|
| 54 |
# Generate dataset
|
| 55 |
+
# ------------------------------------
|
| 56 |
if train_btn:
|
| 57 |
|
| 58 |
with st.spinner("β³ Training model..."):
|
|
|
|
| 87 |
|
| 88 |
st.session_state.trained = True
|
| 89 |
|
| 90 |
+
|
| 91 |
+
# ------------------------------------
|
| 92 |
# Visualization
|
| 93 |
+
# ------------------------------------
|
| 94 |
if st.session_state.trained:
|
| 95 |
|
| 96 |
st.success("π Model trained successfully!")
|
| 97 |
|
| 98 |
+
# ----------------- 2D Regression -----------------
|
| 99 |
if mode == "2D Regression":
|
| 100 |
X, y, y_pred, mse, model = st.session_state.data
|
| 101 |
|
|
|
|
| 112 |
with col2:
|
| 113 |
st.metric("MSE", f"{mse:.4f}")
|
| 114 |
|
| 115 |
+
# FIXED FULL EQUATION BOX
|
| 116 |
+
equation = rf"y = {model.coef_[0]:.3f}x + {model.intercept_:.3f}"
|
| 117 |
+
st.markdown(f"<div class='eq-box'>${equation}$</div>", unsafe_allow_html=True)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# ----------------- 3D Regression -----------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
else:
|
| 122 |
X1, X2, Z, Z_pred, mse, model = st.session_state.data
|
| 123 |
|
|
|
|
| 130 |
ax = fig.add_subplot(111, projection="3d")
|
| 131 |
|
| 132 |
idx = np.random.choice(len(Z.ravel()), min(350, len(Z.ravel())), replace=False)
|
| 133 |
+
ax.scatter(
|
| 134 |
+
X1.ravel()[idx], X2.ravel()[idx], Z.ravel()[idx],
|
| 135 |
+
color="orange", alpha=0.25, s=8
|
| 136 |
+
)
|
| 137 |
|
| 138 |
ax.plot_surface(X1, X2, Z_pred, alpha=0.75, color="blue")
|
| 139 |
ax.set_title("3D Linear Regression")
|
|
|
|
| 164 |
b = model.coef_[1]
|
| 165 |
c = model.intercept_
|
| 166 |
|
| 167 |
+
equation3d = rf"z = {a:.3f}x_1 + {b:.3f}x_2 + {c:.3f}"
|
| 168 |
+
|
| 169 |
+
# FIXED FULL EQUATION BOX
|
| 170 |
+
st.markdown(f"<div class='eq-box'>${equation3d}$</div>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
else:
|
| 173 |
st.info("Click **Generate & Train Model** to begin.")
|