Predict which customers will cancel

Your app already records which customers cancelled and what their accounts looked like before. Spavik learns from that history and gives each current customer a probability of cancelling, so your team can reach out first.

Returns
A probability of cancelling, per customer
Language
JavaScript

In your app

Northwind

78%

churn probability

Display the churn probability on the customer’s account page.

The data you need

One row per past case. Spavik learns how the inputs relate to the column you want to predict.

Past accounts, with plan, usage, support history, and whether each customer cancelled.

ColumnRoleMeaning
planInputThe customer’s plan
seatsInputNumber of seats
tickets_90dInputSupport tickets in the last 90 days
churnPredicted1 if the customer cancelled, otherwise 0

The code

A complete, commented script. Set SPAVIK_API_KEY in your server environment, never in browser code.

churn-prediction.ts JavaScript
import { readFile } from 'node:fs/promises';
import { Spavik } from 'spavik';
 
// Set SPAVIK_API_KEY in your server environment.
const spavik = new Spavik();
 
// Historical accounts: plan, seats, tickets_90d.
// churn = 1 if the customer cancelled, otherwise 0.
const history = JSON.parse(
  await readFile('accounts.json', 'utf8')
);
const report = await spavik.evaluate(history, {
  target: 'churn'
});
const { status, message } = report.assessment as {
  status: string; message: string;
};
if (status !== 'better_than_baseline') {
  throw new Error(message);
}
 
// Create once, then reuse this model for new accounts.
const model = await spavik.train(history, {
  target: 'churn'
});
 
// Northwind's current data, without the churn label.
const account = {
  plan: 'pro', seats: 12, tickets_90d: 3
};
const result = await model.predict(account);
 
// proba_1 is the probability of cancellation.
const risk = Math.round(Number(result.lignes[0].proba_1) * 100);
console.log('Northwind churn risk:', risk + '%');

How it works

  1. 1

    Check your data

    evaluate() tells you, for free, whether your history does better than a simple guess.

  2. 2

    Learn from your history

    train() creates the model once, for free. Keep it and reuse it for every new case.

  3. 3

    Ask for predictions

    predict() returns a value for each new row, from your server. One credit per row.

  4. 4

    Keep it accurate

    Report what actually happened with submit_outcomes(), check performance(), then refresh().

Questions

How much history do I need?

At least 10 rows, with at least 2 different values in the column you predict. More past accounts, including customers who stayed and customers who left, give the model more to learn from. evaluate() tells you for free whether your history does better than a simple guess before you train anything.

What does it cost?

Checking your data with evaluate() and creating the model with train() are free. Each predicted row costs one credit on the default engine. You train once and keep the model, so you pay only for the predictions you request.

Try it on your own data

Get an API key. Checking your data is free.