Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from sklearn.linear_model import LinearRegression
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Title
|
| 8 |
+
st.title("Graph Plotter with Linear Regression")
|
| 9 |
+
|
| 10 |
+
# File upload
|
| 11 |
+
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
|
| 12 |
+
|
| 13 |
+
if uploaded_file:
|
| 14 |
+
# Load the CSV file
|
| 15 |
+
data = pd.read_csv(uploaded_file)
|
| 16 |
+
st.write("Preview of the data:")
|
| 17 |
+
st.write(data.head())
|
| 18 |
+
|
| 19 |
+
# Select columns for X and Y
|
| 20 |
+
numeric_columns = data.select_dtypes(include=['float64', 'int64']).columns
|
| 21 |
+
if len(numeric_columns) < 2:
|
| 22 |
+
st.error("The dataset must have at least two numerical columns.")
|
| 23 |
+
else:
|
| 24 |
+
x_col = st.selectbox("Select the X column", numeric_columns)
|
| 25 |
+
y_col = st.selectbox("Select the Y column", numeric_columns)
|
| 26 |
+
|
| 27 |
+
if x_col and y_col:
|
| 28 |
+
# Prepare data for regression
|
| 29 |
+
X = data[[x_col]].values
|
| 30 |
+
Y = data[y_col].values
|
| 31 |
+
|
| 32 |
+
# Linear Regression
|
| 33 |
+
model = LinearRegression()
|
| 34 |
+
model.fit(X, Y)
|
| 35 |
+
slope = model.coef_[0]
|
| 36 |
+
intercept = model.intercept_
|
| 37 |
+
|
| 38 |
+
# Display slope and intercept
|
| 39 |
+
st.write(f"**Slope:** {slope}")
|
| 40 |
+
st.write(f"**Intercept:** {intercept}")
|
| 41 |
+
|
| 42 |
+
# Plot the graph
|
| 43 |
+
fig, ax = plt.subplots()
|
| 44 |
+
ax.scatter(X, Y, label="Data points", color="blue")
|
| 45 |
+
ax.plot(X, model.predict(X), color="red", label="Regression line")
|
| 46 |
+
ax.set_xlabel(x_col)
|
| 47 |
+
ax.set_ylabel(y_col)
|
| 48 |
+
ax.legend()
|
| 49 |
+
ax.grid(True)
|
| 50 |
+
|
| 51 |
+
# Show plot
|
| 52 |
+
st.pyplot(fig)
|
| 53 |
+
|