File size: 3,823 Bytes
5e5c214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt

# Page Configuration
st.set_page_config(page_title="Sales Forecasting App", layout="wide")

# App Header
st.markdown("<h1 style='text-align: center; color: white;'>๐Ÿ“ˆ Sales Forecasting App</h1>", unsafe_allow_html=True)

# Centered Navigation
st.markdown("<div style='display: flex; justify-content: center;'>", unsafe_allow_html=True)
tabs = ["๐Ÿ“Š Data Visualization", "๐Ÿ“ˆ Model Performance", "๐Ÿ”ฎ Prediction"]
selected_tab = st.radio("Navigation", tabs, horizontal=True, key="navigation")
st.markdown("</div>", unsafe_allow_html=True)

# Example Dataset (Replace with actual data)
data = {
    "Time": range(1, 13),  # e.g., months 1 to 12
    "Sales": [150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700],
    "OnlineAds": [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
    "SocialMedia": [5, 15, 25, 35, 45, 55, 65, 75, 85, 95, 105, 115]
}
df = pd.DataFrame(data)

if selected_tab == "๐Ÿ“Š Data Visualization":
    st.markdown("### Preview of Dataset")
    st.dataframe(df)

    st.markdown("### Data Visualization")
    fig, axes = plt.subplots(1, 3, figsize=(15, 5))

    axes[0].scatter(df["Time"], df["Sales"], color='blue', alpha=0.7)
    axes[0].set_title("Time vs Sales")
    axes[0].set_xlabel("Time")
    axes[0].set_ylabel("Sales")

    axes[1].scatter(df["OnlineAds"], df["Sales"], color='green', alpha=0.7)
    axes[1].set_title("Online Ads vs Sales")
    axes[1].set_xlabel("Online Ads")
    axes[1].set_ylabel("Sales")

    axes[2].scatter(df["SocialMedia"], df["Sales"], color='red', alpha=0.7)
    axes[2].set_title("Social Media vs Sales")
    axes[2].set_xlabel("Social Media")
    axes[2].set_ylabel("Sales")

    st.pyplot(fig)

elif selected_tab == "๐Ÿ“ˆ Model Performance":
    st.markdown("### Train Linear Regression Model")

    # Features and target
    X = df[["Time", "OnlineAds", "SocialMedia"]]  # Independent variables
    y = df["Sales"]  # Dependent variable

    # Split data into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    # Create and train the linear regression model
    model = LinearRegression()
    model.fit(X_train, y_train)

    # Predict on test set
    y_pred = model.predict(X_test)

    # Model evaluation
    mse = mean_squared_error(y_test, y_pred)
    r2 = r2_score(y_test, y_pred)

    st.write("Mean Squared Error (MSE):", mse)
    st.write("R-squared (R2):", r2)

    st.write("### Coefficients and Intercept")
    st.write("Coefficients:", model.coef_)
    st.write("Intercept:", model.intercept_)

    st.markdown("### Scatter Plot of Predictions vs Actual Values")
    fig, ax = plt.subplots(figsize=(8, 6))
    ax.scatter(y_test, y_pred, alpha=0.7, color='purple')
    ax.set_title("Predicted vs Actual Sales")
    ax.set_xlabel("Actual Sales")
    ax.set_ylabel("Predicted Sales")
    st.pyplot(fig)

elif selected_tab == "๐Ÿ”ฎ Prediction":
    st.markdown("### Predict Future Sales")

    # Input for prediction
    time_input = st.number_input("Time (e.g., month number):", min_value=1, max_value=100, step=1)
    online_ads_input = st.number_input("Online Ads Spend:", min_value=0.0, step=10.0)
    social_media_input = st.number_input("Social Media Spend:", min_value=0.0, step=10.0)

    if st.button("Predict Sales"):
        future_data = pd.DataFrame({
            "Time": [time_input],
            "OnlineAds": [online_ads_input],
            "SocialMedia": [social_media_input]
        })
        future_sales = model.predict(future_data)
        st.write("Predicted Sales:", future_sales[0])