Spaces:
Runtime error
Runtime error
File size: 1,465 Bytes
9b838e2 4aa3051 479c184 4aa3051 9b838e2 4aa3051 479c184 | 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 | import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
st.title("📈 Simple Linear Regression App")
st.write("Upload a CSV file and perform linear regression.")
# Upload CSV
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
st.subheader("Data Preview")
st.dataframe(df.head())
# Select columns
columns = df.columns.tolist()
x_col = st.selectbox("Select feature (X)", columns)
y_col = st.selectbox("Select target (y)", columns)
if x_col != y_col:
X = df[[x_col]]
y = df[y_col]
# Train model
model = LinearRegression()
model.fit(X, y)
# Predictions
y_pred = model.predict(X)
# Results
st.subheader("Model Results")
st.write(f"**Coefficient:** {model.coef_[0]:.4f}")
st.write(f"**Intercept:** {model.intercept_:.4f}")
st.write(f"**R² Score:** {r2_score(y, y_pred):.4f}")
# Plot
st.subheader("Regression Plot")
fig, ax = plt.subplots()
ax.scatter(X, y, label="Actual Data")
ax.plot(X, y_pred, label="Regression Line")
ax.set_xlabel(x_col)
ax.set_ylabel(y_col)
ax.legend()
st.pyplot(fig)
else:
st.warning("Please select different columns for X and y.")
|