Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
import warnings
|
| 8 |
+
from sklearn.linear_model import LogisticRegression
|
| 9 |
+
from sklearn.model_selection import train_test_split
|
| 10 |
+
from sklearn.preprocessing import LabelEncoder
|
| 11 |
+
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
|
| 12 |
+
import time
|
| 13 |
+
|
| 14 |
+
warnings.filterwarnings('ignore')
|
| 15 |
+
|
| 16 |
+
st.set_page_config(page_title="Electronics Sales Prediction", layout="wide")
|
| 17 |
+
|
| 18 |
+
st.title("π Consumer Electronics Sales Prediction App")
|
| 19 |
+
|
| 20 |
+
st.markdown("## π Upload Your Dataset")
|
| 21 |
+
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
|
| 22 |
+
|
| 23 |
+
if uploaded_file is not None:
|
| 24 |
+
with st.spinner('Loading Data...'):
|
| 25 |
+
time.sleep(1)
|
| 26 |
+
|
| 27 |
+
data = pd.read_csv(uploaded_file)
|
| 28 |
+
st.success("Data Uploaded Successfully β
")
|
| 29 |
+
|
| 30 |
+
st.subheader("π Data Preview")
|
| 31 |
+
st.write(data.head())
|
| 32 |
+
|
| 33 |
+
df = data.copy()
|
| 34 |
+
|
| 35 |
+
# Rename columns
|
| 36 |
+
df = df.rename(columns={'Order Date': 'order_date', 'Category': 'category', 'Sub-Category': 'sub_category', 'Sales': 'sales'})
|
| 37 |
+
|
| 38 |
+
st.subheader("π Data Summary")
|
| 39 |
+
st.write(df.describe())
|
| 40 |
+
|
| 41 |
+
# Data Visualization
|
| 42 |
+
st.subheader("π Sales Distribution")
|
| 43 |
+
fig, ax = plt.subplots()
|
| 44 |
+
sns.histplot(df['sales'], kde=True, color='skyblue', ax=ax)
|
| 45 |
+
st.pyplot(fig)
|
| 46 |
+
|
| 47 |
+
st.markdown("### π Encoding Categorical Variables")
|
| 48 |
+
le = LabelEncoder()
|
| 49 |
+
df['category'] = le.fit_transform(df['category'])
|
| 50 |
+
df['sub_category'] = le.fit_transform(df['sub_category'])
|
| 51 |
+
st.write("Categorical Encoding Done π―")
|
| 52 |
+
|
| 53 |
+
# Train-test split
|
| 54 |
+
X = df[['category', 'sub_category']]
|
| 55 |
+
y = df['sales']
|
| 56 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 57 |
+
|
| 58 |
+
st.markdown("### π Model Training")
|
| 59 |
+
model = LogisticRegression()
|
| 60 |
+
model.fit(X_train, y_train)
|
| 61 |
+
|
| 62 |
+
# Predictions
|
| 63 |
+
y_pred = model.predict(X_test)
|
| 64 |
+
|
| 65 |
+
# Evaluation
|
| 66 |
+
st.markdown("### π Model Evaluation")
|
| 67 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 68 |
+
st.metric(label="Model Accuracy", value=f"{accuracy:.2%}")
|
| 69 |
+
st.write("π Classification Report:")
|
| 70 |
+
st.text(classification_report(y_test, y_pred))
|
| 71 |
+
|
| 72 |
+
# Confusion Matrix
|
| 73 |
+
st.subheader("π― Confusion Matrix")
|
| 74 |
+
fig, ax = plt.subplots()
|
| 75 |
+
sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues', ax=ax)
|
| 76 |
+
st.pyplot(fig)
|
| 77 |
+
|
| 78 |
+
# Additional Feature: Interactive Plot
|
| 79 |
+
st.subheader("π Interactive Sales Analysis")
|
| 80 |
+
fig = px.scatter(df, x='category', y='sales', color='sub_category', title="Sales by Category and Sub-Category")
|
| 81 |
+
st.plotly_chart(fig)
|
| 82 |
+
|
| 83 |
+
# Sidebar Information
|
| 84 |
+
st.sidebar.title("π App Navigation")
|
| 85 |
+
st.sidebar.markdown("- Upload Dataset")
|
| 86 |
+
st.sidebar.markdown("- View Data Summary")
|
| 87 |
+
st.sidebar.markdown("- Train Model")
|
| 88 |
+
st.sidebar.markdown("- View Results")
|
| 89 |
+
|
| 90 |
+
st.sidebar.info("π§ **Ensure to preprocess your data properly for accurate results.**")
|