trohith89 commited on
Commit
36c2ddf
Β·
verified Β·
1 Parent(s): ee16b79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -67
app.py CHANGED
@@ -17,78 +17,86 @@ 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 (Check if 'Sales' exists)
36
- if 'Sales' in df.columns:
37
- df = df.rename(columns={'Order Date': 'order_date', 'Category': 'category', 'Sub-Category': 'sub_category', 'Sales': 'sales'})
38
- else:
39
- st.error("🚨 'Sales' column not found in the uploaded file. Please ensure the correct dataset is uploaded.")
40
- st.stop()
41
-
42
- st.subheader("πŸ“Š Data Summary")
43
- st.write(df.describe())
44
-
45
- # Data Visualization
46
- st.subheader("πŸ“ˆ Sales Distribution")
47
- fig, ax = plt.subplots()
48
- sns.histplot(df['sales'], kde=True, color='skyblue', ax=ax)
49
- st.pyplot(fig)
50
-
51
- st.markdown("### πŸ”„ Encoding Categorical Variables")
52
- le = LabelEncoder()
53
- df['category'] = le.fit_transform(df['category'])
54
- df['sub_category'] = le.fit_transform(df['sub_category'])
55
- st.write("Categorical Encoding Done 🎯")
56
-
57
- # Train-test split
58
- X = df[['category', 'sub_category']]
59
- y = df['sales']
60
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
61
-
62
- st.markdown("### πŸš€ Model Training")
63
- model = LogisticRegression()
64
- model.fit(X_train, y_train)
65
-
66
- # Predictions
67
- y_pred = model.predict(X_test)
68
-
69
- # Evaluation
70
- st.markdown("### πŸ“ˆ Model Evaluation")
71
- accuracy = accuracy_score(y_test, y_pred)
72
- st.metric(label="Model Accuracy", value=f"{accuracy:.2%}")
73
- st.write("πŸ” Classification Report:")
74
- st.text(classification_report(y_test, y_pred))
75
-
76
- # Confusion Matrix
77
- st.subheader("🎯 Confusion Matrix")
78
- fig, ax = plt.subplots()
79
- sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues', ax=ax)
80
- st.pyplot(fig)
81
-
82
- # Additional Feature: Interactive Plot
83
- st.subheader("πŸ“Š Interactive Sales Analysis")
84
- fig = px.scatter(df, x='category', y='sales', color='sub_category', title="Sales by Category and Sub-Category")
85
- st.plotly_chart(fig)
86
-
87
- # Sidebar Information
88
- st.sidebar.title("πŸ“‹ App Navigation")
89
- st.sidebar.markdown("- Upload Dataset")
90
- st.sidebar.markdown("- View Data Summary")
91
- st.sidebar.markdown("- Train Model")
92
- st.sidebar.markdown("- View Results")
93
-
94
- st.sidebar.info("πŸ”§ **Ensure to preprocess your data properly for accurate results.**")
 
17
 
18
  st.title("πŸ“Š Consumer Electronics Sales Prediction App")
19
 
20
+ # Load default dataset
21
+ @st.cache_data
22
+ def load_default_data():
23
+ return pd.read_csv('/mnt/data/consumer_electronics_sales_data.csv')
24
+
25
+ st.markdown("## πŸ“‚ Upload Your Dataset or Use Default")
26
  uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
27
 
28
  if uploaded_file is not None:
29
  with st.spinner('Loading Data...'):
30
  time.sleep(1)
 
31
  data = pd.read_csv(uploaded_file)
32
  st.success("Data Uploaded Successfully βœ…")
33
+ else:
34
+ st.info("πŸ“Š Using Default Dataset")
35
+ data = load_default_data()
36
+
37
+ st.subheader("πŸ‘€ Data Preview")
38
+ st.write(data.head())
39
+
40
 
41
+ df = data.copy()
42
+
43
+ # Rename columns (Check if 'Sales' exists)
44
+ if 'Sales' in df.columns:
45
+ df = df.rename(columns={'Order Date': 'order_date', 'Category': 'category', 'Sub-Category': 'sub_category', 'Sales': 'sales'})
46
+ else:
47
+ st.error("🚨 'Sales' column not found in the dataset. Please check the file format.")
48
+ st.stop()
49
+
50
+ st.subheader("πŸ“Š Data Summary")
51
+ st.write(df.describe())
52
+
53
+ # Data Visualization
54
+ st.subheader("πŸ“ˆ Sales Distribution")
55
+ fig, ax = plt.subplots()
56
+ sns.histplot(df['sales'], kde=True, color='skyblue', ax=ax)
57
+ st.pyplot(fig)
58
+
59
+ st.markdown("### πŸ”„ Encoding Categorical Variables")
60
+ le = LabelEncoder()
61
+ df['category'] = le.fit_transform(df['category'])
62
+ df['sub_category'] = le.fit_transform(df['sub_category'])
63
+ st.write("Categorical Encoding Done 🎯")
64
+
65
+ # Train-test split
66
+ X = df[['category', 'sub_category']]
67
+ y = df['sales']
68
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
69
+
70
+ st.markdown("### πŸš€ Model Training")
71
+ model = LogisticRegression()
72
+ model.fit(X_train, y_train)
73
+
74
+ # Predictions
75
+ y_pred = model.predict(X_test)
76
+
77
+ # Evaluation
78
+ st.markdown("### πŸ“ˆ Model Evaluation")
79
+ accuracy = accuracy_score(y_test, y_pred)
80
+ st.metric(label="Model Accuracy", value=f"{accuracy:.2%}")
81
+ st.write("πŸ” Classification Report:")
82
+ st.text(classification_report(y_test, y_pred))
83
+
84
+ # Confusion Matrix
85
+ st.subheader("🎯 Confusion Matrix")
86
+ fig, ax = plt.subplots()
87
+ sns.heatmap(confusion_matrix(y_test, y_pred), annot=True, fmt='d', cmap='Blues', ax=ax)
88
+ st.pyplot(fig)
89
+
90
+ # Additional Feature: Interactive Plot
91
+ st.subheader("πŸ“Š Interactive Sales Analysis")
92
+ fig = px.scatter(df, x='category', y='sales', color='sub_category', title="Sales by Category and Sub-Category")
93
+ st.plotly_chart(fig)
94
+
95
+ # Sidebar Information
96
+ st.sidebar.title("πŸ“‹ App Navigation")
97
+ st.sidebar.markdown("- Upload Dataset")
98
+ st.sidebar.markdown("- View Data Summary")
99
+ st.sidebar.markdown("- Train Model")
100
+ st.sidebar.markdown("- View Results")
101
+
102
+ st.sidebar.info("πŸ”§ **Ensure to preprocess your data properly for accurate results.**")