Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("should make test number 33", function () {
// 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 });
});
});
it("should make test number 35", function () {
// 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);
});
});
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);
});
});
it("should make test number 37", function () {
// 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);
});
});
it("should make test number 40", function () {
// 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
});
});
it('should get the parameters of the default object', function () {
var neighbor = new analytics.NearestNeighborAD();
var params = neighbor.getParams();
assert.equal(params.rate[0], [0.05]);
})
it('should get the parameters of the object', function () {
it('should explain differences for the given vector', function () {
var neighbor = new analytics.NearestNeighborAD({ windowSize: 3 });
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]], [[0, 0], [1, 1]]]);
var idVec = new la.IntVector([1, 2, 3]);
neighbor.fit(matrix, idVec);
var vector = new la.SparseVector([[1, 4]]);
var explain = neighbor.explain(vector);
assert.equal(explain.nearestDat, 1);
assert.equal(explain.distance, 5);
assert.equal(explain.features[0].id, 0);
assert.equal(explain.features[0].val, 0);
assert.equal(explain.features[0].nearVal, 1);
assert.equal(explain.features[0].contribution, 0.2);
assert.equal(explain.features[1].id, 1);
assert.equal(explain.features[1].val, 4);
assert.equal(explain.features[1].nearVal, 2);
assert.equal(explain.features[1].contribution, 0.8);
it('should not throw an exception', function () {
var neighbor = new analytics.NearestNeighborAD({ windowSize: 2 });
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]]]);
neighbor.fit(matrix);
var vector = new la.SparseVector([[0, 0], [1, 1]]);
assert.doesNotThrow(function () {
neighbor.partialFit(vector);
});
})
it('should update the model and return the threshold equal to 8, vector input', function () {
it('should set the parameters of the default object', function () {
var neighbor = new analytics.NearestNeighborAD();
neighbor.setParams({ rate: [0.1] });
var params = neighbor.getParams();
assert.equal(params.rate[0], 0.1);
})
it('should set the parameters of the object', function () {
it('should update the model and return the threshold equal to 8, vector input', function () {
var neighbor = new analytics.NearestNeighborAD({ windowSize: 2 });
var matrix = new la.SparseMatrix([[[0, 1], [1, 2]], [[0, -2], [1, 3]]]);
neighbor.fit(matrix);
var vector = new la.SparseVector([[0, 0], [1, 1]]);
neighbor.partialFit(vector);
var model = neighbor.getModel();
assert.eqtol(model.threshold, 8);
})
});