MrDdz commited on
Commit
10a1a64
1 Parent(s): 10aa42b

app.py created

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import streamlit as st
3
+ import numpy as np
4
+ from matplotlib import pyplot as plt
5
+ import pickle
6
+ import sklearn
7
+ from PIL import Image
8
+
9
+
10
+
11
+ # Load the saved components:
12
+ with open("dt_model.pkl", "rb") as f:
13
+ components = pickle.load(f)
14
+
15
+ # Extract the individual components
16
+ num_imputer = components["num_imputer"]
17
+ cat_imputer = components["cat_imputer"]
18
+ encoder = components["encoder"]
19
+ scaler = components["scaler"]
20
+ dt_model = components["models"]
21
+
22
+ # Create the app
23
+
24
+ st.set_page_config(
25
+ layout="wide"
26
+ )
27
+
28
+
29
+ # Add an image or logo to the app
30
+ image = Image.open('copofav.jpg')
31
+
32
+ # Open the image file
33
+ st.image(image)
34
+
35
+
36
+ #add app title
37
+ st.title("SALES PREDICTION APP")
38
+
39
+
40
+ # Add some text
41
+ st.write("Please ENTER the relevant data and CLICK Predict.")
42
+
43
+ # Create the input fields
44
+ input_data = {}
45
+ col1,col2,col3 = st.columns(3)
46
+ with col1:
47
+ input_data['store_nbr'] = st.slider("Store Number",0,54)
48
+ input_data['products'] = st.selectbox("Products Family", ['OTHERS', 'CLEANING', 'FOODS', 'STATIONERY', 'GROCERY', 'HARDWARE',
49
+ 'HOME', 'CLOTHING'])
50
+ input_data['onpromotion'] =st.number_input("Discount Amt On Promotion",step=1)
51
+ input_data['state'] = st.selectbox("State", ['Pichincha', 'Cotopaxi', 'Chimborazo', 'Imbabura',
52
+ 'Santo Domingo de los Tsachilas', 'Bolivar', 'Pastaza',
53
+ 'Tungurahua', 'Guayas', 'Santa Elena', 'Los Rios', 'Azuay', 'Loja',
54
+ 'El Oro', 'Esmeraldas', 'Manabi'])
55
+ with col2:
56
+ input_data['store_type'] = st.selectbox("Store Type",['D', 'C', 'B', 'E', 'A'])
57
+ input_data['cluster'] = st.number_input("Cluster",step=1)
58
+ input_data['dcoilwtico'] = st.number_input("DCOILWTICO",step=1)
59
+ input_data['year'] = st.number_input("Year to Predict",step=1)
60
+ with col3:
61
+ input_data['month'] = st.slider("Month",1,12)
62
+ input_data['day'] = st.slider("Day",1,31)
63
+ input_data['dayofweek'] = st.number_input("Day of Week,0=Sunday and 6=Satruday",step=1)
64
+ input_data['end_month'] = st.selectbox("Is it End of the Month?",['True','False'])
65
+
66
+
67
+ # Create a button to make a prediction
68
+ if st.button("Predict"):
69
+ # Convert the input data to a pandas DataFrame
70
+ input_df = pd.DataFrame([input_data])
71
+
72
+ # categorizing the products
73
+ food_families = ['BEVERAGES', 'BREAD/BAKERY', 'FROZEN FOODS', 'MEATS', 'PREPARED FOODS', 'DELI','PRODUCE', 'DAIRY','POULTRY','EGGS','SEAFOOD']
74
+ home_families = ['HOME AND KITCHEN I', 'HOME AND KITCHEN II', 'HOME APPLIANCES']
75
+ clothing_families = ['LINGERIE', 'LADYSWARE']
76
+ grocery_families = ['GROCERY I', 'GROCERY II']
77
+ stationery_families = ['BOOKS', 'MAGAZINES','SCHOOL AND OFFICE SUPPLIES']
78
+ cleaning_families = ['HOME CARE', 'BABY CARE','PERSONAL CARE']
79
+ hardware_families = ['PLAYERS AND ELECTRONICS','HARDWARE']
80
+ others_families = ['AUTOMOTIVE', 'BEAUTY','CELEBRATION', 'LADIESWEAR', 'LAWN AND GARDEN', 'LIQUOR,WINE,BEER', 'PET SUPPLIES']
81
+
82
+
83
+
84
+
85
+ # Apply the same preprocessing steps as done during training
86
+ input_df['products'] = np.where(input_df['products'].isin(food_families), 'FOODS', input_df['products'])
87
+ input_df['products'] = np.where(input_df['products'].isin(home_families), 'HOME', input_df['products'])
88
+ input_df['products'] = np.where(input_df['products'].isin(clothing_families), 'CLOTHING', input_df['products'])
89
+ input_df['products'] = np.where(input_df['products'].isin(grocery_families), 'GROCERY', input_df['products'])
90
+ input_df['products'] = np.where(input_df['products'].isin(stationery_families), 'STATIONERY', input_df['products'])
91
+ input_df['products'] = np.where(input_df['products'].isin(cleaning_families), 'CLEANING', input_df['products'])
92
+ input_df['products'] = np.where(input_df['products'].isin(hardware_families), 'HARDWARE', input_df['products'])
93
+ input_df['products'] = np.where(input_df['products'].isin(others_families), 'OTHERS', input_df['products'])
94
+
95
+
96
+ categorical_columns = ['products', 'end_month', 'store_type', 'state']
97
+ numerical_columns =['store_nbr','onpromotion','cluster','dcoilwtico','year','month','day','dayofweek']
98
+ # Impute missing values
99
+ input_df_cat = input_df[categorical_columns].copy()
100
+ input_df_num = input_df[numerical_columns].copy()
101
+ input_df_cat_imputed = cat_imputer.transform(input_df_cat)
102
+ input_df_num_imputed = num_imputer.transform(input_df_num)
103
+
104
+ # Encode categorical features
105
+ input_df_cat_encoded = pd.DataFrame(encoder.transform(input_df_cat_imputed).toarray(),
106
+ columns=encoder.get_feature_names_out(categorical_columns))
107
+
108
+ # Scale numerical features
109
+ input_df_num_scaled = scaler.transform(input_df_num_imputed)
110
+ input_df_num_sc = pd.DataFrame(input_df_num_scaled, columns=numerical_columns)
111
+
112
+ # Combine encoded categorical features and scaled numerical features
113
+ input_df_processed = pd.concat([input_df_num_sc, input_df_cat_encoded], axis=1)
114
+
115
+ # Make predictions using the trained model
116
+ predictions = dt_model.predict(input_df_processed)
117
+
118
+ # Display the predicted sales value to the user:
119
+ st.write("Predicted Sales:", predictions[0])