Property

new NearestNeighborAD([arg])

Nearest Neighbour Anomaly Detection

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD({ rate: 0.1 });
// create a sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create a new sparse vector
var vector = new la.SparseVector([[0, 4], [1, 0]]);
// predict if the vector is an anomaly or not
var prediction = neighbor.predict(vector);

Parameter

Name Type Optional Description

arg

(module:analytics~detectorParam or module:fs.FIn)

Yes

Construction arguments. There are two ways of constructing:
1. Using the module:analytics~detectorParam object,
2. using the file input stream module:fs.FIn.

Property

init

Returns true when the model has enough data to initialize. Type boolean.

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD({ rate:0.05, windowSize:3 });
// check if the model has enough data
neighbor.init;

Methods

decisionFunction(x) → number

Compares the point to the known points and returns distance to the nearest one.

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create a new sparse vector
var vector = new la.SparseVector([[0, 4], [1, 0]]);
// get the distance of the vector from the model
var prediction = neighbor.decisionFunction(vector); // returns 1

Parameter

Name Type Optional Description

x

module:la.Vector

 

Test vector.

Returns

numberB Distance to the nearest point.

explain(x) → module:analytics~NearestNeighborADExplain

Returns an object that encodes the ID of the nearest neighbor and the features that contributed to the distance.

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD({ rate:0.05, windowSize:3 });
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix and provide a vector record IDs
neighbor.fit(matrix, new la.IntVector([3541, 1112, 4244]));
// create a new sparse vector
var vector = new la.SparseVector([[0, 4], [1, 0]]);
// check if the vector is an anomaly
var explanation = neighbor.explain(vector); // returns an explanation

Parameter

Name Type Optional Description

x

module:la.SparseVector

 

Test vector.

Returns

module:analytics~NearestNeighborADExplainB The explanation object.

fit(A[, idVec]) → module:analytics.NearestNeighborAD

Analyzes the nearest neighbor distances and calculates the detector threshold based on the rate parameter.

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);

Parameters

Name Type Optional Description

A

module:la.SparseMatrix

 

Matrix whose columns correspond to known examples. Gets saved as it is part of the model.

idVec

module:la.IntVector

Yes

An integer vector of IDs.

Returns

module:analytics.NearestNeighborADB Self. The model is set by the matrix A.

getModel() → Object

Returns the model.

Example

// import analytics module
var analytics = require('qminer').analytics;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD({ rate: 0.1 });
// get the model of the object
// returns a json object { rate: 0.1, window: 0 }
var model = neighbor.getModel();
Returns

ObjectB The object neighbourModel containing the properties:
1. neighbourModel.rate - The expected fraction of emmited anomalies.
2. neighbourModel.thresh - Maximal squared distance to the nearest neighbor that is not anomalous.

getParams() → module:analytics~detectorParam

Gets parameters.

Example

// import analytics module
var analytics = require('qminer').analytics;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// get the parameters of the object
// returns a json object { rate: 0.05 }
var params = neighbor.getParams();
Returns

module:analytics~detectorParamB The object containing the parameters.

partialFit(X, recId) → module:analytics.NearestNeighborAD

Adds a new point to the known points and recalculates the threshold.

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create a new sparse vector
var vector = new la.SparseVector([[0, 2], [1, 5]]);
// update the model with the vector
neighbor.partialFit(vector);

Parameters

Name Type Optional Description

X

module:la.SparseVector

 

Test example.

recId

number

 

Integer record ID, used in module:analytics.NearestNeighborAD.prototype.explain.

Returns

module:analytics.NearestNeighborADB Self. The model is updated.

predict(x) → number

Compares the point to the known points and returns 1 if it's too far away (based on the precalculated threshold).

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create a new sparse vector
var vector = new la.SparseVector([[0, 4], [1, 0]]);
// check if the vector is an anomaly
var prediction = neighbor.predict(vector); // returns 1

Parameter

Name Type Optional Description

x

module:la.SparseVector

 

Test vector.

Returns

numberB Returns 1.0 if the vector x is an anomaly and 0.0 otherwise.

save(fout) → module:fs.FOut

Saves model to provided output stream.

Example

// import modules
var analytics = require('qminer').analytics;
var la = require('qminer').la;
var fs = require('qminer').fs;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// create a new sparse matrix
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
// fit the model with the matrix
neighbor.fit(matrix);
// create an output stream object and save the model
var fout = fs.openWrite('neighbor_example.bin');
neighbor.save(fout);
fout.close();
// create a new Nearest Neighbor Anomaly model by loading the model
var fin = fs.openRead('neighbor_example.bin');
var neighbor2 = new analytics.NearestNeighborAD(fin);

Parameter

Name Type Optional Description

fout

module:fs.FOut

 

The output stream.

Returns

module:fs.FOutB The output stream fout.

setParams(params) → module:analytics.NearestNeighborAD

Sets parameters.

Example

// import analytics module
var analytics = require('qminer').analytics;
// create a new NearestNeighborAD object
var neighbor = new analytics.NearestNeighborAD();
// set it's parameters to rate: 0.1
neighbor.setParams({ rate: 0.1 });

Parameter

Name Type Optional Description

params

module:analytics~detectorParam

 

The object containing the parameters.

Returns

module:analytics.NearestNeighborADB Self. The parameters are updated with params.