@@ -106,15 +106,19 @@ area.plot(
106106
107107![ Histogram Plot] ( assets/images/analysis_modules/plots/histogram_plot.svg ) { align=right loading=lazy width="50%"}
108108
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.
110113
111114Histograms are commonly used to analyze:
112115
113116- Sales, revenue or other metric distributions
114117- Distribution of customer segments (e.g., by age, income)
115118- Comparing metric distributions across product categories
116119
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.
118122
119123</div >
120124
@@ -154,15 +158,19 @@ histogram.plot(
154158
155159![ Bar Plot] ( assets/images/analysis_modules/plots/bar_plot.svg ) { align=right loading=lazy width="50%"}
156160
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.
158165
159166Bar plots are frequently used to compare:
160167
161168- Product sales across regions or quarters
162169- Revenue across product categories or customer segments
163170- Performance metrics side by side
164171
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.
166174
167175</div >
168176
@@ -391,39 +399,97 @@ pa.df.head()
391399
392400<div class =" clear " markdown >
393401
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%"}
395403
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.
397418
398419</div >
399420
400421Example:
401422
402423``` 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+ )
404438```
405439
406440### Gain Loss
407441
408442<div class =" clear " markdown >
409443
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:
411455
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
413460
414461</div >
415462
416463Example:
417464
418465``` 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+ )
420484```
421485
422486### Customer Decision Hierarchy
423487
424488<div class =" clear " markdown >
425489
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%"}
427493
428494A Customer Decision Hierarchy (CDH), also known as a Customer Decision Tree, is a powerful tool in retail analytics that
429495 visually represents the sequential steps and criteria customers use when making purchase decisions within a specific
@@ -494,13 +560,10 @@ Example:
494560``` python
495561from pyretailscience import revenue_tree
496562
497- p1_index = df[" transaction_date" ] < " 2023-06-01"
498- p2_index = df[" transaction_date" ] >= " 2023-06-01"
499-
500563rev_tree = revenue_tree.RevenueTree(
501564 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 " ,
504567)
505568```
506569
@@ -528,25 +591,11 @@ entirely, or place them in a separate "Zero" segment.
528591Example:
529592
530593``` python
531- import numpy as np
532- import pandas as pd
533-
534594from pyretailscience.plots import bar
535595from pyretailscience.segmentation import HMLSegmentation
536596
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
547597seg = HMLSegmentation(df, zero_value_customers = " include_with_light" )
548598
549- # Visualize spend by segment
550599bar.plot(
551600 seg.df.groupby(" segment_name" )[" unit_spend" ].sum(),
552601 value_col = " unit_spend" ,
@@ -585,35 +634,21 @@ them with the lowest segment, exclude them entirely, or place them in a separate
585634Example:
586635
587636``` python
588- import numpy as np
589- import pandas as pd
590-
591637from pyretailscience.plots import bar
592638from pyretailscience.segmentation import ThresholdSegmentation
593639
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-
603640# Create custom segmentation with quartiles
604641# Define thresholds at 25%, 50%, 75%, and 100% (quartiles)
605642thresholds = [0.25 , 0.50 , 0.75 , 1.0 ]
606643segments = [" Bronze" , " Silver" , " Gold" , " Platinum" ]
607644
608- # Create threshold segmentation
609645seg = ThresholdSegmentation(
610646 df = df,
611647 thresholds = thresholds,
612648 segments = segments,
613649 zero_value_customers = " separate_segment" ,
614650)
615651
616- # Visualize spend by segment
617652bar.plot(
618653 seg.df.groupby(" segment_name" )[" unit_spend" ].sum(),
619654 value_col = " unit_spend" ,
@@ -630,62 +665,126 @@ bar.plot(
630665
631666<div class =" clear " markdown >
632667
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.
636674
637675</div >
638676
639677Example:
640678
641679``` 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
643695```
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 -->
644704
645705### Purchases Per Customer
646706
647707<div class =" clear " markdown >
648708
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%"}
650712
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.
652716
653717</div >
654718
655719Example:
656720
657721``` 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+ )
659731```
660732
661733### Days Between Purchases
662734
663735<div class =" clear " markdown >
664736
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:
666743
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.
668752
669753</div >
670754
671755Example:
672756
673757``` 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+ )
675765```
676766
677767### Transaction Churn
678768
679769<div class =" clear " markdown >
680770
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%"}
682772
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
684775
685776</div >
686777
687778Example:
688779
689780``` 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+ )
691790```
0 commit comments