File size: 11,377 Bytes
49a2db6
 
 
 
39e9eaf
49a2db6
 
 
 
 
 
b0d095a
4b14127
39e9eaf
cba3792
49a2db6
 
 
 
 
bd42d8d
49a2db6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b0d9bb
d3abf86
49a2db6
 
 
b31c3e6
49a2db6
b31c3e6
 
 
49a2db6
 
 
 
 
 
 
 
 
 
3b0d9bb
f859059
49a2db6
f859059
 
 
 
 
 
 
49a2db6
 
f859059
 
 
49a2db6
 
 
 
 
 
 
 
 
 
 
 
40ca788
49a2db6
 
 
d61824b
4fb3f39
cba3792
78fb6be
9f84f69
b4dd2a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python
# coding: utf-8

# In[1]:



import streamlit as st
from PIL import Image
import pandas as pd
import numpy as np
import catboost
import random
#from streamlit_js_eval import streamlit_js_eval

# Create two columns
col1, col2 = st.columns([1, 3])  # Adjust the ratio as needed

# Load and display the logo image in the first column
with col1:
    image_path = "niq.png"  # Update this path if your image is in a different directory
    st.image(image_path, width=150)  # Adjust the width as needed

# Set the title of the app in the second column
with col2:
    st.title("Segmentation Tool")

st.sidebar.title("Welcome to the Dollar General Segmentation Tool!")
st.sidebar.info(
    """
    **Please follow the instructions below to contribute to our research:**

    - On the right side, you will encounter a series of statements.
    - **Carefully read each statement** and use the dropdowns and sliders to select the option that best describes your preferences or behaviors.
    - Your thoughtful responses are crucial for the accuracy of our segmentation model.
    - The information you provide will be used to enhance our understanding of different customer segments.

    **Thank you for participating in our research. Your input is invaluable!**
    """
)


st.markdown("<h2 style='color: black;'>Demographics</h2>", unsafe_allow_html=True)


# In[ ]:


# Add statement for Gender
st.write("**Gender**")
gender_display_options = ["Male", "Female", "Other", "Prefer not to disclose"]
gender_encoding = {"Male": 1, "Female": 2, "Other": 3, "Prefer not to disclose": 4}
selected_gender_display = st.selectbox("Select your gender:", gender_display_options)
selected_gender_encoded = gender_encoding[selected_gender_display]

# Add statement for Age
st.write("**Age**")
age_display_options = ["18-34", "35-44", "45-54", "55-64", "65 and above"]
age_encoding = {"18-34": 3, "35-44": 4, "45-54": 5, "55-64": 6, "65 and above": 7}
selected_age_display = st.selectbox("Select your age range:", age_display_options)
selected_age_encoded = age_encoding[selected_age_display]



# In[ ]:


# Add a heading for Shopping Behaviour section with highlighted color
st.markdown("<h2 style='color: black;'>Shopping Behaviour</h2>", unsafe_allow_html=True)

# In[ ]:


# First statement with dropdown options
statement1 = "Which of the following best describes how well you know the prices of the household items you buy regularly?"
statement1_options = [
    "I know the prices of the household items I buy regularly and always notice when the prices change",
    "I know the prices of some of the items I buy regularly and usually notice when the prices change",
    "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",
    "Convenience is more important to me than lower prices"
]
statement1_encoding = {
    "I know the prices of the household items I buy regularly and always notice when the prices change": 1,
    "I know the prices of some of the items I buy regularly and usually notice when the prices change": 2,
    "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,
    "Convenience is more important to me than lower prices": 4
}
selected_statement1_display = st.selectbox(f"**{statement1}**", statement1_options)


# Save the encoding for the selected statement1 option
selected_statement1_encoded = statement1_encoding[selected_statement1_display]


# In[ ]:


# Second statement with dropdown options
statement2 = "How much did you spend when visiting any Dollar General store in the past month in total?"
statement2_options = ["$10 or less", "$11-$30", "$31-$70", "$71-$200", "Over $200","I have not shopped in the past month"]
statement2_encoding = {
    "$10 or less": 1,
    "$11-$30": 2,
    "$31-$70": 3,
    "$71-$200": 4,
    "Over $200": 5,
    "I have not shopped in the past month":1
}
selected_statement2_display = st.selectbox(f"**{statement2}**", statement2_options)

# Save the encoding for the selected statement2 option
selected_statement2_encoded = statement2_encoding[selected_statement2_display]


# In[ ]:


#Third statement with dropdown options
statement3 = "On a typical shopping trip to Dollar General, how many items do you purchase?"
statement3_options = ["1-2 items", "3-4 items", "5-6 items", "7-8 items", "More than 8 items"]
statement3_encoding = {
   "1-2 items": 1,
   "3-4 items": 2,
   "5-6 items": 3,
   "7-8 items": 4,
   "More than 8 items": 5
}
selected_statement3_display = st.selectbox(f"**{statement3}**", statement3_options)
# Save the encoding for the selected statement3 option
selected_statement3_encoded = statement3_encoding[selected_statement3_display]


# In[ ]:


#Fourth statement with dropdown options
statement4 = "How often do you go shopping at any Dollar General?"
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"]
statement4_encoding = {
   "1-2 times a year": 1,
   "3-5 times a year": 2,
   "6-11 times a year": 3,
   "Once a month": 4,
   "2-3 times a month": 5,
   "4 or more times a month": 6
}
selected_statement4_display = st.selectbox(f"**{statement4}**", statement4_options)

# Save the encoding for the selected statement4 option
selected_statement4_encoded = statement4_encoding[selected_statement4_display]

# Add a heading for Shopping Habit section with highlighted color
st.markdown("<h2 style='color: black;'>Shopping Habit</h2>", unsafe_allow_html=True)
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.**")
# Create sliders with descriptive statements
sliders = [
    ("I always buy well-known brands", "I don’t care much about brands"),
    ("Promotions / sales rarely change my brand choices", "I buy different brands because of promotions / sales"),
    ("Often, I am stressed while shopping", "I find shopping enjoyable"),
    ("I feel shopping is fun" , "I feel shopping is a tedious task"),
    ("I like to take my time and browse when shopping", "I don’t like spending unnecessary time when shopping"),
    ("I use apps while shopping", "I do not use apps while shopping"),
    ("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"),
    ("I know prices of household items very well", "I do not pay attention to the price of household items"),
    ("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")
]

#slider_responses = {}
#for idx, (left_text, right_text) in enumerate(sliders):
#    cols = st.columns([1, 2, 1])  # Define columns with the desired width ratio
#    with cols[0]:
#        st.write(left_text)  # Right-side statement
#    with cols[1]:
#        slider_key = f"slider_{idx}" 
#        slider_responses[(left_text, right_text)] = st.slider(
#            "",
#            min_value=1,
#            max_value=5,
#            value=3,
#            format="%d",
#            key=slider_key 
#        )
#    with cols[2]:
#        st.write(right_text)  # Left-side statement
        
#import streamlit as st

# Custom function to display a slider without showing its value
def slider_without_value(label, min_value, max_value, value, key):
    # Create a slider and capture its value
    selected_value = st.slider(label, min_value, max_value, value, format="", key=key)
    # Return the selected value without displaying it
    return selected_value



slider_responses = {}
for idx, (left_text, right_text) in enumerate(sliders):
    cols = st.columns([1, 2, 1])  # Define columns with the desired width ratio
    with cols[0]:
        st.write(left_text)  # Left-side statement
    with cols[1]:
        slider_key = f"slider_{idx}"
        slider_responses[(left_text, right_text)] = slider_without_value(
            "", 1, 5, 3, key=slider_key
        )
    with cols[2]:
        st.write(right_text)  # Right-side statement


# Collect responses for each statement
responses = {
    "SC2": selected_gender_encoded,
    "SC3a": selected_age_encoded,
    "PR2a": selected_statement1_encoded,
    "SH1": slider_responses[("I always buy well-known brands", "I don’t care much about brands")],
    "SH2": slider_responses[("Promotions / sales rarely change my brand choices", "I buy different brands because of promotions / sales")],
    "SH3": slider_responses[("Often, I am stressed while shopping", "I find shopping enjoyable")],
    "SH4":slider_responses[("I feel shopping is fun" , "I feel shopping is a tedious task")],
    "SH5": slider_responses[("I like to take my time and browse when shopping", "I don’t like spending unnecessary time when shopping")],
    "SH6": slider_responses[("I use apps while shopping", "I do not use apps while shopping")],
    "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")],
    "SH8": slider_responses[("I know prices of household items very well", "I do not pay attention to the price of household items")],
    "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")],
        "Q21": selected_statement2_encoded,
    "Q25": selected_statement3_encoded,
    "Q26": selected_statement4_encoded
}
df=pd.DataFrame([responses])
#st.write(df)


# Load the saved model
#import pickle

#model_path = 'Trained_model.pickle'
#with open(model_path, 'rb') as model_file:
#    model = pickle.load(model_file)
label_mapping = {
    1: "Stacey",
    2: "Dana",
    3: "Marge",
    4: "Carl",
    5: "Ivy",
    6: "Sue",
    7: "Cora",
    8: "Strangers"
}

# Make prediction for demo purposes
if st.button('Submit'):
    # Choose a random key from label_mapping
    random_key = random.choice(list(label_mapping.keys()))
    random_label = label_mapping[random_key]

#if st.button('Submit'):
#    prediction_numeric = model.predict(df)[0]
#    prediction_numeric=prediction_numeric+1

    # Convert numpy array to int if it's a single value array
#    if isinstance(prediction_numeric, np.ndarray) and prediction_numeric.size == 1:
 #       prediction_numeric = int(prediction_numeric)
 #   predicted_label = label_mapping.get(prediction_numeric, "Unknown")

    # Assuming 'predicted_label' is defined and holds the prediction result

    # Create two columns
    col1, col2 = st.columns(2)

    # Use the first column to display the statement with a border
    with col1:
        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)

    # Use the second column to display the label aligned to the right with a border
    with col2:
        st.markdown(f"<div style='text-align: right; padding-right: 16px; border: 2px solid #f0f2f6; padding: 4px; border-radius: 5px; margin: 10px 0;'><strong>{random_label}</strong></div>", unsafe_allow_html=True)


    # Add prediction to the DataFrame
    #df['Assgined_Segment'] = predicted_label