Product Associations
Market-basket (product association) rules from transaction data.
Rules are computed lazily in the Ibis engine: table is the lazy Ibis table and
df materializes it.
ProductAssociation
Association rules between products from transaction data.
The unit of analysis is the distinct (group, product) pair. Output columns are
{value_col}_1 / {value_col}_2 (the product pair), occurrences_1 /
occurrences_2 (count of distinct group_col values containing that product),
cooccurrences (distinct group_col values containing both), plus:
support = cooccurrences / total, wheretotalis the count of distinctgroup_colvalues over the ENTIRE input — not just groups containing either product.confidence = cooccurrences / occurrences_1= P(product_2 | product_1) — asymmetric.uplift = support / ((occurrences_1 / total) * (occurrences_2 / total)); 1 means the pair co-occurs as often as chance, above 1 more often.
With target_item=None both directions (a, b) and (b, a) are emitted, each with its
own confidence; with a given target_item only rows with the target as product_1 are
emitted. min_confidence applies to each direction separately, so (a, b) can be kept
while (b, a) is dropped.
table is the lazy Ibis result; df materializes it.
Source code in openretailscience/analysis/product_association.py
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 | |
df
cached
property
Materialized association rules DataFrame (cached after first execution).
__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 and compute the rules lazily.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Table
|
Transaction data with one row per (group, product) occurrence. |
required |
value_col
|
str
|
Column containing the product identifiers. |
required |
group_col
|
str | None
|
Column defining a basket. The default, option
|
None
|
target_item
|
str | int | list[str | int] | None
|
Product(s) to focus the analysis on; if None, associations for all products are calculated. Floats are rejected: an ID compared as a float matches only when its stored value is bit-for-bit equal. |
None
|
min_occurrences
|
int
|
Minimum distinct-group count per product; must be >= 1. Defaults to 1. |
1
|
min_cooccurrences
|
int
|
Minimum distinct-group count per pair; must be >= 1. Defaults to 1. |
1
|
min_support
|
float
|
Minimum support; must be in [0, 1]. Defaults to 0.0. |
0.0
|
min_confidence
|
float
|
Minimum confidence; must be in [0, 1]. Defaults to 0.0. |
0.0
|
min_uplift
|
float
|
Minimum uplift; must be >= 0. Defaults to 0.0. |
0.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any |
ValueError
|
If |
TypeError
|
If |
ValueError
|
If |