Omniscope Custom Blocks › Analytics/Prediction/KNN

K-Nearest-Neighbours

Predicts a category for each record from the k most similar records whose category is known (k-nearest neighbours), on numeric fields standardised first, with the share of neighbours that agree.

Analytics › Prediction R

Open the demo in Omniscope Download demo.ioz Sample data: known-customers.csv Sample data: new-customers.csv Source on GitHub

Omniscope custom block · Analytics · Prediction

Give new or unlabelled records the category of the known records they most resemble, with a vote share that shows how clear-cut each call is.

R · class · comes with R

The problem it solves

An account team has sorted 120 customers into three segments (bargain hunters, loyal regulars and big-ticket buyers) and wants every new customer sorted the same way, without a meeting for each one. The segments are not cut-offs on one number: bargain hunters and loyal regulars spend much the same; discount use tells them apart.

The block compares each new customer with the sorted ones on spend, orders and discount, and gives it the segment of its five closest matches. Standardising the three fields first stops spend, in hundreds of pounds, from drowning out discount, in single figures.

How it works

What goes in

Known customers (first input)

126 invented customers, 120 with a segment and 6 not yet assigned.

Customer Annual Spend Orders Per Year Average Discount Segment
C-0001 2645.93 4 7.3 Big-ticket buyer
C-0002 1130.70 22 23.2 Bargain hunter
C-0003 2339.16 5 1.7 Big-ticket buyer
C-0004 771.23 27 4.6 Loyal regular
C-0005 1001.28 27 9.1 Loyal regular
C-0006 2254.98 3 6.0 Big-ticket buyer

126 rows in the sample file: 39 bargain hunters, 36 big-ticket buyers, 45 loyal regulars and 6 with no segment.

New customers (second input)

15 invented customers with the same three measures and no segment.

The options

Option Demo setting What it does
Field to predict Segment The category to predict.
Use all numeric fields ticked Predict from Annual Spend, Orders Per Year and Average Discount.
Fields to predict from (not used) The fields to use when "Use all numeric fields" is unticked.
Number of neighbours 5 Five closest known customers vote.
Standardise fields ticked Put the three fields on the same scale first.

What comes out

Predicted (new customers, first 8)

Customer Annual Spend Orders Per Year Average Discount Predicted (Segment) Vote share
N-0001 2858.21 4 19.0 Big-ticket buyer 1.0
N-0002 1353.37 18 17.3 Bargain hunter 0.8
N-0003 1048.49 21 22.5 Bargain hunter 1.0
N-0004 1391.32 26 6.3 Loyal regular 1.0
N-0005 678.54 24 23.6 Bargain hunter 1.0
N-0006 1260.71 16 9.6 Loyal regular 1.0
N-0007 2496.93 6 13.7 Big-ticket buyer 1.0
N-0008 3473.15 4 12.3 Big-ticket buyer 1.0

Five new customers are bargain hunters, six loyal regulars and four big-ticket buyers; 13 of the 15 calls are unanimous.

With the first input alone

Connect only the known customers and every record gets a prediction. The six unsorted customers get one each (C-0009 and C-0077 big-ticket buyers, C-0031 a loyal regular, the other three bargain hunters, all 5 votes to 0). For the 120 sorted customers the prediction ignores their own segment; it matches for 117. The three that differ (C-0024, C-0028, C-0103) have discounts of 14 to 16 %, between the two groups, and vote shares of 0.6 to 0.8: worth a second look by the team.

Where it pays off

Sales and marketing: Segmenting new customers

Give each new account the segment of the existing accounts it most resembles, and review only the close calls.

Operations: Fault or job types

Classify new readings, tickets or jobs by the known cases with the most similar measurements.

Quality: Grading

Grade items by their measurements against a set of items already graded by an inspector.

Data cleaning: Filling gaps

With one input, fill in a missing category from similar records, and see from the known ones how reliable that is.

Use cases describe typical scenarios and are illustrative; the sample data is invented.

Why do this in Omniscope

How it fits into an Omniscope workflow

Sorted customers (Database) + New customers (Database) → K-Nearest-Neighbours → Report

For an accuracy check, run it on the first input alone and feed the output to the Model Validation block (Ground Truth: Segment, Prediction: Predicted (Segment)), or to an Aggregate on both fields.

Try it

  1. Open the demo project (demo.iox on the demo server, or import demo.ioz from this folder): 126 known customers and 15 new ones.
  2. The question: which segment does each new customer belong to?
  3. Open K-Nearest-Neighbours: field to predict Segment, use all numeric fields, 5 neighbours, standardise ticked. Run it.
  4. Look at the output: 15 new customers, each with a predicted segment and a vote share; N-0002 and N-0014 are 4 to 1 calls.
  5. Untick Standardise fields and run again: four of the 15 change segment, because spend now decides almost alone.

Running in Omniscope

Omniscope workflow
Omniscope workflow

The demo project's workflow: the sample data, the block, and a Validate data check on its output.

Language and libraries

R. It uses the class package (GPL-2 or GPL-3), one of R's recommended packages that come with most R installations; install it with install.packages("class") if it is missing.

Tested

The block is covered by automated tests that run its script against a stand-in for Omniscope's R API: the sample data with two inputs and with one, leave-one-out predictions, standardised and raw fields, tied votes on repeated runs, records with missing values, chosen and Integer fields, empty inputs, bad choices (no field to predict, a numeric field to predict, text fields to predict from, missing fields, too many neighbours, no known records, no input) and 20,000 records on each input.

Reference

Fields it reads

NameTypeWhat it does
Field to predictFieldThe Text field holding the category to predict, in the first input. Records where it is empty are not used to learn from; they are predicted.
Fields to predict fromFieldsThe numeric fields that measure how similar two records are. Used when "Use all numeric fields" is unticked. A second input must have fields with the same names.

Options

NameTypeWhat it does
Use all numeric fieldsYes / noPredict from every Integer and Decimal field in the first input. Untick to choose the fields in "Fields to predict from".
Number of neighboursWhole numberThe k in k-nearest neighbours: how many of the most similar known records vote on each prediction. Odd numbers avoid some ties.
Standardise fieldsYes / noRescale each field to mean 0 and standard deviation 1 (from the known records) before measuring similarity, so a field measured in thousands does not outweigh one measured in single figures.

Outputs

OutputWhat it holds
PredictedThe second input (or, with one input, the first) with Predicted (field) and Vote share; both empty where a field to predict from is missing.