Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,14 +8,11 @@ from sklearn.metrics import mean_squared_error, r2_score
|
|
| 8 |
def perform_linear_regression(X, Y):
|
| 9 |
X = np.array(X).reshape(-1, 1) # Reshape for sklearn
|
| 10 |
Y = np.array(Y)
|
| 11 |
-
|
| 12 |
model = LinearRegression()
|
| 13 |
model.fit(X, Y)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
intercept = model.intercept_
|
| 17 |
-
|
| 18 |
-
return model, slope, intercept
|
| 19 |
|
| 20 |
# Streamlit interface
|
| 21 |
st.title("Linear Regression Visualization")
|
|
@@ -42,10 +39,10 @@ except ValueError:
|
|
| 42 |
|
| 43 |
# Proceed only if both vectors are valid and have 3 elements each
|
| 44 |
if len(x_values) == 3 and len(y_values) == 3:
|
| 45 |
-
model
|
| 46 |
-
|
| 47 |
-
slope = round(
|
| 48 |
-
intercept = round(
|
| 49 |
|
| 50 |
st.write(f"Slope: {slope}")
|
| 51 |
st.write(f"Intercept: {intercept}")
|
|
@@ -54,31 +51,34 @@ if len(x_values) == 3 and len(y_values) == 3:
|
|
| 54 |
with col2:
|
| 55 |
plt.figure(figsize=(8, 6))
|
| 56 |
plt.scatter(x_values, y_values, color='red', label='Data Points')
|
| 57 |
-
|
| 58 |
x_range = np.linspace(min(x_values), max(x_values), 100)
|
| 59 |
y_range = slope * x_range + intercept
|
| 60 |
plt.plot(x_range, y_range, color='blue', label='Regression Line')
|
| 61 |
-
|
| 62 |
plt.xlabel("X")
|
| 63 |
plt.ylabel("Y")
|
| 64 |
plt.legend()
|
| 65 |
plt.title("Linear Regression Visualization")
|
| 66 |
-
|
| 67 |
st.pyplot(plt)
|
| 68 |
|
| 69 |
# Prompt for a test value of X and predict its corresponding Y
|
| 70 |
test_x_input = st.number_input("Enter a test value for X", value=0.0)
|
| 71 |
-
|
| 72 |
if test_x_input:
|
| 73 |
predicted_y = model.predict(np.array([[test_x_input]]))[0]
|
| 74 |
st.write(f"Predicted Y for X = {test_x_input}: {predicted_y}")
|
| 75 |
-
|
| 76 |
# Performance metrics
|
|
|
|
|
|
|
|
|
|
| 77 |
y_predicted = model.predict(X)
|
| 78 |
mse = mean_squared_error(Y, y_predicted)
|
| 79 |
-
|
| 80 |
|
| 81 |
st.write(f"Mean Squared Error: {mse}")
|
| 82 |
-
|
| 83 |
else:
|
| 84 |
st.write("Please provide valid inputs with 3 values each.")
|
|
|
|
| 8 |
def perform_linear_regression(X, Y):
|
| 9 |
X = np.array(X).reshape(-1, 1) # Reshape for sklearn
|
| 10 |
Y = np.array(Y)
|
| 11 |
+
|
| 12 |
model = LinearRegression()
|
| 13 |
model.fit(X, Y)
|
| 14 |
+
|
| 15 |
+
return model
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Streamlit interface
|
| 18 |
st.title("Linear Regression Visualization")
|
|
|
|
| 39 |
|
| 40 |
# Proceed only if both vectors are valid and have 3 elements each
|
| 41 |
if len(x_values) == 3 and len(y_values) == 3:
|
| 42 |
+
model = perform_linear_regression(x_values, y_values)
|
| 43 |
+
|
| 44 |
+
slope = round(model.coef_[0], 2)
|
| 45 |
+
intercept = round(model.intercept_, 2)
|
| 46 |
|
| 47 |
st.write(f"Slope: {slope}")
|
| 48 |
st.write(f"Intercept: {intercept}")
|
|
|
|
| 51 |
with col2:
|
| 52 |
plt.figure(figsize=(8, 6))
|
| 53 |
plt.scatter(x_values, y_values, color='red', label='Data Points')
|
| 54 |
+
|
| 55 |
x_range = np.linspace(min(x_values), max(x_values), 100)
|
| 56 |
y_range = slope * x_range + intercept
|
| 57 |
plt.plot(x_range, y_range, color='blue', label='Regression Line')
|
| 58 |
+
|
| 59 |
plt.xlabel("X")
|
| 60 |
plt.ylabel("Y")
|
| 61 |
plt.legend()
|
| 62 |
plt.title("Linear Regression Visualization")
|
| 63 |
+
|
| 64 |
st.pyplot(plt)
|
| 65 |
|
| 66 |
# Prompt for a test value of X and predict its corresponding Y
|
| 67 |
test_x_input = st.number_input("Enter a test value for X", value=0.0)
|
| 68 |
+
|
| 69 |
if test_x_input:
|
| 70 |
predicted_y = model.predict(np.array([[test_x_input]]))[0]
|
| 71 |
st.write(f"Predicted Y for X = {test_x_input}: {predicted_y}")
|
| 72 |
+
|
| 73 |
# Performance metrics
|
| 74 |
+
X = np.array(x_values).reshape(-1, 1)
|
| 75 |
+
Y = np.array(y_values)
|
| 76 |
+
|
| 77 |
y_predicted = model.predict(X)
|
| 78 |
mse = mean_squared_error(Y, y_predicted)
|
| 79 |
+
r2 = r2_score(Y, y_predicted)
|
| 80 |
|
| 81 |
st.write(f"Mean Squared Error: {mse}")
|
| 82 |
+
st.write(f"R^2 Score: {r2}")
|
| 83 |
else:
|
| 84 |
st.write("Please provide valid inputs with 3 values each.")
|