Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,14 @@ import numpy as np
|
|
| 3 |
from sklearn.linear_model import LinearRegression
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
|
| 6 |
-
#
|
| 7 |
def perform_linear_regression(X, Y):
|
| 8 |
X = np.array(X).reshape(-1, 1) # Reshape for sklearn
|
| 9 |
Y = np.array(Y)
|
| 10 |
|
| 11 |
-
# Initialize and fit the linear model
|
| 12 |
model = LinearRegression()
|
| 13 |
model.fit(X, Y)
|
| 14 |
|
| 15 |
-
# Get the slope and intercept
|
| 16 |
slope = model.coef_[0]
|
| 17 |
intercept = model.intercept_
|
| 18 |
|
|
@@ -23,42 +21,41 @@ st.title("Linear Regression App")
|
|
| 23 |
|
| 24 |
st.subheader("Input vectors X and Y")
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Check if both vectors are filled
|
| 31 |
if len(x_values) == 3 and len(y_values) == 3:
|
| 32 |
slope, intercept = perform_linear_regression(x_values, y_values)
|
| 33 |
|
| 34 |
-
# Rounded values for display
|
| 35 |
slope = round(slope, 2)
|
| 36 |
intercept = round(intercept, 2)
|
| 37 |
-
|
| 38 |
-
# Add labels for slope and intercept
|
| 39 |
-
#plt.text(min(x_values), min(y_range), f"Slope: {slope}", fontsize=12, color='blue')
|
| 40 |
-
#plt.text(min(x_values), min(y_range) - 0.5, f"Intercept: {intercept}", fontsize=12, color='blue')
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
st.pyplot(plt)
|
| 62 |
-
|
| 63 |
else:
|
| 64 |
st.write("Please provide 3 values for both vectors X and Y.")
|
|
|
|
| 3 |
from sklearn.linear_model import LinearRegression
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
|
| 6 |
+
# Function for linear regression
|
| 7 |
def perform_linear_regression(X, Y):
|
| 8 |
X = np.array(X).reshape(-1, 1) # Reshape for sklearn
|
| 9 |
Y = np.array(Y)
|
| 10 |
|
|
|
|
| 11 |
model = LinearRegression()
|
| 12 |
model.fit(X, Y)
|
| 13 |
|
|
|
|
| 14 |
slope = model.coef_[0]
|
| 15 |
intercept = model.intercept_
|
| 16 |
|
|
|
|
| 21 |
|
| 22 |
st.subheader("Input vectors X and Y")
|
| 23 |
|
| 24 |
+
# Create two columns
|
| 25 |
+
col1, col2 = st.columns(2)
|
| 26 |
+
|
| 27 |
+
# Input fields for vectors X and Y in the first column
|
| 28 |
+
with col1:
|
| 29 |
+
st.write("Enter values for X and Y:")
|
| 30 |
+
|
| 31 |
+
x_values = [st.number_input(f"X[{i}]", value=0.0, key=f'x{i}') for i in range(3)]
|
| 32 |
+
y_values = [st.number_input(f"Y[{i}]", value=0.0, key=f'y{i}') for i in range(3)]
|
| 33 |
|
| 34 |
# Check if both vectors are filled
|
| 35 |
if len(x_values) == 3 and len(y_values) == 3:
|
| 36 |
slope, intercept = perform_linear_regression(x_values, y_values)
|
| 37 |
|
|
|
|
| 38 |
slope = round(slope, 2)
|
| 39 |
intercept = round(intercept, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Display the slope and intercept at the top of the window
|
| 42 |
+
st.write(f"Slope: {slope}")
|
| 43 |
+
st.write(f"Intercept: {intercept}")
|
| 44 |
+
|
| 45 |
+
# Display graph in the second column
|
| 46 |
+
with col2:
|
| 47 |
+
plt.figure(figsize=(8, 6))
|
| 48 |
+
plt.scatter(x_values, y_values, color='red', label='Data Points')
|
| 49 |
+
|
| 50 |
+
x_range = np.linspace(min(x_values), max(x_values), 100)
|
| 51 |
+
y_range = slope * x_range + intercept
|
| 52 |
+
plt.plot(x_range, y_range, color='blue', label='Regression Line')
|
| 53 |
+
|
| 54 |
+
plt.xlabel("X")
|
| 55 |
+
plt.ylabel("Y")
|
| 56 |
+
plt.legend()
|
| 57 |
+
plt.title("Linear Regression Visualization")
|
| 58 |
+
|
| 59 |
+
st.pyplot(plt)
|
|
|
|
|
|
|
| 60 |
else:
|
| 61 |
st.write("Please provide 3 values for both vectors X and Y.")
|