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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """.1294
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/18GMbHEjdUUsZiko73-qVxV-WVgsf5hgs
8
+ """
9
+
10
+
11
+
12
+ import numpy as np
13
+ import pandas as pd
14
+ import plotly.express as px
15
+ import plotly.graph_objects as go
16
+ from sklearn.cluster import KMeans
17
+ from sklearn.preprocessing import StandardScaler
18
+ import statsmodels.api as sm
19
+ import warnings
20
+
21
+ import warnings # Importing the warnings module
22
+ warnings.filterwarnings('ignore') # Calling the filterwarnings function
23
+
24
+ df = pd.read_csv("/content/shopping_trends (2).csv")
25
+
26
+ df.head()
27
+
28
+ df.sample(10)
29
+
30
+ df.info()
31
+
32
+ fig_age = px.histogram(
33
+ df,
34
+ x='Age',
35
+ nbins= 50,
36
+ title='Age Distribution of Customers',
37
+ color_discrete_sequence=['cyan']
38
+ )
39
+
40
+ fig_age.update_layout(
41
+ template='plotly_dark',
42
+ plot_bgcolor='black',
43
+ paper_bgcolor='black',
44
+ font=dict(color='white')
45
+ )
46
+ fig_age.show()
47
+
48
+ gender_counts = df['Gender'].value_counts().reset_index()
49
+ gender_counts.columns = ['Gender', 'Count']
50
+
51
+ fig_gender = px.pie(
52
+ gender_counts,
53
+ names='Gender',
54
+ values='Count',
55
+ title='Gender Proportions of Customers',
56
+ color_discrete_sequence=px.colors.sequential.RdBu
57
+ )
58
+ fig_gender.update_layout(
59
+ template='plotly_dark',
60
+ plot_bgcolor='black',
61
+ paper_bgcolor='black',
62
+ font=dict(color='white')
63
+ )
64
+ fig_gender.show()
65
+
66
+ location_counts = df['Location'].value_counts().reset_index()
67
+ location_counts.columns = ['Location', 'Count']
68
+
69
+ fig_location = px.bar(
70
+ location_counts,
71
+ x='Location',
72
+ y='Count',
73
+ text='Count',
74
+ title='Customer Count by Location',
75
+ color_discrete_sequence=['lime']
76
+ )
77
+ location_counts = df['Location'].value_counts().reset_index()
78
+ location_counts.columns = ['Location', 'Count']
79
+
80
+ fig_location = px.bar(
81
+ location_counts,
82
+ x='Location',
83
+ y='Count',
84
+ text='Count',
85
+ title='Customer Count by Location',
86
+ color_discrete_sequence=['lime']
87
+ )
88
+ fig_location.update_layout(
89
+ template='plotly_dark',
90
+ plot_bgcolor='black',
91
+ paper_bgcolor='black',
92
+ font=dict(color='white'),
93
+ xaxis_title="Location",
94
+ yaxis_title="Number of Customers"
95
+ )
96
+ fig_location.show()
97
+ fig_location = px.bar(
98
+ location_counts,
99
+ x='Location',
100
+ y='Count',
101
+ text='Count',
102
+ title='Customer Count by Location',
103
+ color_discrete_sequence=['lime']
104
+ )
105
+ fig_location.update_layout(
106
+ template='plotly_dark',
107
+ plot_bgcolor='black',
108
+ paper_bgcolor='black',
109
+ font=dict(color='white'),
110
+ xaxis_title="Location",
111
+ yaxis_title="Number of Customers"
112
+ )
113
+ fig_location.show()
114
+
115
+ item_counts = df['Item Purchased'].value_counts().reset_index()
116
+ item_counts.columns = ['Item Purchased', 'Count']
117
+
118
+ fig_items = px.bar(
119
+ item_counts,
120
+ x='Item Purchased',
121
+ y='Count',
122
+ text='Count',
123
+ title='Most Purchased Items',
124
+ color_discrete_sequence=['orange']
125
+ )
126
+ fig_items.update_layout(
127
+ template='plotly_dark',
128
+ plot_bgcolor='black',
129
+ paper_bgcolor='black',
130
+ font=dict(color='white'),
131
+ xaxis_title='Items',
132
+ yaxis_title='Count of Purchases'
133
+ )
134
+ fig_items.show()
135
+
136
+ fig_amount = px.box(
137
+ df,
138
+ y='Purchase Amount (USD)', # Changed from 'Purchased Amount (USD)'
139
+ title='Purchase Amount Distribution',
140
+ color_discrete_sequence=['magenta']
141
+ )
142
+
143
+ fig_amount.update_layout(
144
+ template='plotly_dark',
145
+ plot_bgcolor='black',
146
+ paper_bgcolor='black',
147
+ font=dict(color='white'),
148
+ yaxis_title='Purchase Amount (USD)'
149
+ )
150
+ fig_amount.show()
151
+
152
+ # Count popular sizes
153
+ size_counts = df['Size'].value_counts().reset_index()
154
+ size_counts.columns = ['Size', 'Count']
155
+
156
+ fig_sizes = px.bar(
157
+ size_counts,
158
+ x='Size',
159
+ y='Count',
160
+ text='Count',
161
+ title='Preferred Sizes',
162
+ color_discrete_sequence=['green']
163
+ )
164
+ fig_sizes.update_layout(
165
+ template='plotly_dark',
166
+ plot_bgcolor='black',
167
+ paper_bgcolor='black',
168
+ font=dict(color='white'),
169
+ xaxis_title='Size',
170
+ yaxis_title='Count of Purchases'
171
+ )
172
+ fig_sizes.show()
173
+
174
+ # Count popular colors
175
+ color_counts = df['Color'].value_counts().reset_index()
176
+ color_counts.columns = ['Color', 'Count']
177
+
178
+ fig_colors = px.bar(
179
+ color_counts,
180
+ x='Color',
181
+ y='Count',
182
+ text='Count',
183
+ title='Preferred Colors',
184
+ color_discrete_sequence=['teal']
185
+ )
186
+ fig_colors.update_layout(
187
+ template='plotly_dark',
188
+ plot_bgcolor='black',
189
+ paper_bgcolor='black',
190
+ font=dict(color='white'),
191
+ xaxis_title='Color',
192
+ yaxis_title='Count of Purchases'
193
+ )
194
+ fig_colors.show()
195
+
196
+ # Seasonal Trends
197
+ season_counts = df['Season'].value_counts().reset_index()
198
+ season_counts.columns = ['Season', 'Count']
199
+
200
+ fig_season = px.bar(
201
+ season_counts,
202
+ x='Season',
203
+ y='Count',
204
+ text='Count',
205
+ title='Seasonal Trends in Purchases',
206
+ color_discrete_sequence=['blue']
207
+ )
208
+ fig_season.update_layout(
209
+ template='plotly_dark',
210
+ plot_bgcolor='black',
211
+ paper_bgcolor='black',
212
+ font=dict(color='white'),
213
+ xaxis_title='Season',
214
+ yaxis_title='Count of Purchases'
215
+ )
216
+ fig_season.show()
217
+
218
+ # Frequency of Purchases
219
+ frequency_counts = df['Frequency of Purchases'].value_counts().reset_index()
220
+ frequency_counts.columns = ['Frequency', 'Count']
221
+
222
+ fig_frequency = px.bar(
223
+ frequency_counts,
224
+ x='Frequency',
225
+ y='Count',
226
+ text='Count',
227
+ title='Frequency of Purchases',
228
+ color_discrete_sequence=['red']
229
+ )
230
+ fig_frequency.update_layout(
231
+ template='plotly_dark',
232
+ plot_bgcolor='black',
233
+ paper_bgcolor='black',
234
+ font=dict(color='white'),
235
+ xaxis_title='Frequency',
236
+ yaxis_title='Count of Purchases'
237
+ )
238
+ fig_frequency.show()
239
+
240
+ payment_counts = df['Payment Method'].value_counts().reset_index()
241
+ payment_counts.columns = ['Payment Method', 'Count']
242
+
243
+ fig_payment = px.pie(
244
+ payment_counts,
245
+ names='Payment Method',
246
+ values='Count',
247
+ title='Popular Payment Methods',
248
+ color_discrete_sequence=px.colors.sequential.Plasma
249
+ )
250
+ fig_payment.update_layout(
251
+ template='plotly_dark',
252
+ plot_bgcolor='black',
253
+ paper_bgcolor='black',
254
+ font=dict(color='white')
255
+ )
256
+ fig_payment.show()
257
+
258
+ subscription_data = df.groupby('Subscription Status')['Purchase Amount (USD)'].sum().reset_index()
259
+
260
+ fig_subscription = px.bar(
261
+ subscription_data,
262
+ x='Subscription Status',
263
+ y='Purchase Amount (USD)',
264
+ text='Purchase Amount (USD)',
265
+ title='Impact of Subscription on Purchases',
266
+ color='Subscription Status',
267
+ color_discrete_sequence=px.colors.sequential.Viridis
268
+ )
269
+ fig_subscription.update_layout(
270
+ template='plotly_dark',
271
+ plot_bgcolor='black',
272
+ paper_bgcolor='black',
273
+ font=dict(color='white'),
274
+ xaxis_title='Subscription Status',
275
+ yaxis_title='Total Purchase Amount (USD)'
276
+ )
277
+ fig_subscription.show()
278
+
279
+ discount_data = df['Discount Applied'].value_counts().reset_index()
280
+ discount_data.columns = ['Discount Applied', 'Count']
281
+
282
+ fig_discount = px.bar(
283
+ discount_data,
284
+ x='Discount Applied',
285
+ y='Count',
286
+ text='Count',
287
+ title='Discount Usage Analysis',
288
+ color='Discount Applied',
289
+ color_discrete_sequence=px.colors.sequential.Cividis
290
+ )
291
+ fig_discount.update_layout(
292
+ template='plotly_dark',
293
+ plot_bgcolor='black',
294
+ paper_bgcolor='black',
295
+ font=dict(color='white'),
296
+ xaxis_title='Discount Applied',
297
+ yaxis_title='Number of Purchases'
298
+ )
299
+ fig_discount.show()
300
+
301
+ category_revenue = df.groupby('Category')['Purchase Amount (USD)'].sum().reset_index()
302
+
303
+ fig_category_revenue = px.treemap(
304
+ category_revenue,
305
+ path=['Category'],
306
+ values='Purchase Amount (USD)',
307
+ title='Category-Wise Revenue',
308
+ color='Purchase Amount (USD)',
309
+ color_continuous_scale=px.colors.sequential.Sunset
310
+ )
311
+ fig_category_revenue.update_layout(
312
+ template='plotly_dark',
313
+ plot_bgcolor='black',
314
+ paper_bgcolor='black',
315
+ font=dict(color='white')
316
+ )
317
+ fig_category_revenue.show()
318
+
319
+ fig_ratings = px.histogram(
320
+ df,
321
+ x='Review Rating',
322
+ nbins=10,
323
+ title='Distribution of Review Ratings',
324
+ color_discrete_sequence=['#FFA07A']
325
+ )
326
+ fig_ratings.update_layout(
327
+ template='plotly_dark',
328
+ plot_bgcolor='black',
329
+ paper_bgcolor='black',
330
+ font=dict(color='white'),
331
+ xaxis_title='Review Rating',
332
+ yaxis_title='Count'
333
+ )
334
+ fig_ratings.show()
335
+
336
+ shipping_data = df.groupby('Shipping Type')['Purchase Amount (USD)'].sum().reset_index()
337
+
338
+ fig_shipping = px.bar(
339
+ shipping_data,
340
+ x='Shipping Type',
341
+ y='Purchase Amount (USD)',
342
+ text='Purchase Amount (USD)',
343
+ title='Shipping Types and Revenue Impact',
344
+ color='Shipping Type',
345
+ color_discrete_sequence=px.colors.sequential.Teal
346
+ )
347
+ fig_shipping.update_layout(
348
+ template='plotly_dark',
349
+ plot_bgcolor='black',
350
+ paper_bgcolor='black',
351
+ font=dict(color='white'),
352
+ xaxis_title='Shipping Type',
353
+ yaxis_title='Total Revenue (USD)'
354
+ )
355
+ fig_shipping.show()
356
+
357
+ customer_revenue = df.groupby('Customer ID')['Purchase Amount (USD)'].sum().reset_index()
358
+ customer_revenue = customer_revenue.sort_values(by='Purchase Amount (USD)', ascending=False)
359
+ customer_revenue['Cumulative Percentage'] = customer_revenue['Purchase Amount (USD)'].cumsum() / customer_revenue['Purchase Amount (USD)'].sum() * 100
360
+
361
+ fig_pareto = px.bar(
362
+ customer_revenue,
363
+ x='Customer ID',
364
+ y='Purchase Amount (USD)',
365
+ text='Purchase Amount (USD)',
366
+ title='High-Spending Customers - Pareto Chart',
367
+ color_discrete_sequence=['#FF7F50']
368
+ )
369
+ fig_pareto.add_scatter(
370
+ x=customer_revenue['Customer ID'],
371
+ y=customer_revenue['Cumulative Percentage'],
372
+ mode='lines+markers',
373
+ name='Cumulative Percentage',
374
+ line=dict(color='cyan')
375
+ )
376
+ fig_pareto.update_layout(
377
+ template='plotly_dark',
378
+ plot_bgcolor='black',
379
+ paper_bgcolor='black',
380
+ font=dict(color='white'),
381
+ xaxis_title='Customer ID',
382
+ yaxis_title='Purchase Amount (USD)',
383
+ yaxis2=dict(title='Cumulative Percentage', overlaying='y', side='right')
384
+ )
385
+ fig_pareto.show()
386
+
387
+ clustering_data = df.groupby('Customer ID').agg({
388
+ 'Purchase Amount (USD)': 'sum',
389
+ 'Frequency of Purchases': 'count',
390
+ 'Category': 'nunique'
391
+ }).reset_index()
392
+ clustering_data.columns = ['Customer ID', 'Total Purchase Amount', 'Purchase Frequency', 'Unique Categories']
393
+
394
+ # Standardize the data
395
+ scaler = StandardScaler()
396
+ clustering_data_scaled = scaler.fit_transform(clustering_data[['Total Purchase Amount', 'Purchase Frequency', 'Unique Categories']])
397
+
398
+ # Apply K-means clustering
399
+ kmeans = KMeans(n_clusters=3, random_state=42)
400
+ clustering_data['Cluster'] = kmeans.fit_predict(clustering_data_scaled)
401
+
402
+ # Scatter plot
403
+ fig_clusters = px.scatter_3d(
404
+ clustering_data,
405
+ x='Total Purchase Amount',
406
+ y='Purchase Frequency',
407
+ z='Unique Categories',
408
+ color='Cluster',
409
+ title='Behavioral Clusters of Customers',
410
+ symbol='Cluster',
411
+ color_continuous_scale=px.colors.sequential.Viridis
412
+ )
413
+ fig_clusters.update_layout(
414
+ template='plotly_dark',
415
+ plot_bgcolor='black',
416
+ paper_bgcolor='black',
417
+ font=dict(color='white'),
418
+ scene=dict(
419
+ xaxis_title='Total Purchase Amount',
420
+ yaxis_title='Purchase Frequency',
421
+ zaxis_title='Unique Categories'
422
+ )
423
+ )
424
+ fig_clusters.show()
425
+
426
+ fig_purchase_vs_rating = px.scatter(
427
+ df,
428
+ x='Purchase Amount (USD)',
429
+ y='Review Rating',
430
+ title='Purchase Amount vs. Review Rating',
431
+ color='Review Rating',
432
+ color_continuous_scale='Viridis'
433
+ )
434
+
435
+ # Add regression line
436
+ X = sm.add_constant(df['Purchase Amount (USD)']) # Add constant for intercept
437
+ y = df['Review Rating']
438
+ model = sm.OLS(y, X).fit()
439
+ df['Regression Line'] = model.predict(X)
440
+
441
+ fig_purchase_vs_rating.add_scatter(
442
+ x=df['Purchase Amount (USD)'],
443
+ y=df['Regression Line'],
444
+ mode='lines',
445
+ name='Regression Line',
446
+ line=dict(color='cyan')
447
+ )
448
+
449
+ fig_purchase_vs_rating.update_layout(
450
+ template='plotly_dark',
451
+ plot_bgcolor='black',
452
+ paper_bgcolor='black',
453
+ font=dict(color='white'),
454
+ xaxis_title='Purchase Amount (USD)',
455
+ yaxis_title='Review Rating'
456
+ )
457
+
458
+ fig_purchase_vs_rating.show()
459
+
460
+ fig_age_vs_spending = px.scatter(
461
+ df,
462
+ x='Age',
463
+ y='Purchase Amount (USD)',
464
+ title='Age vs. Spending Habits',
465
+ color='Age',
466
+ color_continuous_scale='Viridis'
467
+ )
468
+
469
+ fig_age_vs_spending.update_layout(
470
+ template='plotly_dark',
471
+ plot_bgcolor='black',
472
+ paper_bgcolor='black',
473
+ font=dict(color='white'),
474
+ xaxis_title='Age',
475
+ yaxis_title='Purchase Amount (USD)'
476
+ )
477
+
478
+ fig_age_vs_spending.show()
479
+
480
+ fig_category_vs_gender.update_layout(
481
+ template='plotly_dark', # Corrected the template name to 'plotly_dark'
482
+ plot_bgcolor='black',
483
+ paper_bgcolor='black',
484
+ font=dict(color='white'),
485
+ xaxis_title='Product Category',
486
+ yaxis_title='Count'
487
+ )
488
+ fig_category_vs_gender.show()
489
+
490
+ fig_discounts_vs_spending = px.box(
491
+ df,
492
+ x='Discount Applied',
493
+ y='Purchase Amount (USD)',
494
+ title='Effect of Discounts on Spending',
495
+ color='Discount Applied',
496
+ color_discrete_sequence=['#FF6347', '#20B2AA']
497
+ )
498
+
499
+ fig_discounts_vs_spending.update_layout(
500
+ template='plotly_dark',
501
+ plot_bgcolor='black',
502
+ paper_bgcolor='black',
503
+ font=dict(color='white'),
504
+ xaxis_title='Discount Applied',
505
+ yaxis_title='Purchase Amount (USD)'
506
+ )
507
+
508
+ fig_discounts_vs_spending.show()
509
+
510
+ fig_profitability_analysis = px.treemap(
511
+ df,
512
+ path=['Category', 'Size', 'Color'], # Hierarchy: Category -> Size -> Color
513
+ values='Purchase Amount (USD)',
514
+ title='Profitability Analysis by Category, Size, and Color',
515
+ color='Purchase Amount (USD)', # Color by total purchase amount
516
+ color_continuous_scale='Viridis'
517
+ )
518
+
519
+ fig_profitability_analysis.update_layout(
520
+ template='plotly_dark',
521
+ plot_bgcolor='black',
522
+ paper_bgcolor='black',
523
+ font=dict(color='white')
524
+ )
525
+
526
+ fig_profitability_analysis.show()
527
+