@@ -106,15 +106,19 @@ area.plot(
106
106
107
107
![ Histogram Plot] ( assets/images/analysis_modules/plots/histogram_plot.svg ) { align=right loading=lazy width="50%"}
108
108
109
- Histograms are particularly useful for visualizing the distribution of data, allowing you to see how values in one or more metrics are spread across different ranges. This module also supports grouping by categories, enabling you to compare the distributions across different groups. When grouping by a category, multiple histograms are generated on the same plot, allowing for easy comparison across categories.
109
+ Histograms are particularly useful for visualizing the distribution of data, allowing you to see how values in one or
110
+ more metrics are spread across different ranges. This module also supports grouping by categories, enabling you to
111
+ compare the distributions across different groups. When grouping by a category, multiple histograms are generated on the
112
+ same plot, allowing for easy comparison across categories.
110
113
111
114
Histograms are commonly used to analyze:
112
115
113
116
- Sales, revenue or other metric distributions
114
117
- Distribution of customer segments (e.g., by age, income)
115
118
- Comparing metric distributions across product categories
116
119
117
- This module allows you to customize legends, axes, and other visual elements, as well as apply clipping or filtering on the data values to focus on specific ranges.
120
+ This module allows you to customize legends, axes, and other visual elements, as well as apply clipping or filtering on
121
+ the data values to focus on specific ranges.
118
122
119
123
</div >
120
124
@@ -154,15 +158,19 @@ histogram.plot(
154
158
155
159
![ Bar Plot] ( assets/images/analysis_modules/plots/bar_plot.svg ) { align=right loading=lazy width="50%"}
156
160
157
- Bar plots are ideal for visualizing comparisons between categories or groups, showing how metrics such as revenue, sales, or other values vary across different categories. This module allows you to easily group bars by different categories and stack them when comparing multiple metrics. You can also add data labels to display absolute or percentage values for each bar.
161
+ Bar plots are ideal for visualizing comparisons between categories or groups, showing how metrics such as revenue,
162
+ sales, or other values vary across different categories. This module allows you to easily group bars by different
163
+ categories and stack them when comparing multiple metrics. You can also add data labels to display absolute or
164
+ percentage values for each bar.
158
165
159
166
Bar plots are frequently used to compare:
160
167
161
168
- Product sales across regions or quarters
162
169
- Revenue across product categories or customer segments
163
170
- Performance metrics side by side
164
171
165
- This module provides flexibility in customizing legends, axes, and other visual elements, making it easy to represent data across different dimensions, either as grouped or single bar plots.
172
+ This module provides flexibility in customizing legends, axes, and other visual elements, making it easy to represent
173
+ data across different dimensions, either as grouped or single bar plots.
166
174
167
175
</div >
168
176
@@ -391,39 +399,97 @@ pa.df.head()
391
399
392
400
<div class =" clear " markdown >
393
401
394
- ![ Image title ] ( https://placehold.co/600x400/EEE/31343C ) { align=right loading=lazy width="50%"}
402
+ ![ Cross Shop ] ( assets/images/analysis_modules/cross_shop.svg ) { align=right loading=lazy width="50%"}
395
403
396
- PASTE TEXT HERE
404
+ Cross Shop analysis visualizes the overlap between different customer groups or product categories, helping retailers
405
+ understand cross-purchasing behaviors. This powerful visualization technique employs Venn or Euler diagrams to show how
406
+ customers interact across different product categories or segments.
407
+
408
+ Key applications include:
409
+
410
+ - Identifying opportunities for cross-selling and bundling
411
+ - Evaluating product category relationships
412
+ - Analyzing promotion cannibalization
413
+ - Understanding customer shopping patterns across departments
414
+ - Planning targeted marketing campaigns based on complementary purchasing behavior
415
+
416
+ The module provides options to visualize both the proportional size of each group and the percentage of overlap, making
417
+ it easy to identify significant patterns in customer shopping behavior.
397
418
398
419
</div >
399
420
400
421
Example:
401
422
402
423
``` python
403
- PASTE CODE HERE
424
+ from pyretailscience import cross_shop
425
+
426
+ cs_customers = cross_shop.CrossShop(
427
+ df,
428
+ group_1_idx = df[" category_name" ] == " Electronics" ,
429
+ group_2_idx = df[" category_name" ] == " Clothing" ,
430
+ group_3_idx = df[" category_name" ] == " Home" ,
431
+ labels = [" Electronics" , " Clothing" , " Home" ],
432
+ )
433
+
434
+ cs_customers.plot(
435
+ title = " Customer Spend Overlap Across Categories" ,
436
+ source_text = " Source: PyRetailScience" ,
437
+ )
404
438
```
405
439
406
440
### Gain Loss
407
441
408
442
<div class =" clear " markdown >
409
443
410
- ![ Image title] ( https://placehold.co/600x400/EEE/31343C ) { align=right loading=lazy width="50%"}
444
+ ![ Gain Loss] ( assets/images/analysis_modules/gain_loss.svg ) { align=right loading=lazy width="50%"}
445
+
446
+ The Gain Loss module (also known as switching analysis) helps analyze changes in customer behavior between two time
447
+ periods. It breaks down revenue or customer movement between a focus group and a comparison group by:
448
+
449
+ - New customers: Customers who didn't purchase in period 1 but did in period 2
450
+ - Lost customers: Customers who purchased in period 1 but not in period 2
451
+ - Increased/decreased spending: Existing customers who changed their spending level
452
+ - Switching: Customers who moved between the focus and comparison groups
453
+
454
+ This module is particularly valuable for:
411
455
412
- PASTE TEXT HERE
456
+ - Analyzing promotion cannibalization
457
+ - Understanding customer migration between brands or categories
458
+ - Evaluating the effectiveness of marketing campaigns
459
+ - Quantifying the sources of revenue changes
413
460
414
461
</div >
415
462
416
463
Example:
417
464
418
465
``` python
419
- PASTE CODE HERE
466
+ from pyretailscience.gain_loss import GainLoss
467
+
468
+ gl = GainLoss(
469
+ df = df,
470
+ p1_index = df[" transaction_date" ] < " 2023-05-01" ,
471
+ p2_index = df[" transaction_date" ] >= " 2023-05-01" ,
472
+ focus_group_index = df[" brand" ] == " Brand A" ,
473
+ focus_group_name = " Brand A" ,
474
+ comparison_group_index = df[" brand" ] == " Brand B" ,
475
+ comparison_group_name = " Brand B" ,
476
+ )
477
+
478
+ gl.plot(
479
+ title = " Brand A vs Brand B: Customer Movement Analysis" ,
480
+ x_label = " Revenue Change" ,
481
+ source_text = " Source: PyRetailScience" ,
482
+ move_legend_outside = True ,
483
+ )
420
484
```
421
485
422
486
### Customer Decision Hierarchy
423
487
424
488
<div class =" clear " markdown >
425
489
426
- ![ Customer Decision Hierarchy] ( assets/images/analysis_modules/customer_decision_hierarchy.svg ) { align=right loading=lazy width="50%"}
490
+ ![ Customer Decision Hierarchy] (
491
+ assets/images/analysis_modules/customer_decision_hierarchy.svg
492
+ ){ align=right loading=lazy width="50%"}
427
493
428
494
A Customer Decision Hierarchy (CDH), also known as a Customer Decision Tree, is a powerful tool in retail analytics that
429
495
visually represents the sequential steps and criteria customers use when making purchase decisions within a specific
@@ -494,13 +560,10 @@ Example:
494
560
``` python
495
561
from pyretailscience import revenue_tree
496
562
497
- p1_index = df[" transaction_date" ] < " 2023-06-01"
498
- p2_index = df[" transaction_date" ] >= " 2023-06-01"
499
-
500
563
rev_tree = revenue_tree.RevenueTree(
501
564
df = df,
502
- p1_index = p1_index ,
503
- p2_index = p2_index ,
565
+ p1_index = df[ " transaction_date " ] < " 2023-06-01 " ,
566
+ p2_index = df[ " transaction_date " ] >= " 2023-06-01 " ,
504
567
)
505
568
```
506
569
@@ -528,25 +591,11 @@ entirely, or place them in a separate "Zero" segment.
528
591
Example:
529
592
530
593
``` python
531
- import numpy as np
532
- import pandas as pd
533
-
534
594
from pyretailscience.plots import bar
535
595
from pyretailscience.segmentation import HMLSegmentation
536
596
537
- # Create sample transaction data
538
- rng = np.random.default_rng(42 )
539
- df = pd.DataFrame(
540
- {
541
- " customer_id" : np.repeat(range (1 , 51 ), 3 ), # 50 customers with 3 transactions each
542
- " unit_spend" : rng.pareto(a = 1.5 , size = 150 ) * 20 , # Pareto distribution to mimic real spending
543
- },
544
- )
545
-
546
- # Create HML segmentation
547
597
seg = HMLSegmentation(df, zero_value_customers = " include_with_light" )
548
598
549
- # Visualize spend by segment
550
599
bar.plot(
551
600
seg.df.groupby(" segment_name" )[" unit_spend" ].sum(),
552
601
value_col = " unit_spend" ,
@@ -585,35 +634,21 @@ them with the lowest segment, exclude them entirely, or place them in a separate
585
634
Example:
586
635
587
636
``` python
588
- import numpy as np
589
- import pandas as pd
590
-
591
637
from pyretailscience.plots import bar
592
638
from pyretailscience.segmentation import ThresholdSegmentation
593
639
594
- # Create sample transaction data
595
- rng = np.random.default_rng(42 )
596
- df = pd.DataFrame(
597
- {
598
- " customer_id" : np.repeat(range (1 , 51 ), 3 ), # 50 customers with 3 transactions each
599
- " unit_spend" : rng.pareto(a = 1.5 , size = 150 ) * 20 , # Pareto distribution to mimic real spending
600
- },
601
- )
602
-
603
640
# Create custom segmentation with quartiles
604
641
# Define thresholds at 25%, 50%, 75%, and 100% (quartiles)
605
642
thresholds = [0.25 , 0.50 , 0.75 , 1.0 ]
606
643
segments = [" Bronze" , " Silver" , " Gold" , " Platinum" ]
607
644
608
- # Create threshold segmentation
609
645
seg = ThresholdSegmentation(
610
646
df = df,
611
647
thresholds = thresholds,
612
648
segments = segments,
613
649
zero_value_customers = " separate_segment" ,
614
650
)
615
651
616
- # Visualize spend by segment
617
652
bar.plot(
618
653
seg.df.groupby(" segment_name" )[" unit_spend" ].sum(),
619
654
value_col = " unit_spend" ,
@@ -630,62 +665,126 @@ bar.plot(
630
665
631
666
<div class =" clear " markdown >
632
667
633
- ![ Image title] ( https://placehold.co/600x400/EEE/31343C ) { align=right loading=lazy width="50%"}
634
-
635
- PASTE TEXT HERE
668
+ The Segmentation Stats module provides functionality to calculate transaction statistics by segment for a particular
669
+ segmentation. It makes it easy to compare key metrics across different segments, helping you understand how your
670
+ customer (or transactions or promotions) groups differ in terms of spending behavior and transaction patterns.
671
+ This module calculates metrics such as total spend, number of transactions, average spend per customer, and transactions
672
+ per customer for each segment. It's particularly useful when combined with other segmentation approaches like HML
673
+ segmentation.
636
674
637
675
</div >
638
676
639
677
Example:
640
678
641
679
``` python
642
- PASTE CODE HERE
680
+ from pyretailscience.segmentation import HMLSegmentation, SegTransactionStats
681
+
682
+ seg = HMLSegmentation(df, zero_value_customers = " include_with_light" )
683
+
684
+ # First, segment customers using HML segmentation
685
+ segmentation = HMLSegmentation(df)
686
+
687
+ # Add segment labels to the transaction data
688
+ df_with_segments = segmentation.add_segment(df)
689
+
690
+ # Calculate transaction statistics by segment
691
+ segment_stats = SegTransactionStats(df_with_segments)
692
+
693
+ # Display the statistics
694
+ segment_stats.df
643
695
```
696
+ <!-- markdownlint-disable MD013 -->
697
+ | segment_name | spend | transactions | customers | spend_per_customer | spend_per_transaction | transactions_per_customer | customers_pct |
698
+ | :---------------| ---------:| ---------------:| ------------:| ---------------------:| ------------------------:| ----------------------------:| ----------------:|
699
+ | Heavy | 2927.21 | 30 | 10 | 292.721 | 97.5735 | 3 | 0.2 |
700
+ | Medium | 1014.97 | 45 | 15 | 67.6644 | 22.5548 | 3 | 0.3 |
701
+ | Light | 662.107 | 75 | 25 | 26.4843 | 8.82809 | 3 | 0.5 |
702
+ | Total | 4604.28 | 150 | 50 | 92.0856 | 30.6952 | 3 | 1 |
703
+ <!-- markdownlint-enable MD013 -->
644
704
645
705
### Purchases Per Customer
646
706
647
707
<div class =" clear " markdown >
648
708
649
- ![ Image title] ( https://placehold.co/600x400/EEE/31343C ) { align=right loading=lazy width="50%"}
709
+ ![ Purchases Per Customer] (
710
+ assets/images/analysis_modules/purchases_per_customer.svg
711
+ ){align=right loading=lazy width="50%"}
650
712
651
- PASTE TEXT HERE
713
+ The Purchases Per Customer module analyzes and visualizes the distribution of transaction frequency across your customer
714
+ base. This module helps you understand customer purchasing patterns by percentile and is useful for determining values
715
+ like your churn window.
652
716
653
717
</div >
654
718
655
719
Example:
656
720
657
721
``` python
658
- PASTE CODE HERE
722
+ from pyretailscience.customer import PurchasesPerCustomer
723
+
724
+ ppc = PurchasesPerCustomer(transactions)
725
+
726
+ ppc.plot(
727
+ title = " Purchases per Customer" ,
728
+ percentile_line = 0.8 ,
729
+ source_text = " Source: PyRetailScience" ,
730
+ )
659
731
```
660
732
661
733
### Days Between Purchases
662
734
663
735
<div class =" clear " markdown >
664
736
665
- ![ Image title] ( https://placehold.co/600x400/EEE/31343C ) { align=right loading=lazy width="50%"}
737
+ ![ Days Between Purchases] (
738
+ assets/images/analysis_modules/days_between_purchases.svg
739
+ ){align=right loading=lazy width="50%"}
740
+
741
+ The Days Between Purchases module analyzes the time intervals between customer transactions, providing valuable insights
742
+ into purchasing frequency and shopping patterns. This analysis helps you understand:
666
743
667
- PASTE TEXT HERE
744
+ - How frequently your customers typically return to make purchases
745
+ - The distribution of purchase intervals across your customer base
746
+ - Which customer segments have shorter or longer repurchase cycles
747
+ - Where intervention might be needed to prevent customer churn
748
+
749
+ This information is critical for planning communication frequency, timing promotional campaigns, and developing
750
+ effective retention strategies. The module can visualize both standard and cumulative distributions of days between
751
+ purchases.
668
752
669
753
</div >
670
754
671
755
Example:
672
756
673
757
``` python
674
- PASTE CODE HERE
758
+ dbp = DaysBetweenPurchases(transactions)
759
+
760
+ dbp.plot(
761
+ bins = 15 ,
762
+ title = " Average Days Between Customer Purchases" ,
763
+ percentile_line = 0.5 , # Mark the median with a line
764
+ )
675
765
```
676
766
677
767
### Transaction Churn
678
768
679
769
<div class =" clear " markdown >
680
770
681
- ![ Image title ] ( https://placehold.co/600x400/EEE/31343C ) { align=right loading=lazy width="50%"}
771
+ ![ Transaction Churn ] ( assets/images/analysis_modules/transaction_churn.svg ) { align=right loading=lazy width="50%"}
682
772
683
- PASTE TEXT HERE
773
+ The Transaction Churn module analyzes how customer churn rates vary based on the number of purchases customers have
774
+ made. This helps reveal critical retention thresholds in the customer lifecycle when setting a churn window
684
775
685
776
</div >
686
777
687
778
Example:
688
779
689
780
``` python
690
- PASTE CODE HERE
781
+ from pyretailscience.customer import TransactionChurn
782
+
783
+ tc = TransactionChurn(transactions, churn_period = churn_period)
784
+
785
+ tc.plot(
786
+ title = " Churn Rate by Number of Purchases" ,
787
+ cumulative = True ,
788
+ source_text = " Source: PyRetailScience" ,
789
+ )
691
790
```
0 commit comments