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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# In[1]:
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
# Create two columns
|
| 13 |
+
col1, col2 = st.columns([1, 3]) # Adjust the ratio as needed
|
| 14 |
+
|
| 15 |
+
# Load and display the logo image in the first column
|
| 16 |
+
with col1:
|
| 17 |
+
image_path = "Niq.png" # Update this path if your image is in a different directory
|
| 18 |
+
st.image(image_path, width=150) # Adjust the width as needed
|
| 19 |
+
|
| 20 |
+
# Set the title of the app in the second column
|
| 21 |
+
with col2:
|
| 22 |
+
st.title("Segmentation Tool")
|
| 23 |
+
|
| 24 |
+
st.sidebar.title("Welcome to the Dollar General Segmentation Tool!")
|
| 25 |
+
st.sidebar.info(
|
| 26 |
+
"""
|
| 27 |
+
**Please follow the instructions below to contribute to our research:**
|
| 28 |
+
|
| 29 |
+
- On the right side, you will encounter a series of statements.
|
| 30 |
+
- **Carefully read each statement** and use the dropdowns and sliders to select the option that best describes your preferences or behaviors.
|
| 31 |
+
- Your thoughtful responses are crucial for the accuracy of our segmentation model.
|
| 32 |
+
- The information you provide will be used to enhance our understanding of different customer segments.
|
| 33 |
+
|
| 34 |
+
**Thank you for participating in our research. Your input is invaluable!**
|
| 35 |
+
"""
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
st.markdown("<h2 style='color: black;'>Demographics</h2>", unsafe_allow_html=True)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# In[ ]:
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Add statement for Gender
|
| 46 |
+
st.write("**Gender**")
|
| 47 |
+
gender_display_options = ["Male", "Female", "Other", "Prefer not to disclose"]
|
| 48 |
+
gender_encoding = {"Male": 1, "Female": 2, "Other": 3, "Prefer not to disclose": 4}
|
| 49 |
+
selected_gender_display = st.selectbox("Select your gender:", gender_display_options)
|
| 50 |
+
selected_gender_encoded = gender_encoding[selected_gender_display]
|
| 51 |
+
|
| 52 |
+
# Add statement for Age
|
| 53 |
+
st.write("**Age**")
|
| 54 |
+
age_display_options = ["18-34", "35-44", "45-54", "55-64", "65 and above"]
|
| 55 |
+
age_encoding = {"18-34": 3, "35-44": 4, "45-54": 5, "55-64": 6, "65 and above": 7}
|
| 56 |
+
selected_age_display = st.selectbox("Select your age range:", age_display_options)
|
| 57 |
+
selected_age_encoded = age_encoding[selected_age_display]
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# In[ ]:
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Add a heading for Shopping Behaviour section with highlighted color
|
| 65 |
+
st.markdown("<h2 style='color: black;'>Shopping Behaviour</h2>", unsafe_allow_html=True)
|
| 66 |
+
|
| 67 |
+
# In[ ]:
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# First statement with dropdown options
|
| 71 |
+
statement1 = "Which of the following best describes how well you know the prices of the household items you buy regularly?"
|
| 72 |
+
statement1_options = [
|
| 73 |
+
"I know the prices of the household items I buy regularly and always notice when the prices change",
|
| 74 |
+
"I know the prices of some of the items I buy regularly and usually notice when the prices change",
|
| 75 |
+
"I generally know about how much I pay for things, but I don’t pay much attention to how much the products I buy cost or when prices change",
|
| 76 |
+
"Convenience is more important to me than lower prices"
|
| 77 |
+
]
|
| 78 |
+
statement1_encoding = {
|
| 79 |
+
"I know the prices of the household items I buy regularly and always notice when the prices change": 1,
|
| 80 |
+
"I know the prices of some of the items I buy regularly and usually notice when the prices change": 2,
|
| 81 |
+
"I generally know about how much I pay for things, but I don’t pay much attention to how much the products I buy cost or when prices change": 3,
|
| 82 |
+
"Convenience is more important to me than lower prices": 4
|
| 83 |
+
}
|
| 84 |
+
selected_statement1_display = st.selectbox(f"**{statement1}**", statement1_options)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# Save the encoding for the selected statement1 option
|
| 88 |
+
selected_statement1_encoded = statement1_encoding[selected_statement1_display]
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# In[ ]:
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# Second statement with dropdown options
|
| 95 |
+
statement2 = "How much did you spend when visiting any Dollar General store in the past month in total?"
|
| 96 |
+
statement2_options = ["$10 or less", "$11-$30", "$31-$70", "$71-$200", "Over $200","I have not shopped in the past month"]
|
| 97 |
+
statement2_encoding = {
|
| 98 |
+
"$10 or less": 1,
|
| 99 |
+
"$11-$30": 2,
|
| 100 |
+
"$31-$70": 3,
|
| 101 |
+
"$71-$200": 4,
|
| 102 |
+
"Over $200": 5,
|
| 103 |
+
"I have not shopped in the past month":1
|
| 104 |
+
}
|
| 105 |
+
selected_statement2_display = st.selectbox(f"**{statement2}**", statement2_options)
|
| 106 |
+
|
| 107 |
+
# Save the encoding for the selected statement2 option
|
| 108 |
+
selected_statement2_encoded = statement2_encoding[selected_statement2_display]
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# In[ ]:
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
#Third statement with dropdown options
|
| 115 |
+
statement3 = "On a typical shopping trip to Dollar General, how many items do you purchase?"
|
| 116 |
+
statement3_options = ["1-2 items", "3-4 items", "5-6 items", "7-8 items", "More than 8 items"]
|
| 117 |
+
statement3_encoding = {
|
| 118 |
+
"1-2 items": 1,
|
| 119 |
+
"3-4 items": 2,
|
| 120 |
+
"5-6 items": 3,
|
| 121 |
+
"7-8 items": 4,
|
| 122 |
+
"More than 8 items": 5
|
| 123 |
+
}
|
| 124 |
+
selected_statement3_display = st.selectbox(f"**{statement3}**", statement3_options)
|
| 125 |
+
# Save the encoding for the selected statement3 option
|
| 126 |
+
selected_statement3_encoded = statement3_encoding[selected_statement3_display]
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# In[ ]:
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
#Fourth statement with dropdown options
|
| 133 |
+
statement4 = "How often do you go shopping at any Dollar General?"
|
| 134 |
+
statement4_options = ["1-2 times a year", "3-5 times a year", "6-11 times a year", "Once a month", "2-3 times a month", "4 or more times a month"]
|
| 135 |
+
statement4_encoding = {
|
| 136 |
+
"1-2 times a year": 1,
|
| 137 |
+
"3-5 times a year": 2,
|
| 138 |
+
"6-11 times a year": 3,
|
| 139 |
+
"Once a month": 4,
|
| 140 |
+
"2-3 times a month": 5,
|
| 141 |
+
"4 or more times a month": 6
|
| 142 |
+
}
|
| 143 |
+
selected_statement4_display = st.selectbox(f"**{statement4}**", statement4_options)
|
| 144 |
+
|
| 145 |
+
# Save the encoding for the selected statement4 option
|
| 146 |
+
selected_statement4_encoded = statement4_encoding[selected_statement4_display]
|
| 147 |
+
|
| 148 |
+
# Add a heading for Shopping Habit section with highlighted color
|
| 149 |
+
st.markdown("<h2 style='color: black;'>Shopping Habit</h2>", unsafe_allow_html=True)
|
| 150 |
+
st.write("**If you were to shop for household items, how would you shop? Please select where on the scale you feel best describes you.**")
|
| 151 |
+
# Create sliders with descriptive statements
|
| 152 |
+
sliders = [
|
| 153 |
+
("I always buy well-known brands", "I don’t care much about brands"),
|
| 154 |
+
("Promotions / sales rarely change my brand choices", "I buy different brands because of promotions / sales"),
|
| 155 |
+
("Often, I am stressed while shopping", "I find shopping enjoyable"),
|
| 156 |
+
("I feel shopping is fun" , "I feel shopping is a tedious task"),
|
| 157 |
+
("I like to take my time and browse when shopping", "I don’t like spending unnecessary time when shopping"),
|
| 158 |
+
("I use apps while shopping", "I do not use apps while shopping"),
|
| 159 |
+
("I end up purchasing a lot of things that I didn’t intend to", "I am very disciplined when I shop and only get what I intended to buy"),
|
| 160 |
+
("I know prices of household items very well", "I do not pay attention to the price of household items"),
|
| 161 |
+
("I know exactly what items to buy before I get to the store", "I tend to make most of my shopping decisions when I’m in the store")
|
| 162 |
+
]
|
| 163 |
+
|
| 164 |
+
#slider_responses = {}
|
| 165 |
+
#for idx, (left_text, right_text) in enumerate(sliders):
|
| 166 |
+
# cols = st.columns([1, 2, 1]) # Define columns with the desired width ratio
|
| 167 |
+
# with cols[0]:
|
| 168 |
+
# st.write(left_text) # Right-side statement
|
| 169 |
+
# with cols[1]:
|
| 170 |
+
# slider_key = f"slider_{idx}"
|
| 171 |
+
# slider_responses[(left_text, right_text)] = st.slider(
|
| 172 |
+
# "",
|
| 173 |
+
# min_value=1,
|
| 174 |
+
# max_value=5,
|
| 175 |
+
# value=3,
|
| 176 |
+
# format="%d",
|
| 177 |
+
# key=slider_key
|
| 178 |
+
# )
|
| 179 |
+
# with cols[2]:
|
| 180 |
+
# st.write(right_text) # Left-side statement
|
| 181 |
+
|
| 182 |
+
#import streamlit as st
|
| 183 |
+
|
| 184 |
+
# Custom function to display a slider without showing its value
|
| 185 |
+
def slider_without_value(label, min_value, max_value, value, key):
|
| 186 |
+
# Create a slider and capture its value
|
| 187 |
+
selected_value = st.slider(label, min_value, max_value, value, format="", key=key)
|
| 188 |
+
# Return the selected value without displaying it
|
| 189 |
+
return selected_value
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
slider_responses = {}
|
| 194 |
+
for idx, (left_text, right_text) in enumerate(sliders):
|
| 195 |
+
cols = st.columns([1, 2, 1]) # Define columns with the desired width ratio
|
| 196 |
+
with cols[0]:
|
| 197 |
+
st.write(left_text) # Left-side statement
|
| 198 |
+
with cols[1]:
|
| 199 |
+
slider_key = f"slider_{idx}"
|
| 200 |
+
slider_responses[(left_text, right_text)] = slider_without_value(
|
| 201 |
+
"", 1, 5, 3, key=slider_key
|
| 202 |
+
)
|
| 203 |
+
with cols[2]:
|
| 204 |
+
st.write(right_text) # Right-side statement
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
# Collect responses for each statement
|
| 208 |
+
responses = {
|
| 209 |
+
"SC2": selected_gender_encoded,
|
| 210 |
+
"SC3a": selected_age_encoded,
|
| 211 |
+
"PR2a": selected_statement1_encoded,
|
| 212 |
+
"SH1": slider_responses[("I always buy well-known brands", "I don’t care much about brands")],
|
| 213 |
+
"SH2": slider_responses[("Promotions / sales rarely change my brand choices", "I buy different brands because of promotions / sales")],
|
| 214 |
+
"SH3": slider_responses[("Often, I am stressed while shopping", "I find shopping enjoyable")],
|
| 215 |
+
"SH4":slider_responses[("I feel shopping is fun" , "I feel shopping is a tedious task")],
|
| 216 |
+
"SH5": slider_responses[("I like to take my time and browse when shopping", "I don’t like spending unnecessary time when shopping")],
|
| 217 |
+
"SH6": slider_responses[("I use apps while shopping", "I do not use apps while shopping")],
|
| 218 |
+
"SH7": slider_responses[("I end up purchasing a lot of things that I didn’t intend to", "I am very disciplined when I shop and only get what I intended to buy")],
|
| 219 |
+
"SH8": slider_responses[("I know prices of household items very well", "I do not pay attention to the price of household items")],
|
| 220 |
+
"SH9": slider_responses[("I know exactly what items to buy before I get to the store", "I tend to make most of my shopping decisions when I’m in the store")],
|
| 221 |
+
"Q21": selected_statement2_encoded,
|
| 222 |
+
"Q25": selected_statement3_encoded,
|
| 223 |
+
"Q26": selected_statement4_encoded
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
# Load the saved model
|
| 229 |
+
import pickle
|
| 230 |
+
|
| 231 |
+
model_path = 'Trained_model.pickle'
|
| 232 |
+
with open(model_path, 'rb') as model_file:
|
| 233 |
+
model = pickle.load(model_file)
|
| 234 |
+
label_mapping = {
|
| 235 |
+
1: "Stacey",
|
| 236 |
+
2: "Dana",
|
| 237 |
+
3: "Marge",
|
| 238 |
+
4: "Carl",
|
| 239 |
+
5: "Ivy",
|
| 240 |
+
6: "Sue",
|
| 241 |
+
7: "Cora",
|
| 242 |
+
8: "Strangers"
|
| 243 |
+
}
|
| 244 |
+
df=pd.DataFrame([responses])
|
| 245 |
+
st.write(df)
|
| 246 |
+
# Make prediction
|
| 247 |
+
if st.button('Submit'):
|
| 248 |
+
prediction_numeric = model.predict(df)[0]
|
| 249 |
+
prediction_numeric=prediction_numeric+1
|
| 250 |
+
|
| 251 |
+
# Convert numpy array to int if it's a single value array
|
| 252 |
+
if isinstance(prediction_numeric, np.ndarray) and prediction_numeric.size == 1:
|
| 253 |
+
prediction_numeric = int(prediction_numeric)
|
| 254 |
+
predicted_label = label_mapping.get(prediction_numeric, "Unknown")
|
| 255 |
+
|
| 256 |
+
# Assuming 'predicted_label' is defined and holds the prediction result
|
| 257 |
+
|
| 258 |
+
# Create two columns
|
| 259 |
+
col1, col2 = st.columns(2)
|
| 260 |
+
|
| 261 |
+
# Use the first column to display the statement with a border
|
| 262 |
+
with col1:
|
| 263 |
+
st.markdown("<div style='border: 2px solid #f0f2f6; padding: 4px; border-radius: 5px; margin: 10px 0;'><strong>Assigned Statement:</strong></div>", unsafe_allow_html=True)
|
| 264 |
+
|
| 265 |
+
# Use the second column to display the label aligned to the right with a border
|
| 266 |
+
with col2:
|
| 267 |
+
st.markdown(f"<div style='text-align: right; padding-right: 16px; border: 2px solid #f0f2f6; padding: 4px; border-radius: 5px; margin: 10px 0;'><strong>{predicted_label}</strong></div>", unsafe_allow_html=True)
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
# Add prediction to the DataFrame
|
| 271 |
+
df['Assgined_Segment'] = predicted_label
|