Product Associations
Market Basket Analysis and Product Association Rules for Retail Optimization.
Business Context
Product association analysis (market basket analysis) uncovers the hidden relationships in customer purchasing behavior. This intelligence transforms how retailers approach merchandising, marketing, and operations by revealing which products naturally sell together and why.
The Business Problem
Retailers lose revenue from missed cross-selling opportunities and poor product placement. Without understanding product associations, stores might: - Place complementary items in different aisles, reducing impulse purchases - Miss bundling opportunities that could increase average transaction value - Stock-out on associated items when promoting a product - Fail to identify new product opportunities based on basket gaps
Real-World Applications
- Strategic Merchandising
- Place chips near beer when data shows strong association
- Position phone cases near phones based on attachment rates
- Create end-cap displays with products that sell together
-
Optimize shelf space allocation using association strength
-
Dynamic Bundle Pricing
- Create "Breakfast bundle": Coffee + Pastry when uplift shows synergy
- Design seasonal bundles based on historical associations
- Price bundles to incentivize larger baskets while maintaining margins
-
Test bundle combinations using confidence metrics
-
Personalized Recommendations
- Power "Customers who bought X also bought Y" suggestions
- Enhance cart abandonment recovery with associated items
- Design email campaigns based on previous purchase associations
-
Improve search results by showing associated products
-
Inventory Optimization
- Stock pasta sauce when pasta is promoted (if association exists)
- Prepare battery inventory when toys are featured
- Coordinate supply chain for products that sell together
-
Reduce stockouts by understanding product relationships
-
New Product Placement
- Place new organic items near existing organic purchases
- Position private label next to associated national brands
- Test new products within high-association categories
- Leverage existing associations to drive trial
Key Metrics Explained
Support (Frequency)
The proportion of all transactions containing both products. Higher support indicates a more common pairing. Use this to identify: - Core product relationships for everyday decisions - Sufficient sample size for confident conclusions - Opportunities worth marketing investment
Confidence (Conditional Probability)
The probability of buying product B given product A was purchased. This answers "If a customer buys A, how likely are they to buy B?" Use this for: - Recommendation engine rules - Promotional targeting decisions - Cross-sell prioritization
Uplift/Lift (Synergy Indicator)
Measures how much more likely products are bought together than would be expected by chance. Uplift = Observed probability / Expected probability. Interpretation: - Uplift > 1: Products have positive association (sell better together) - Uplift = 1: Products are independent (no relationship) - Uplift < 1: Products have negative association (rarely bought together)
Higher uplift values indicate stronger synergies worth exploiting.
Actionable Decision Framework
Retailers should consider multiple metrics together:
High Support + High Confidence - Strong, frequent relationship - Priority for permanent merchandising changes - Core bundle candidates
Low Support + High Confidence - Niche but reliable relationship - Targeted marketing opportunities - Specialized customer segments
High Support + Low Confidence - Common but weak relationship - Test before major changes - Monitor for shifts over time
High Uplift (regardless of support) - Strong synergy exists - Test for merchandising opportunities - Consider for promotional strategies
Implementation Considerations
- Validate associations across time periods before major changes
- Consider seasonality in association patterns
- Test recommendations with A/B experiments
- Monitor associations as product mix evolves
- Account for external factors (promotions, events) affecting associations
ProductAssociation
A class for generating and analyzing product association rules.
This class calculates association rules between products based on transaction data, helping to identify patterns in customer purchasing behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
The input DataFrame containing transaction data. |
required |
value_col |
str
|
The name of the column in the input DataFrame that contains the product identifiers. |
required |
group_col |
str
|
The name of the column that identifies unique transactions or customers. Defaults to option column.column_id. |
None
|
target_item |
str | float | list[str | float] | None
|
A specific product or list of products to focus the association analysis on. If None, associations for all products are calculated. Defaults to None. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
df |
DataFrame
|
A DataFrame containing the calculated association rules and their metrics. |
Example
import pandas as pd transaction_df = pd.DataFrame({ ... 'customer_id': [1, 1, 2, 2, 3], ... 'product_id': ['A', 'B', 'B', 'C', 'A'] ... }) pa = ProductAssociation(df=transaction_df, value_col='product_id', group_col='customer_id') print(pa.df) # View the calculated association rules
Note
The resulting DataFrame (pa.df) contains the following columns: - product_1, product_2: The pair of products for which the association is calculated. - occurrences_1, occurrences_2: The number of transactions containing each product. - cooccurrences: The number of transactions containing both products. - support: The proportion of transactions containing both products. - confidence: The probability of buying product_2 given that product_1 was bought. - uplift: The ratio of the observed support to the expected support if the products were independent.
Source code in pyretailscience/analysis/product_association.py
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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
df: pd.DataFrame
property
Returns the executed DataFrame.
__init__(df, value_col, group_col=None, target_item=None, min_occurrences=1, min_cooccurrences=1, min_support=0.0, min_confidence=0.0, min_uplift=0.0)
Initialize the ProductAssociation object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
pd.DataFrame | ibis.Table)
|
The input DataFrame or ibis Table containing transaction data. |
required |
value_col |
str
|
The name of the column in the input DataFrame that contains the product identifiers. |
required |
group_col |
str
|
The name of the column that identifies unique transactions or customers. Defaults to option column.unit_spend. |
None
|
target_item |
str | float | list[str | float] | None
|
A specific product or list of products to focus the association analysis on. If None, associations for all products are calculated. Defaults to None. |
None
|
min_occurrences |
int
|
The minimum number of occurrences required for each product in the association analysis. Defaults to 1. Must be at least 1. |
1
|
min_cooccurrences |
int
|
The minimum number of co-occurrences required for the product pairs in the association analysis. Defaults to 1. Must be at least 1. |
1
|
min_support |
float
|
The minimum support value required for the association rules. Defaults to 0.0. Must be between 0 and 1. |
0.0
|
min_confidence |
float
|
The minimum confidence value required for the association rules. Defaults to 0.0. Must be between 0 and 1. |
0.0
|
min_uplift |
float
|
The minimum uplift value required for the association rules. Defaults to 0.0. Must be greater or equal to 0. |
0.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of combinations is not 2 or 3, or if any of the minimum values are invalid. |
ValueError
|
If the minimum support, confidence, or uplift values are outside the valid range. |
ValueError
|
If the minimum occurrences or cooccurrences are less than 1. |
ValueError
|
If the input DataFrame does not contain the required columns or if they have null values. |