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.")