Cross Shop Analysis
Cross-shop analysis: per-customer overlap (Venn) of 2-3 segments defined by column=value filters.
CrossShop
Per-customer overlap of 2-3 segments defined by column=value filters.
Both results are pandas DataFrames, computed at construction:
cross_shop_df: one row pergroup_colvalue (the index), including entities in no segment. Columnsgroup_1,group_2(andgroup_3) are 0/1 flags,groupsis the tuple of those flags in segment order,group_labelsjoins the matching labels ("No Groups" when none), andvalue_colholds the entity's value.cross_shop_table_df: one row pergroupscombination, with itsgroup_labels, the summedvalue_colandpercent, that combination's share of the total (a fraction). "No Groups" counts in the total. The share is ofvalue_col(spend by default), not of customers; passvalue_col=customer_id, agg_func="nunique"for customer shares.
Source code in openretailscience/analysis/cross_shop.py
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 | |
__init__(df, group_1_col, group_1_val, group_2_val, group_2_col=None, group_3_col=None, group_3_val=None, labels=None, group_col=None, value_col=None, agg_func='sum')
Initialize cross-shop analysis for 2 or 3 segments defined by column=value filters.
Each entity appears once: a segment flag is 1 if the entity has ANY transaction
matching that segment, and value_col is the entity's
agg_func over ALL of its transactions, not just matching ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Table
|
Transaction data with customer purchases. |
required |
group_1_col
|
str
|
Column defining the first segment (e.g., "category", "brand", "channel"). |
required |
group_1_val
|
str
|
Value of |
required |
group_2_val
|
str
|
Value of |
required |
group_2_col
|
str
|
Column defining the second segment.
Defaults to |
None
|
group_3_col
|
str
|
Column for three-way analysis.
Defaults to |
None
|
group_3_val
|
str
|
Value of |
None
|
labels
|
list[str]
|
Segment labels. Defaults to alphabetical labels [A, B, C]. |
None
|
group_col
|
str
|
Entity column to overlap on. Defaults to option
|
None
|
value_col
|
str
|
Metric to aggregate per entity. Defaults to option
|
None
|
agg_func
|
str
|
Ibis aggregation name combining the entity's values (e.g. "sum", "nunique", "count"). The table then sums these across entities, so "mean" gives a sum of per-customer means. Defaults to "sum". |
'sum'
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If required columns are missing, the label count doesn't match the groups,
or |
Source code in openretailscience/analysis/cross_shop.py
plot(title=None, eyebrow=None, subtitle=None, source_text=None, vary_size=False, figsize=None, ax=None, subset_label_formatter=None, **kwargs)
Generate a Venn diagram of the segment overlaps in cross_shop_table_df.
Region labels are each combination's percent, a share of value_col (spend by default).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
Chart title (e.g., "Cross-Shopping: Organic vs Conventional"). |
None
|
eyebrow
|
str
|
Small uppercase label rendered above the title. Defaults to None. |
None
|
subtitle
|
str
|
Supporting copy rendered below the title. Defaults to None. |
None
|
source_text
|
str
|
Data source attribution. Defaults to None. |
None
|
vary_size
|
bool
|
Scale circles by segment value. True = larger segments appear bigger. Defaults to False. |
False
|
figsize
|
tuple[int, int]
|
Plot dimensions. Defaults to None. |
None
|
ax
|
Axes
|
Existing axes for subplot integration. Defaults to None. |
None
|
subset_label_formatter
|
Callable
|
Custom formatting for subset percentages. Default shows one decimal place (e.g., "34.5%"). |
None
|
**kwargs
|
Any
|
Additional diagram customization options. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
SubplotBase |
SubplotBase
|
Matplotlib axes containing the cross-shop visualization. |