Customer Tiering — Get Scoring Script (large books)
customer_tiering_get_engineFor books LARGER than 500 transaction rows. Returns a complete, runnable Python script that scores the book into A/B/C/D tiers with survival modelling (BG/NBD), spend modelling (Gamma-Gamma), tier migration, a money layer and plain-language decision cards. Run it in your code sandbox against the user's transaction file. The rows never pass through you as tokens, so a 10,000-row book costs the same to run as a 600-row one. Needs numpy. Prints ranked decisions and headline figures; writes the full per-customer ledger to customer_tiering_result.json beside the input file. No customer data reaches this server on this path. SAVE AND RUN THE RETURNED SCRIPT VERBATIM — every block of it is required for the computation. Do not retype it from memory, shorten it, reformat it, split it up, or reimplement the maths with pandas/sklearn; only the PATH / AS_OF / CURRENCY / OUT / OVERRIDES / CONTACTS lines at the bottom may be edited. The script cleans customer identities itself before scoring — merging capitalisation and spelling variants by rule, printing what it merged, and listing the similar-but-unproven groups for you to rule on via OVERRIDES — so do not pre-clean the file or edit those rules. Optionally takes contacts_path, a log of rep calls or visits (customer_id + date only). It is not required and the book scores fine without it, but it is valuable: with it the money layer MEASURES what a contact is worth per tier from touched-vs-untouched tier transitions instead of assuming a flat rate, so ask for it whenever the user mentions a CRM, a call log or a visit register.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| as_of | No | Analysis date YYYY-MM-DD. Defaults to latest in file. | |
| currency | No | ISO currency code for display (INR, USD, EUR...). | USD |
| file_path | Yes | Path to the transaction file (CSV/TSV/JSON) inside your sandbox. The file needs one row per PURCHASE with three things: a customer identifier, a date, and an amount > 0. Column names are matched flexibly (customer_id / AccountId / Party Name; date / invoice_date / Invoice Date; amount / revenue / Invoice Amount), so most CRM and Excel exports work unchanged, including a separate name column used to label accounts when the id is a code. A pre-aggregated per-customer summary will NOT work — the models need purchase timing. The script cleans name variants itself ('Acme Pvt Ltd' and 'ACME PVT LTD' become one account), so pass the file as it is. | |
| contacts_path | No | Optional path to a rep-contact log (CSV/TSV/JSON) inside your sandbox: one row per call, visit or WhatsApp touch, needing only a customer identifier and a date — no amount. Column names are matched as leniently as the transaction file, and the ids go through the same identity cleaning, so a log spelling 'ACME PVT LTD.' still joins 'Acme Pvt Ltd'. Strictly optional; without it the book scores exactly as it would anyway. With it, what a contact is worth stops being an assumed flat rate and is MEASURED per tier by comparing tier transitions that followed a contact against those that did not (any tier with at least 10 touched transitions; thinner tiers keep the assumption). Worth asking for whenever the user mentions a CRM, a call log or a visit register. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| notes | No | ||
| script | Yes | The runnable scoring script. | |
| currency | No | ||
| language | No | ||
| requires | No | ||
| writes_file | No | ||
| instructions | Yes | ||
| code_integrity | Yes | Rules for running the script: it must be saved and executed verbatim, which lines may be edited, why every block matters, and how to verify the copy is intact. | |
| engine_version | Yes | ||
| file_path_used | No | ||
| scipy_required | No | ||
| data_requirements | No | What the input file must contain: required fields and their accepted aliases, formats handled, minimum data volumes, and what will not work. | |
| identity_cleaning | Yes | How the script cleans customer identities before scoring: which variants it merges by rule, which similar-but-unproven groups it leaves for the caller to judge and how to feed that decision back via OVERRIDES, how accounts get their display names, and what it writes out. | |
| contacts_path_used | No | ||
| network_access_required | No |