Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
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.**")
|
|
|
|
|
|
|
|
|