Spaces:
Sleeping
Sleeping
eaglelandsonce
commited on
Create 42_regression.py
Browse files- pages/42_regression.py +49 -0
pages/42_regression.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import plotly.graph_objects as go
|
3 |
+
|
4 |
+
# Title
|
5 |
+
st.title("Interactive Equation Plotter")
|
6 |
+
|
7 |
+
# Sidebar sliders
|
8 |
+
st.sidebar.header("Controls")
|
9 |
+
w = st.sidebar.slider('W (slope)', min_value=-10.0, max_value=10.0, value=1.0, step=0.1)
|
10 |
+
b = st.sidebar.slider('B (intercept)', min_value=-100.0, max_value=100.0, value=0.0, step=1.0)
|
11 |
+
|
12 |
+
# Initial x position
|
13 |
+
x = st.slider('X position', min_value=-100.0, max_value=100.0, value=0.0, step=1.0)
|
14 |
+
|
15 |
+
# Calculate y based on the equation y = w * x + b
|
16 |
+
y = w * x + b
|
17 |
+
|
18 |
+
# Create the plot
|
19 |
+
fig = go.Figure()
|
20 |
+
|
21 |
+
# Plot for the y-axis (top line)
|
22 |
+
fig.add_shape(type="line", x0=-100, x1=100, y0=1, y1=1,
|
23 |
+
line=dict(color="blue", width=2, dash="dash"))
|
24 |
+
|
25 |
+
# Plot for the x-axis (bottom line)
|
26 |
+
fig.add_shape(type="line", x0=-100, x1=100, y0=-1, y1=-1,
|
27 |
+
line=dict(color="blue", width=2, dash="dash"))
|
28 |
+
|
29 |
+
# Plot the x point
|
30 |
+
fig.add_trace(go.Scatter(x=[x], y=[-1], mode='markers', marker=dict(color='red', size=10), name="X point"))
|
31 |
+
|
32 |
+
# Plot the y point
|
33 |
+
fig.add_trace(go.Scatter(x=[y], y=[1], mode='markers', marker=dict(color='red', size=10), name="Y point"))
|
34 |
+
|
35 |
+
# Update the layout
|
36 |
+
fig.update_layout(
|
37 |
+
title=f'y = {w} * x + {b}',
|
38 |
+
xaxis=dict(range=[-100, 100]),
|
39 |
+
yaxis=dict(range=[-2, 2], showticklabels=False),
|
40 |
+
showlegend=False,
|
41 |
+
height=400,
|
42 |
+
margin=dict(t=50, b=10)
|
43 |
+
)
|
44 |
+
|
45 |
+
# Display the plot in Streamlit
|
46 |
+
st.plotly_chart(fig)
|
47 |
+
|
48 |
+
# Add instruction to click and drag the point on x-axis
|
49 |
+
st.write("Drag the red dot on the x-axis to change the x position.")
|