MLDeveloper commited on
Commit
1840ab8
·
verified ·
1 Parent(s): 8950d93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -29
app.py CHANGED
@@ -1,49 +1,71 @@
1
  import streamlit as st
 
2
  import numpy as np
3
  from sklearn.linear_model import LinearRegression
 
 
4
 
5
- # Streamlit page config
6
  st.set_page_config(page_title="BigMart Sales Predictor", page_icon="🛒", layout="centered")
 
 
7
 
8
- st.title("🛒 BigMart Sales Prediction")
9
- st.markdown("Enter item details below to predict sales:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Input fields
 
 
 
 
12
  product_name = st.text_input("📦 Product Name")
13
- item_weight = st.number_input("⚖️ Item Weight (in kg)", min_value=0.0, step=0.1)
14
  item_visibility = st.slider("👀 Item Visibility", 0.0, 1.0, 0.05)
15
  item_mrp = st.number_input("💰 Item MRP", min_value=0.0, step=1.0)
16
 
17
- # Predict button
18
  if st.button("Predict Sales"):
19
  if not product_name:
20
  st.warning("Please enter a product name.")
21
  else:
22
- # Dummy training data for demo
23
- X_train = np.array([
24
- [9.3, 0.016, 249.8],
25
- [5.92, 0.019, 48.27],
26
- [17.5, 0.016, 141.62],
27
- [19.2, 0.0075, 182.095],
28
- ])
29
- y_train = np.array([3735.14, 443.42, 2233.6, 3612.47]) # Target: Item_Outlet_Sales
30
-
31
- # Train model
32
- model = LinearRegression()
33
- model.fit(X_train, y_train)
34
-
35
- # Prepare user input
36
  user_input = np.array([[item_weight, item_visibility, item_mrp]])
37
  predicted_sales = model.predict(user_input)[0]
38
-
39
  st.success(f"📈 Predicted Sales for '{product_name}': ₹{predicted_sales:,.2f}")
 
 
 
 
 
 
 
 
 
40
 
41
- # Sidebar
 
 
42
  st.sidebar.title("📌 About")
43
- st.sidebar.markdown(
44
- """
45
- This app predicts sales based on item weight, visibility, and MRP using a demo ML model.
46
-
47
- 🔧 Replace with a trained model on BigMart dataset for real-world use!
48
- """
49
- )
 
1
  import streamlit as st
2
+ import pandas as pd
3
  import numpy as np
4
  from sklearn.linear_model import LinearRegression
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.preprocessing import LabelEncoder
7
 
8
+ # Streamlit UI
9
  st.set_page_config(page_title="BigMart Sales Predictor", page_icon="🛒", layout="centered")
10
+ st.title("🛒 BigMart Sales Prediction using Real Dataset")
11
+ st.markdown("Fill in the product details to get a sales prediction.")
12
 
13
+ # Load and preprocess dataset
14
+ @st.cache_data
15
+ def load_data():
16
+ data = pd.read_csv("Train.csv") # 👈 Make sure Train.csv is in the same directory
17
+ # Handle missing values
18
+ data.fillna(data.mean(numeric_only=True), inplace=True)
19
+ data.fillna("Unknown", inplace=True)
20
+
21
+ # Encode categorical columns
22
+ label_enc = LabelEncoder()
23
+ for col in ['Item_Fat_Content', 'Item_Type', 'Outlet_Identifier', 'Outlet_Size', 'Outlet_Location_Type', 'Outlet_Type']:
24
+ data[col] = label_enc.fit_transform(data[col])
25
+ return data
26
+
27
+ df = load_data()
28
+
29
+ # Select features and target
30
+ features = ['Item_Weight', 'Item_Visibility', 'Item_MRP']
31
+ target = 'Item_Outlet_Sales'
32
+
33
+ X = df[features]
34
+ y = df[target]
35
 
36
+ # Train model
37
+ model = LinearRegression()
38
+ model.fit(X, y)
39
+
40
+ # Input UI
41
  product_name = st.text_input("📦 Product Name")
42
+ item_weight = st.number_input("⚖️ Item Weight (kg)", min_value=0.0, step=0.1)
43
  item_visibility = st.slider("👀 Item Visibility", 0.0, 1.0, 0.05)
44
  item_mrp = st.number_input("💰 Item MRP", min_value=0.0, step=1.0)
45
 
46
+ # Prediction
47
  if st.button("Predict Sales"):
48
  if not product_name:
49
  st.warning("Please enter a product name.")
50
  else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  user_input = np.array([[item_weight, item_visibility, item_mrp]])
52
  predicted_sales = model.predict(user_input)[0]
 
53
  st.success(f"📈 Predicted Sales for '{product_name}': ₹{predicted_sales:,.2f}")
54
+
55
+ # Optional: Download Prediction
56
+ result_df = pd.DataFrame({
57
+ "Product Name": [product_name],
58
+ "Item Weight": [item_weight],
59
+ "Item Visibility": [item_visibility],
60
+ "Item MRP": [item_mrp],
61
+ "Predicted Sales": [predicted_sales]
62
+ })
63
 
64
+ st.download_button("📥 Download Result as CSV", result_df.to_csv(index=False), file_name="prediction.csv", mime="text/csv")
65
+
66
+ # Sidebar Info
67
  st.sidebar.title("📌 About")
68
+ st.sidebar.markdown("""
69
+ This app uses a **real BigMart dataset** from Kaggle and a **Linear Regression model** to predict sales.
70
+ You can customize features or switch to advanced ML models later!
71
+ """)