trohith89 commited on
Commit
d36d293
Β·
verified Β·
1 Parent(s): 6a90002

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
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.**")