Spaces:
Runtime error
Runtime error
Download src/streamlit_app.py from hamzacodesnow989/LinearRegression: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/hamzacodesnow989/LinearRegression/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/hamzacodesnow989/LinearRegression/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/hamzacodesnow989/LinearRegression/resolve/main/src/streamlit_app.py
1.47 kB
| 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.") | |