|
import streamlit as st |
|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
from sklearn.linear_model import LinearRegression |
|
import numpy as np |
|
|
|
|
|
st.title("Graph Plotter with Linear Regression") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"]) |
|
|
|
if uploaded_file: |
|
|
|
data = pd.read_csv(uploaded_file) |
|
st.write("Preview of the data:") |
|
st.write(data.head()) |
|
|
|
|
|
numeric_columns = data.select_dtypes(include=['float64', 'int64']).columns |
|
if len(numeric_columns) < 2: |
|
st.error("The dataset must have at least two numerical columns.") |
|
else: |
|
x_col = st.selectbox("Select the X column", numeric_columns) |
|
y_col = st.selectbox("Select the Y column", numeric_columns) |
|
|
|
if x_col and y_col: |
|
|
|
X = data[[x_col]].values |
|
Y = data[y_col].values |
|
|
|
|
|
model = LinearRegression() |
|
model.fit(X, Y) |
|
slope = model.coef_[0] |
|
intercept = model.intercept_ |
|
|
|
|
|
st.write(f"**Slope:** {slope}") |
|
st.write(f"**Intercept:** {intercept}") |
|
|
|
|
|
fig, ax = plt.subplots() |
|
ax.scatter(X, Y, label="Data points", color="blue") |
|
ax.plot(X, model.predict(X), color="red", label="Regression line") |
|
ax.set_xlabel(x_col) |
|
ax.set_ylabel(y_col) |
|
ax.legend() |
|
ax.grid(True) |
|
|
|
|
|
st.pyplot(fig) |
|
|