File size: 1,648 Bytes
1ef5be3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)