MuhammadHananKhan123's picture
Update app.py
1ef5be3 verified
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import numpy as np
# Title
st.title("Graph Plotter with Linear Regression")
# File upload
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
if uploaded_file:
# Load the CSV file
data = pd.read_csv(uploaded_file)
st.write("Preview of the data:")
st.write(data.head())
# Select columns for X and Y
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:
# Prepare data for regression
X = data[[x_col]].values
Y = data[y_col].values
# Linear Regression
model = LinearRegression()
model.fit(X, Y)
slope = model.coef_[0]
intercept = model.intercept_
# Display slope and intercept
st.write(f"**Slope:** {slope}")
st.write(f"**Intercept:** {intercept}")
# Plot the graph
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)
# Show plot
st.pyplot(fig)