Update pages/3_EDA_and_Feature_Engineering.py
Browse files- pages/3_EDA_and_Feature_Engineering.py +340 -340
pages/3_EDA_and_Feature_Engineering.py
CHANGED
|
@@ -1,352 +1,352 @@
|
|
| 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 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
|
| 317 |
-
#
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
#
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
#
|
| 324 |
-
|
| 325 |
-
|
| 326 |
|
| 327 |
-
#
|
| 328 |
-
|
| 329 |
|
| 330 |
-
#
|
| 331 |
-
|
| 332 |
|
| 333 |
-
|
| 334 |
-
#
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
|
| 341 |
-
|
| 342 |
|
| 343 |
-
|
| 344 |
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
|
| 352 |
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import seaborn as sns
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import matplotlib.patches as mpatches
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
from sklearn.preprocessing import LabelEncoder
|
| 8 |
|
| 9 |
+
st.title("Exploratory Data Analysis (EDA)")
|
| 10 |
|
| 11 |
+
if 'df' in st.session_state:
|
| 12 |
+
df = st.session_state['df'] # Retrieve dataset
|
| 13 |
+
st.write("### Dataset Preview")
|
| 14 |
+
st.dataframe(df)
|
| 15 |
|
| 16 |
+
st.write("### Summary Statistics")
|
| 17 |
+
st.write(df.describe())
|
| 18 |
+
# rename columns
|
| 19 |
+
df = df.rename(columns={'ProductCategory': 'Category', 'ProductBrand': 'Brand', 'ProductPrice': 'Price'})
|
| 20 |
+
# Correct Price Column values (round off to 2 decimal places)
|
| 21 |
+
df['Price'] = df['Price'].apply(lambda x: round(x,2))
|
| 22 |
+
print(df.head())
|
| 23 |
+
st.markdown("Complete EDA + Feature Engineering")
|
| 24 |
+
st.markdown("## **Univariate Analysis**")
|
| 25 |
+
st.markdown("### PRODUCT CATEGORY")
|
| 26 |
+
plt.figure(figsize=(10, 6))
|
| 27 |
+
sns.countplot(x='Category', data=df, palette='viridis')
|
| 28 |
+
plt.title("Product Category Distribution")
|
| 29 |
+
plt.xlabel("Product Category")
|
| 30 |
+
plt.ylabel("Count")
|
| 31 |
+
plt.xticks(rotation=45)
|
| 32 |
+
plt.show()
|
| 33 |
+
st.markdown('''**Insights :**
|
| 34 |
+
- We've 5 Product Categories:
|
| 35 |
+
1. Smart Phones & Laptops are the most highest and similar in frequency,
|
| 36 |
+
2. Followed by Smart Watches,
|
| 37 |
+
3. Tablets and Headphones are little less in frequency overall.''')
|
| 38 |
+
|
| 39 |
+
st.markdown("### BRANDS")
|
| 40 |
+
plt.figure(figsize=(10, 6))
|
| 41 |
+
sns.countplot(x='Brand', data=df, palette='cubehelix')
|
| 42 |
+
plt.title("Product Brand Distribution")
|
| 43 |
+
plt.xlabel("Product Brand")
|
| 44 |
+
plt.ylabel("Count")
|
| 45 |
+
plt.xticks(rotation=45)
|
| 46 |
+
plt.show()
|
| 47 |
+
st.markdown('''**Insights :**
|
| 48 |
+
- We've 5 Brand Categories:
|
| 49 |
+
1. Samsung & HP are the most highest and similar in frequency,
|
| 50 |
+
2. Followed by Sony, Apple, and other brands.''')
|
| 51 |
+
|
| 52 |
+
st.markdown("### PRICE DISTRIBUTION")
|
| 53 |
+
plt.figure(figsize=(10, 6))
|
| 54 |
+
sns.histplot(df['Price'], kde=True, color='orange') # 'orange' mimics Agsunset
|
| 55 |
+
plt.title("Product Price Distribution")
|
| 56 |
+
plt.xlabel("Product Price")
|
| 57 |
+
plt.ylabel("Count")
|
| 58 |
+
plt.show()
|
| 59 |
+
st.markdown('''**Insights :**
|
| 60 |
+
- **Wide Range:** The products span a considerable price range (from near 0 to 3000).
|
| 61 |
+
- **Concentration:** There's a noticeable concentration of products priced between roughly 200 and 2500.
|
| 62 |
+
- **Roughly Uniform:** The distribution appears somewhat uniform with some peaks and valleys. This indicates there isn't a single dominant price point, and products are fairly evenly distributed across the price range (with some exceptions).''')
|
| 63 |
+
|
| 64 |
+
st.markdown("### PRODUCT PRICE BINING")
|
| 65 |
+
df['ProductPriceBucket'] = pd.cut(df['Price'], bins=[100, 500, 1000, 1500, 2000, 3000], labels=['Very Low', 'Low', 'Medium', 'High', 'Very High'])
|
| 66 |
+
plt.figure(figsize=(10, 6))
|
| 67 |
+
sns.countplot(x='ProductPriceBucket', data=df, palette='icefire')
|
| 68 |
+
plt.title("Product Price Distribution")
|
| 69 |
+
plt.xlabel("Product Price Bucket")
|
| 70 |
+
plt.ylabel("Count")
|
| 71 |
+
plt.xticks(rotation=45)
|
| 72 |
+
plt.show()
|
| 73 |
+
st.markdown('''**Insights :**
|
| 74 |
+
- **Uneven Distribution:** The most striking observation is that the distribution is not even across the price buckets. This suggests that certain price ranges are more common or more popular than others.
|
| 75 |
+
- **"Very High" Dominance:** The "Very High" price bucket has the highest concentration of products. This indicates that a significant portion of your products fall into this top-tier price range. This could mean you have a focus on premium items, or it might reflect a pricing strategy that emphasizes higher-priced goods.
|
| 76 |
+
- **Lower Representation in "Very Low":** The "Very Low" bucket has the fewest products. This could indicate a limited number of budget or entry-level items in your product catalog.
|
| 77 |
+
- **Similar Counts in "Low", "Medium", and "High":** The counts for "Low", "Medium", and "High" appear relatively similar. This suggests a moderate and somewhat consistent distribution of products across these mid-range price points.''')
|
| 78 |
+
|
| 79 |
+
st.markdown("### AGE DISTRIBUTION")
|
| 80 |
+
st.markdown("### AGE BINNING")
|
| 81 |
+
df['CustomerAgeGroup'] = pd.qcut(df['CustomerAge'], q=4, labels=['Young', 'Middle-aged', 'Mature', 'Senior'])
|
| 82 |
+
fig, axs = plt.subplots(1, 2, figsize=(15, 6))
|
| 83 |
+
|
| 84 |
+
# First plot with a new palette
|
| 85 |
+
sns.countplot(x='CustomerAgeGroup', data=df, ax=axs[0], palette='magma') # 'magma' palette for a different feel
|
| 86 |
+
axs[0].set_title("Customer Age Group Distribution")
|
| 87 |
+
axs[0].set_xlabel("Customer Age Group")
|
| 88 |
+
|
| 89 |
+
# Second plot with a new color
|
| 90 |
+
sns.histplot(df['CustomerAge'], kde=True, ax=axs[1], color='teal') # 'teal' for a distinct color in the second plot
|
| 91 |
+
axs[1].set_title("Customer Age Distribution")
|
| 92 |
+
axs[1].set_xlabel("Customer Age")
|
| 93 |
+
|
| 94 |
+
# Adjust layout for better spacing
|
| 95 |
+
plt.tight_layout()
|
| 96 |
+
plt.show()
|
| 97 |
+
st.markdown('''**Insights :**
|
| 98 |
+
- **Relatively Even Distribution:** The most prominent feature is the relatively even distribution of customers across all four age groups. The bars are of similar height, indicating that each age group represents a comparable portion of the customer base.
|
| 99 |
+
- **Slight Variation:** While generally even, there are slight variations:
|
| 100 |
+
- **Young:** Appears to have a marginally higher count than the others.
|
| 101 |
+
- **Senior:** Has a slightly lower count compared to "Young" but is very close to "Mature" and "Middle-aged."
|
| 102 |
+
- **No Dominant Age Group:** There's no single dominant age group that significantly outweighs the others. This suggests a broad appeal across different age demographics.''')
|
| 103 |
+
|
| 104 |
+
st.markdown("### GENDER DISTRIBUTION")
|
| 105 |
+
print(df['CustomerGender'].value_counts())
|
| 106 |
+
plt.figure(figsize=(10, 8))
|
| 107 |
+
df['CustomerGender'].value_counts().plot(kind='pie',
|
| 108 |
+
colors=['lightblue', 'lightpink'],
|
| 109 |
+
autopct='%1.1f%%', # Shows percentage on pie chart
|
| 110 |
+
startangle=90, # Start the pie chart at a specific angle
|
| 111 |
+
wedgeprops={'edgecolor': 'black'}) # Adds borders to the slices
|
| 112 |
+
|
| 113 |
+
plt.title("Customer Gender Distribution")
|
| 114 |
+
plt.legend(labels=['Female', 'Male'], loc='upper left', fontsize=12, title="Customer Gender")
|
| 115 |
+
|
| 116 |
+
plt.show()
|
| 117 |
+
st.markdown('''**Insights :** Almost same proportion for both the genders
|
| 118 |
+
- Male (49.1%)
|
| 119 |
+
- Female (50.9%)''')
|
| 120 |
+
|
| 121 |
+
st.markdown("### PURCHASE FREQUENCY DISTRIBUTION")
|
| 122 |
+
plt.figure(figsize=(10, 6))
|
| 123 |
+
sns.histplot(df['PurchaseFrequency'], kde=True, color='purple', bins=30)
|
| 124 |
+
plt.title("Purchase Frequency Distribution")
|
| 125 |
+
plt.xlabel("Purchase Frequency")
|
| 126 |
+
plt.ylabel("Count")
|
| 127 |
+
plt.show()
|
| 128 |
+
st.markdown('''**Insights :**
|
| 129 |
+
- The Range is 1 - 19''')
|
| 130 |
+
|
| 131 |
+
st.markdown("### CUSTOMER SATISFACTION DISTRIBUTION")
|
| 132 |
+
plt.figure(figsize=(10, 6))
|
| 133 |
+
sns.histplot(df['CustomerSatisfaction'], kde=True, color=sns.color_palette("crest", n_colors=1)[0]) # Use the first color from the palette
|
| 134 |
+
plt.title("Customer Satisfaction Distribution")
|
| 135 |
+
plt.xlabel("Customer Satisfaction")
|
| 136 |
+
plt.ylabel("Count")
|
| 137 |
+
plt.show()
|
| 138 |
+
st.markdown('''**Insights :**
|
| 139 |
+
- **Multimodal Distribution:** The most striking aspect is the multimodal nature of the distribution. There are distinct peaks around the integer values (1, 2, 3, 4, 5). This suggests that customers tend to provide whole-number ratings rather than choosing intermediate values.
|
| 140 |
+
- **Relatively Uniform Peaks:** The peaks seem relatively uniform in height, indicating a somewhat even distribution of satisfaction levels across the rating scale. This might imply that there isn't a strong concentration of extremely satisfied or dissatisfied customers.''')
|
| 141 |
+
|
| 142 |
+
st.markdown("### CUSTOMER SATISFACTION DISTRIBUTION")
|
| 143 |
+
purchase_intent_counts = df['PurchaseIntent'].value_counts()
|
| 144 |
+
plt.figure(figsize=(8, 6))
|
| 145 |
+
wedges, texts, autotexts = plt.pie(purchase_intent_counts,
|
| 146 |
+
labels=purchase_intent_counts.index,
|
| 147 |
+
colors=sns.color_palette("coolwarm", n_colors=len(purchase_intent_counts)),
|
| 148 |
+
autopct='%1.1f%%',
|
| 149 |
+
startangle=90,
|
| 150 |
+
wedgeprops={'edgecolor': 'black'})
|
| 151 |
+
plt.legend(wedges, purchase_intent_counts.index, title="Purchase Intent", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1))
|
| 152 |
+
plt.title("Purchase Intent Distribution")
|
| 153 |
+
plt.show()
|
| 154 |
+
|
| 155 |
+
st.markdown('''**Insights :**
|
| 156 |
+
\n- We've 0 and 1 which means Not Purchase and Purchase.
|
| 157 |
+
|
| 158 |
+
\n- A binary classification problem.
|
| 159 |
+
|
| 160 |
+
\n- 0: Not Purchase --> 43.4%
|
| 161 |
+
|
| 162 |
+
\n- 1: Purchase --> 56.6%''')
|
| 163 |
+
|
| 164 |
+
st.markdown("## **Bivariate and MultivariateAnalysis**")
|
| 165 |
+
st.write(df.describe())
|
| 166 |
+
print(df.info())
|
| 167 |
+
|
| 168 |
+
import seaborn as sns
|
| 169 |
+
import matplotlib.pyplot as plt
|
| 170 |
+
import matplotlib.patches as mpatches
|
| 171 |
+
|
| 172 |
+
# Exclude the specific columns for histogram plotting ('ProductID', 'age_bins', 'ProductPriceBucket', 'PurchaseFrequency', 'CustomerAge')
|
| 173 |
+
columns_to_exclude = ['ProductID', 'age_bins', 'ProductPriceBucket', 'PurchaseFrequency', 'CustomerAge', 'PurchaseIntent']
|
| 174 |
+
df_filtered = df.drop(columns=columns_to_exclude)
|
| 175 |
+
|
| 176 |
+
# Set up the subplots grid: 1 row and 3 columns
|
| 177 |
+
fig, axs = plt.subplots(1, 3, figsize=(18, 6))
|
| 178 |
+
axs = axs.flatten() # Flatten the 2D array of axes to easily index
|
| 179 |
+
|
| 180 |
+
# Color palettes to cycle through for each subplot
|
| 181 |
+
color_palettes = ['Blues', 'viridis', 'coolwarm']
|
| 182 |
+
|
| 183 |
+
# Loop through the first 3 columns and plot each histogram
|
| 184 |
+
for i, col in enumerate(df_filtered.columns[:3]): # First 3 columns
|
| 185 |
+
axs[i].set_title(f"{col} Distribution")
|
| 186 |
+
axs[i].set_xlabel(col)
|
| 187 |
+
axs[i].set_ylabel("Count")
|
| 188 |
+
|
| 189 |
+
# Create histogram with 'PurchaseIntent' as the hue for color-coding
|
| 190 |
+
sns.histplot(data=df, x=col, hue='PurchaseIntent', multiple="stack",
|
| 191 |
+
palette=color_palettes[i % len(color_palettes)], bins=20, ax=axs[i])
|
| 192 |
+
|
| 193 |
+
# Manually create the custom legend with labels
|
| 194 |
+
handles = [mpatches.Patch(color=sns.color_palette(color_palettes[i % len(color_palettes)])[0], label="PurchaseIntent = 0"),
|
| 195 |
+
mpatches.Patch(color=sns.color_palette(color_palettes[i % len(color_palettes)])[1], label="PurchaseIntent = 1")]
|
| 196 |
+
axs[i].legend(handles=handles, title="Purchase Intent", loc='upper right')
|
| 197 |
+
|
| 198 |
+
# Adjust layout to prevent overlap
|
| 199 |
+
plt.tight_layout()
|
| 200 |
+
plt.show()
|
| 201 |
+
st.markdown('''**Insights :**\n\n- **Category Distribution** : The distribution of products across categories (Smartphones, Smart Watches, Tablets, Laptops, Headphones) is relatively uniform, with slight variations. This suggests a diverse product catalog.
|
| 202 |
+
Purchase Intent: It appears that "Purchase Intent = 1" (meaning intent to purchase is present) is fairly consistent across categories, with no category showing a significantly higher or lower proportion of purchase intent.\n- **Brand Distribution** :The distribution of brands is less uniform. "Other Brands" seems to have the highest representation, followed by Samsung, Sony, HP, and then Apple.
|
| 203 |
+
Purchase Intent: Observe if there are any notable differences in the proportion of "Purchase Intent = 1" between different brands. This could indicate if certain brands are more desirable or effective at converting interest into purchases.
|
| 204 |
+
\n- **Price Distribution**: The price histogram indicates a wide range of product prices, likely spanning from near 0 to 3000 (assuming the x-axis represents price).
|
| 205 |
+
Purchase Intent: Examine how purchase intent varies across different price points. Are there price ranges where purchase intent is higher or lower? This could reveal price sensitivity or the effectiveness of pricing strategies.''')
|
| 206 |
+
fig, axs = plt.subplots(1, 3, figsize=(18, 6))
|
| 207 |
+
axs = axs.flatten() # Flatten the 2D array of axes to easily index
|
| 208 |
+
|
| 209 |
+
# Color palettes to cycle through for each subplot
|
| 210 |
+
color_palettes = ['magma', 'cividis', 'inferno']
|
| 211 |
+
|
| 212 |
+
# Loop through the next 3 columns and plot each histogram
|
| 213 |
+
for i, col in enumerate(df_filtered.columns[3:6]): # Next 3 columns
|
| 214 |
+
axs[i].set_title(f"{col} Distribution")
|
| 215 |
+
axs[i].set_xlabel(col)
|
| 216 |
+
axs[i].set_ylabel("Count")
|
| 217 |
+
|
| 218 |
+
# Create histogram with 'PurchaseIntent' as the hue for color-coding
|
| 219 |
+
sns.histplot(data=df, x=col, hue='PurchaseIntent', multiple="stack",
|
| 220 |
+
palette=color_palettes[i % len(color_palettes)], bins=20, ax=axs[i])
|
| 221 |
+
|
| 222 |
+
# Manually create the custom legend with labels
|
| 223 |
+
handles = [mpatches.Patch(color=sns.color_palette(color_palettes[i % len(color_palettes)])[0], label="PurchaseIntent = 0"),
|
| 224 |
+
mpatches.Patch(color=sns.color_palette(color_palettes[i % len(color_palettes)])[1], label="PurchaseIntent = 1")]
|
| 225 |
+
axs[i].legend(handles=handles, title="Purchase Intent", loc='upper right')
|
| 226 |
+
|
| 227 |
+
# Adjust layout to prevent overlap
|
| 228 |
+
plt.tight_layout() # Correct method name here
|
| 229 |
+
plt.show()
|
| 230 |
+
st.markdown('''**Insights :**
|
| 231 |
+
|
| 232 |
+
\n- **Uneven Distribution:** There's a significant difference in the number of customers in each gender category. The category represented by '1' (likely female) has a much higher count than the category represented by '0' (likely male). This indicates that your customer base is skewed towards one gender.
|
| 233 |
+
- **Purchase Intent:** The proportion of "Purchase Intent = 1" (meaning the intent to purchase is present) appears to be relatively similar between the two genders. The purple bars (Purchase Intent = 1) are proportionally similar in height for both genders.''')
|
| 234 |
+
st.markdown('''### PRODUCT VS BRANDS''')
|
| 235 |
+
plt.figure(figsize=(12, 8))
|
| 236 |
+
ax = sns.histplot(data=df, x='Category', hue='Brand', multiple="stack", palette='rocket', bins=20)
|
| 237 |
+
|
| 238 |
+
plt.title("Product Category and Brand Distribution")
|
| 239 |
+
plt.xlabel("Product Category")
|
| 240 |
+
plt.ylabel("Count")
|
| 241 |
+
|
| 242 |
+
# Manually create legend if it's not generated
|
| 243 |
+
handles, labels = ax.get_legend_handles_labels()
|
| 244 |
+
|
| 245 |
+
if not labels:
|
| 246 |
+
# Ensure unique brand names appear in the legend
|
| 247 |
+
unique_brands = df['Brand'].unique()
|
| 248 |
+
palette = sns.color_palette('rocket', len(unique_brands))
|
| 249 |
+
|
| 250 |
+
# Create legend handles with reversed color order
|
| 251 |
+
handles = [plt.Rectangle((0, 0), 1, 1, color=palette[i]) for i in range(len(unique_brands))]
|
| 252 |
+
|
| 253 |
+
# Reverse both handles and labels
|
| 254 |
+
handles = handles[::-1]
|
| 255 |
+
labels = unique_brands[::-1]
|
| 256 |
+
|
| 257 |
+
# Apply reversed legend
|
| 258 |
+
plt.legend(handles, labels, title="Product Brand", loc='upper right')
|
| 259 |
+
plt.show()
|
| 260 |
+
|
| 261 |
+
st.markdown('''All products are from all the brands present in the dataset.''')
|
| 262 |
+
st.markdown('''### PRODUCT VS PRICE''')
|
| 263 |
+
plt = px.histogram(df, x = 'Price', color='Category', title="Product Category and Price Distribution", color_discrete_sequence=px.colors.sequential.Blackbody)
|
| 264 |
+
plt.show()
|
| 265 |
+
st.markdown('''**Insights :**
|
| 266 |
+
- **Price Range:** The x-axis shows a price range likely from 0 to 3000 (units unspecified, but presumably currency).
|
| 267 |
+
|
| 268 |
+
- **Category Distribution Across Price:** The stacked areas illustrate how the proportion of each product category varies across the price spectrum.
|
| 269 |
+
1. .**Smartphones (Black):** Appear to be concentrated in the lower to mid-price ranges, with fewer smartphones at the higher price points.
|
| 270 |
+
2. **Smart Watches (Red):** Show a relatively consistent distribution across the price range, though perhaps slightly more prevalent in the mid-range.
|
| 271 |
+
3. **Tablets (Yellow):** Seem to be more common in the mid-price range, with fewer tablets at both the low and high ends.
|
| 272 |
+
4. **Laptops (White):** Tend to dominate the higher price ranges, as expected. There are very few laptops at the lower price points.
|
| 273 |
+
5. **Headphones (Light Blue):** Have a fairly even distribution across the price range, although there's a slight increase in the mid-to-high price range.
|
| 274 |
+
|
| 275 |
+
- **Overlapping Areas:** The stacked nature of the chart allows you to see the total number of products at each price point by summing the heights of the stacked areas.''')
|
| 276 |
+
st.markdown('''### BRANDS VS PRICE''')
|
| 277 |
+
plt = px.histogram(df, x='Price', color='Brand', title="Product Category and Price Distribution",
|
| 278 |
+
color_discrete_sequence=px.colors.sequential.Plasma)
|
| 279 |
+
plt.show()
|
| 280 |
+
|
| 281 |
+
st.markdown('''**Insights :**
|
| 282 |
+
|
| 283 |
+
- **Price Range:** The x-axis covers a price range, likely from 0 to 3000 (currency unspecified).
|
| 284 |
+
|
| 285 |
+
- **Brand Distribution Across Price:** The stacked bars show the count of products from each brand within different price intervals.
|
| 286 |
+
|
| 287 |
+
1. **Apple (Darkest Purple/Blue):** Appears to have a significant presence across most of the price range, though perhaps slightly less so at the very lowest end.
|
| 288 |
+
|
| 289 |
+
2. **HP (Medium Purple):** Also has a fairly broad distribution across price points, with a noticeable presence in the mid-range.
|
| 290 |
+
|
| 291 |
+
3. **Sony (Lighter Purple):** Seems to be more concentrated in the mid-to-high price range.
|
| 292 |
+
4. **Samsung (Lightest Purple/Pink):** Has a presence across the price range, but seems to be more prominent in the mid-range and slightly lower-mid range.
|
| 293 |
+
5. **Other Brands (Darkest Purple/Blue, sometimes hard to distinguish from Apple):** This category seems to have a substantial presence across all price points, particularly at the lower end. This suggests a large variety of less prominent brands catering to different price segments.
|
| 294 |
+
- **Overlapping Areas/Stacked Bars:** The stacked nature of the chart shows the total number of products at each price point by adding up the heights of the different brand segments.''')
|
| 295 |
+
st.markdown('''### AGE vs PRODUCT CATEGORY and PRICE''')
|
| 296 |
+
plt = px.histogram(df, x= 'CustomerAge', y = 'Price', color= 'Category',title="Customer Age and Product Category Distribution")
|
| 297 |
+
plt.show()
|
| 298 |
+
st.markdown('''**Insights :**
|
| 299 |
+
|
| 300 |
+
- **Category Distribution Across Age:** The stacked bars illustrate how the proportion of each product category contributes to the total orders within each age group.
|
| 301 |
+
|
| 302 |
+
1. **Smartphones (Blue):** Appear to have a fairly consistent demand across all age groups, forming the base of most stacks. This suggests smartphones are a popular category regardless of age.
|
| 303 |
+
2. **Smart Watches (Red):** Show a notable presence, with potentially higher contributions in the younger and middle-age groups. This could indicate that smartwatches are more popular among these demographics.
|
| 304 |
+
3. **Tablets (Green):** Have a somewhat consistent demand across age groups, similar to smartphones but with a smaller overall contribution to total orders.
|
| 305 |
+
4. **Laptops (Purple):** Appear to have a strong presence across all age groups, often rivaling or exceeding smartphones in contribution. This suggests laptops are essential for a wide range of ages.
|
| 306 |
+
5. **Headphones (Orange):** Show a relatively consistent pattern across age groups, with a moderate contribution to total orders.
|
| 307 |
+
|
| 308 |
+
- Insights:
|
| 309 |
+
|
| 310 |
+
1. **Age-Related Preferences:** While some categories like smartphones and laptops seem to have broad appeal, there are hints of age-related preferences. For example, smartwatches might be more popular among younger demographics.
|
| 311 |
+
2. **Dominant Categories:** Smartphones and laptops appear to be the most consistently popular categories across most age groups.''')
|
| 312 |
+
st.markdown('''### HEATMAP | CORRELATION MATRIX''')
|
| 313 |
+
st.markdown('''Label Encoding''')
|
| 314 |
+
|
| 315 |
+
from sklearn.preprocessing import LabelEncoder
|
| 316 |
|
| 317 |
+
# Create a LabelEncoder instance
|
| 318 |
+
label_encoder = LabelEncoder()
|
| 319 |
+
|
| 320 |
+
# Fit and transform the 'ProductBrand' column
|
| 321 |
+
df['Brand'] = label_encoder.fit_transform(df['Brand'])
|
| 322 |
+
|
| 323 |
+
# Check the unique labels and their corresponding numeric values
|
| 324 |
+
print(f"Label Encoding Mapping: {dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)))}")
|
| 325 |
+
df['Category'] = label_encoder.fit_transform(df['Category'])
|
| 326 |
|
| 327 |
+
# Check the unique labels and their corresponding numeric values
|
| 328 |
+
print(f"Label Encoding Mapping: {dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)))}")
|
| 329 |
|
| 330 |
+
# Display the transformed column
|
| 331 |
+
print(df.head())
|
| 332 |
|
| 333 |
+
import matplotlib.pyplot as plt
|
| 334 |
+
# correlation matrix and heatmap
|
| 335 |
+
corr = df.corr()
|
| 336 |
+
plt.figure(figsize=(20, 10))
|
| 337 |
+
sns.heatmap(corr, annot=True)
|
| 338 |
+
plt.title('Correlation Matrix')
|
| 339 |
+
plt.show()
|
| 340 |
|
| 341 |
+
st.markdown('''**Insights :**
|
| 342 |
|
| 343 |
+
Correlation is a statistical measure that indicates the strength and direction of the linear relationship between two variables. The correlation coefficient ranges from -1 to 1, with the following interpretations:
|
| 344 |
|
| 345 |
+
- -1: Perfect negative correlation (as one variable increases, the other decreases)
|
| 346 |
+
- 0: No correlation (the variables are independent)
|
| 347 |
+
- 1: Perfect positive correlation (as one variable increases, the other increases)''')
|
| 348 |
+
st.markdown('''''')
|
| 349 |
+
else:
|
| 350 |
+
st.warning("No dataset uploaded. Please upload data on the Home Page.")
|
| 351 |
|
| 352 |
|