NLR (New-Lapsed-Repeating) Segmentation
New/Lapsed/Repeating (NLR) customer-lifecycle segmentation across two periods.
A customer counts as active in a period only with a strictly positive aggregated value; customers inactive in both periods are excluded from the results.
NLRSegmentation
Segments customers into New, Repeating, and Lapsed based on activity across two periods.
A customer is active in a period only if their aggregated value is strictly positive (> 0); zero or negative values do not count: - New: active in P2 only - Repeating: active in both P1 and P2 - Lapsed: active in P1 only
With count-like aggregations ("count", "nunique") the positive-value rule measures transaction presence rather than spend.
Attributes:
| Name | Type | Description |
|---|---|---|
table |
Table
|
The underlying lazy ibis Table expression; further ibis operations can be applied before materializing. |
df |
DataFrame
|
The materialized results, indexed by customer_id (and group_col if specified). Cached after first access. |
Source code in openretailscience/segmentation/nlr.py
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 | |
df
cached
property
Returns the DataFrame with segment names, indexed by customer_id (and group_col if specified).
Columns: segment_name, {value_col}_p1 and {value_col}_p2.
__init__(df, period_col, p1_value, p2_value, value_col=None, agg_func='sum', group_col=None)
Segments customers into New, Repeating, and Lapsed based on positive aggregated value.
A customer is active in a period only if their aggregated value_col is strictly positive (> 0): - New: positive value in P2 only - Repeating: positive value in both P1 and P2 - Lapsed: positive value in P1 only Customers with no positive value in either period are excluded from the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Table
|
Transaction data. Must contain customer_id, period_col, and value_col columns. |
required |
period_col
|
str
|
Column containing period identifiers. |
required |
p1_value
|
str | float | Scalar
|
Value in period_col identifying period 1. |
required |
p2_value
|
str | float | Scalar
|
Value in period_col identifying period 2. |
required |
value_col
|
str | None
|
Column to aggregate for determining customer activity. Defaults to ColumnHelper().unit_spend. |
None
|
agg_func
|
str
|
Aggregation function: one of "sum", "mean", "max", "count", "nunique". Defaults to "sum". |
'sum'
|
group_col
|
str | list[str] | None
|
Column(s) to group by when calculating segments. When specified, segments are calculated within each group independently. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If required columns are missing, agg_func is unsupported, or p1_value equals p2_value. |
TypeError
|
If agg_func is not a string. |
Source code in openretailscience/segmentation/nlr.py
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 | |