qminer
Version:
A C++ based data analytics platform for processing large-scale real-time streams containing structured and unstructured data
926 lines (907 loc) • 66.7 kB
JavaScript
/**
* Copyright (c) 2015, Jozef Stefan Institute, Quintelligence d.o.o. and contributors
* All rights reserved.
*
* This source code is licensed under the FreeBSD license found in the
* LICENSE file in the root directory of this source tree.
*/
// JavaScript source code
var qm = require('../../index.js');
var la = qm.la;
var analytics = qm.analytics;
var assert = require("../../src/nodejs/scripts/assert.js");
//Unit test for Kmeans
describe("Kmeans test", function () {
describe("Constructor test", function () {
it("should return empty parameter values", function () {
var KMeans = new analytics.KMeans();
var params = KMeans.getParams();
assert.strictEqual(Object.keys(params).length, 7);
});
it("should return parameter values", function () {
var KMeans = new analytics.KMeans({ iter: 100, k: 2, verbose: false });
var params = KMeans.getParams();
assert.strictEqual(params.iter, 100);
assert.strictEqual(params.k, 2);
assert.strictEqual(params.distanceType, "Euclid");
assert.strictEqual(params.centroidType, "Dense");
assert.strictEqual(params.calcDistQual, false);
assert.strictEqual(params.verbose, false);
});
it("should return the custom KMeans", function () {
var KMeans = new analytics.KMeans({ iter: 20000, k: 100, verbose: true, distanceType: "Cos", centroidType: "Sparse" });
var params = KMeans.getParams();
assert.strictEqual(params.iter, 20000);
assert.strictEqual(params.k, 100);
assert.strictEqual(params.distanceType, "Cos");
assert.strictEqual(params.centroidType, "Sparse");
assert.strictEqual(params.verbose, true);
});
it("should return the custom KMeans with fitIdx", function () {
var KMeans = new analytics.KMeans({ iter: 20000, k: 2, verbose: true, distanceType: "Cos", centroidType: "Sparse", fitIdx: [5, 2], calcDistQual: true });
var params = KMeans.getParams();
assert.strictEqual(params.iter, 20000);
assert.strictEqual(params.k, 2);
assert.strictEqual(params.distanceType, "Cos");
assert.strictEqual(params.centroidType, "Sparse");
assert.strictEqual(params.calcDistQual, true);
assert.strictEqual(params.verbose, true);
assert.strictEqual(params.fitIdx[0], 5);
assert.strictEqual(params.fitIdx[1], 2);
});
});
describe("Testing getParams and setParams", function () {
it("should return the changed values of parameters", function () {
var KMeans = new analytics.KMeans();
KMeans.setParams({ iter: 15, k: 30, verbose: true });
var params = KMeans.getParams();
assert.strictEqual(params.iter, 15);
assert.strictEqual(params.k, 30);
assert.strictEqual(params.verbose, true);
});
it("should return the same parameters used for the construction", function () {
var KMeans = new analytics.KMeans({ iter: 100, k: 20, fitIdx: [0, 1, 2] });
var params = KMeans.getParams();
assert.strictEqual(params.iter, 100);
assert.strictEqual(params.k, 20);
assert.strictEqual(params.verbose, false);
assert.strictEqual(params.fitIdx[0], 0);
assert.strictEqual(params.fitIdx[1], 1);
assert.strictEqual(params.fitIdx[2], 2);
});
it("should return the same parameters used for the construction", function () {
var mat = new la.Matrix([[0, 1], [2, 3]]);
var KMeans = new analytics.KMeans({ iter: 100, k: 20, fitStart: { C: mat } });
var params = KMeans.getParams();
assert.strictEqual(params.iter, 100);
assert.strictEqual(params.k, 20);
assert.strictEqual(params.verbose, false);
assert.strictEqual(params.fitStart.C.rows, 2);
assert.strictEqual(params.fitStart.C.cols, 2);
assert.strictEqual(params.fitStart.C.at(0, 0), 0);
assert.strictEqual(params.fitStart.C.at(0, 1), 1);
assert.strictEqual(params.fitStart.C.at(1, 0), 2);
assert.strictEqual(params.fitStart.C.at(1, 1), 3);
});
it("should return the changed values of parameters even if added keys", function () {
var KMeans = new analytics.KMeans();
KMeans.setParams({ iter: 10, k: 5, alpha: false });
var params = KMeans.getParams();
assert.strictEqual(params.iter, 10);
assert.strictEqual(params.k, 5);
assert.strictEqual(params.verbose, false);
});
});
describe("Fit test", function () {
it("should create the model for dense centroids, dense matrix", function () {
var KMeans = new analytics.KMeans();
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with fitIdx, dense matrix", function () {
var KMeans = new analytics.KMeans({ fitIdx: [0, 1] });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with dense fitStart, dense matrix", function () {
var centroids = new la.Matrix({ rows: 2, cols: 2, random: true });
var KMeans = new analytics.KMeans({ fitStart: { C: centroids } });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with sparse fitStart, dense matrix", function () {
var centroids = new la.SparseMatrix([[[1, 1]], [[0, -1]]]);
var KMeans = new analytics.KMeans({ fitStart: { C: centroids } });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids, sparse matrix", function () {
var KMeans = new analytics.KMeans();
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with fitIdx, sparse matrix", function () {
var KMeans = new analytics.KMeans({ fitIdx: [0, 1] });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with dense fitStart, sparse matrix", function () {
var KMeans = new analytics.KMeans({ fitStart: { C: new la.Matrix({ rows: 11, cols: 2, random: true }) } });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with sparse fitStart, sparse matrix", function () {
var KMeans = new analytics.KMeans({ fitStart: { C: new la.SparseMatrix([[[0, 1]], [[11, 3]]]) }});
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
})
it("should return the correct model for dense centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, calcDistQual: true });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
assert(KMeans.relMeanCentroidDist != null);
});
it("should return the correct model for dense centroids with fitIdx, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, fitIdx: [0, 1] });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with dense fitStart, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, fitStart: { C: new la.Matrix({ rows: 2, cols: 2, random: true }) } });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with sparse fitStart, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, fitStart: { C: new la.SparseMatrix([[[0, 1]], [[1, 3]]]) } });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 2 });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 11);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with fitIdx, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, fitIdx: [0, 1] });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 11);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with dense fitStart, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, fitStart: { C: new la.Matrix({ rows: 2, cols: 2, random: true }) } });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with sparse fitStart, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, fitStart: { C: new la.SparseMatrix([[[0, 1]], [[11, 3]]]) } });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 12);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 2 });
var matrix = new la.Matrix([[1, 2, 3, -10, -10, 4, 2, -10], [7, 6, 5, 5, -5, -1, -3, 0]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 8);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with fitIdx, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, fitIdx: [0, 1] });
var matrix = new la.Matrix([[1, 2, 3, -10, -10, 4, 2, -10], [7, 6, 5, 5, -5, -1, -3, 0]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 8);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the same model even after changing parameter values, dense centroids", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var model = KMeans.getModel();
assert.deepEqual(model.C, X);
KMeans.setParams({ iter: 102 });
var model2 = KMeans.getModel();
assert.deepEqual(model2.C, X);
});
it("should create the model for sparse centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ centroidType: "Sparse" });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with fitIdx, dense matrix", function () {
var KMeans = new analytics.KMeans({ centroidType: "Sparse", fitIdx: [0, 1] });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with dense fitStart, dense matrix", function () {
var KMeans = new analytics.KMeans({ iter: 100, k: 2, centroidType: "Sparse", fitStart: { C: new la.Matrix([[0, 1], [2, 3]]) } });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with sparse fitStart, dense matrix", function () {
var KMeans = new analytics.KMeans({ iter: 100, k: 2, centroidType: "Sparse", fitStart: { C: new la.SparseMatrix([[[0, 1]], [[1, 3]]]) } });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids, sparse matrix", function () {
var KMeans = new analytics.KMeans({ centroidType: "Sparse" });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with fitIdx, sparse matrix", function () {
var KMeans = new analytics.KMeans({ centroidType: "Sparse", fitIdx: [0, 1] });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with dense fitStart, sparse matrix", function () {
var KMeans = new analytics.KMeans({ iter: 100, k: 2, centroidType: "Sparse", fitStart: { C: new la.Matrix([[0, 1], [3, 3]]) } });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with sparse fitStart, sparse matrix", function () {
var KMeans = new analytics.KMeans({ iter: 100, k: 2, centroidType: "Sparse", fitStart: { C: new la.SparseMatrix([[[0, 1]], [[1, 3]]]) } });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should return the correct model for sparse centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse" });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, -1);
assert.strictEqual(model.C.cols, 3);
});
it("should return the correct model for sparse centroids with fitIdx, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse", fitIdx: [0, 1, 2] });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, -1);
assert.strictEqual(model.C.cols, 3);
});
it("should return the correct model for sparse centroids, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, centroidType: "Sparse" });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, -1);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for sparse centroids with fitIdx, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 2, centroidType: "Sparse", fitIdx: [0, 1] });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, -1);
assert.strictEqual(model.C.cols, 2);
});
it("should return the same model even after changing parameter values, sparse centroids", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse" });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var model = KMeans.getModel();
assert.deepEqual(model.C.rows, -1);
assert.deepEqual(model.C.cols, 3);
KMeans.setParams({ iter: 102 });
var model2 = KMeans.getModel();
assert.deepEqual(model2.C.rows, -1);
assert.deepEqual(model2.C.cols, 3);
});
it("should throw an exception if k and length of fitIdx are not equal, dense centroids", function () {
var KMeans = new analytics.KMeans({ k: 3, fitIdx: [0, 1] });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
assert.throws(function () {
KMeans.fit(X);
});
});
it("should throw an exception if maximum value of fitIdx is greater that number of columns of matrix, dense centroids", function () {
var KMeans = new analytics.KMeans({ k: 2, fitIdx: [0, 5] });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
assert.throws(function () {
KMeans.fit(X);
});
});
it("should throw an exception if maximum value of fitIdx is greater that number of columns of matrix, sparse centroids", function () {
var KMeans = new analytics.KMeans({ k: 2, centroidType: "Sparse", fitIdx: [0, 5] });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
assert.throws(function () {
KMeans.fit(X);
});
});
// distanceType: "Cos"
it("should create the model for dense centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos" });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with fitIdx, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", fitIdx: [0, 1] });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with dense fitStart, dense matrix, distanceType Cos", function () {
var centroids = new la.Matrix({ rows: 2, cols: 2, random: true });
var KMeans = new analytics.KMeans({ distanceType: "Cos", fitStart: { C: centroids } });
var matrix = new la.Matrix([[-1, 1], [0, 1]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with sparse fitStart, dense matrix, distanceType Cos", function () {
var centroids = new la.SparseMatrix([[[1, 1]], [[0, -1]]]);
var KMeans = new analytics.KMeans({ distanceType: "Cos", fitStart: { C: centroids } });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos" });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with fitIdx, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", fitIdx: [0, 1] });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with dense fitStart, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", fitStart: { C: new la.Matrix({ rows: 11, cols: 2, random: true }) } });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for dense centroids with sparse fitStart, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", fitStart: { C: new la.SparseMatrix([[[0, 1]], [[11, 3]]]) } });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
})
it("should return the correct model for dense centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", distanceType: "Cos", k: 2 });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with fitIdx, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, fitIdx: [0, 1] });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with dense fitStart, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, fitStart: { C: new la.Matrix({ rows: 2, cols: 2, random: true }) } });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with sparse fitStart, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, fitStart: { C: new la.SparseMatrix([[[0, 1]], [[1, 3]]]) } });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2 });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 11);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with fitIdx, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, fitIdx: [0, 1] });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 11);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with dense fitStart, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, fitStart: { C: new la.Matrix({ rows: 2, cols: 2, random: true }) } });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with sparse fitStart, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, fitStart: { C: new la.SparseMatrix([[[0, 1]], [[11, 3]]]) } });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, 12);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2 });
var matrix = new la.Matrix([[1, 2, 3, -10, -10, 4, 2, -10], [7, 6, 5, 5, -5, -1, -3, 0]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 8);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for dense centroids with fitIdx, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, fitIdx: [0, 1] });
var matrix = new la.Matrix([[1, 2, 3, -10, -10, 4, 2, -10], [7, 6, 5, 5, -5, -1, -3, 0]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 8);
assert.strictEqual(model.C.rows, 2);
assert.strictEqual(model.C.cols, 2);
});
it("should return the same model even after changing parameter values, dense centroids, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var model = KMeans.getModel();
assert.deepEqual(model.C, X);
KMeans.setParams({ iter: 102 });
var model2 = KMeans.getModel();
assert.deepEqual(model2.C, X);
});
it("should create the model for sparse centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", centroidType: "Sparse" });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with fitIdx, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", centroidType: "Sparse", fitIdx: [0, 1] });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with dense fitStart, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", iter: 100, k: 2, centroidType: "Sparse", fitStart: { C: new la.Matrix([[0, 1], [2, 3]]) } });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with sparse fitStart, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", iter: 100, k: 2, centroidType: "Sparse", fitStart: { C: new la.SparseMatrix([[[0, 1]], [[1, 3]]]) } });
var matrix = new la.Matrix([[-1, 1], [0, 0]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", centroidType: "Sparse" });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with fitIdx, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", centroidType: "Sparse", fitIdx: [0, 1] });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with dense fitStart, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", iter: 100, k: 2, centroidType: "Sparse", fitStart: { C: new la.Matrix([[0, 1], [3, 3]]) } });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should create the model for sparse centroids with sparse fitStart, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", iter: 100, k: 2, centroidType: "Sparse", fitStart: { C: new la.SparseMatrix([[[0, 1]], [[1, 3]]]) } });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]]]);
assert.doesNotThrow(function () {
KMeans.fit(matrix);
});
});
it("should return the correct model for sparse centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 3, centroidType: "Sparse" });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, -1);
assert.strictEqual(model.C.cols, 3);
});
it("should return the correct model for sparse centroids with fitIdx, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 3, centroidType: "Sparse", fitIdx: [0, 1, 2] });
var matrix = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, -1);
assert.strictEqual(model.C.cols, 3);
});
it("should return the correct model for sparse centroids, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, centroidType: "Sparse" });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, -1);
assert.strictEqual(model.C.cols, 2);
});
it("should return the correct model for sparse centroids with fitIdx, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, centroidType: "Sparse", fitIdx: [0, 1] });
var matrix = new la.SparseMatrix([[[0, 1], [10, 2]], [[2, 3]], [[4, -1]]]);
KMeans.fit(matrix);
var model = KMeans.getModel();
assert.strictEqual(model.idxv.length, 3);
assert.strictEqual(model.C.rows, -1);
assert.strictEqual(model.C.cols, 2);
});
it("should return the same model even after changing parameter values, sparse centroids, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 3, centroidType: "Sparse" });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var model = KMeans.getModel();
assert.deepEqual(model.C.rows, -1);
assert.deepEqual(model.C.cols, 3);
KMeans.setParams({ iter: 102 });
var model2 = KMeans.getModel();
assert.deepEqual(model2.C.rows, -1);
assert.deepEqual(model2.C.cols, 3);
});
it("should throw an exception if k and length of fitIdx are not equal, dense centroids, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 3, fitIdx: [0, 1] });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
assert.throws(function () {
KMeans.fit(X);
});
});
it("should throw an exception if maximum value of fitIdx is greater that number of columns of matrix, dense centroids, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, fitIdx: [0, 5] });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
assert.throws(function () {
KMeans.fit(X);
});
});
it("should throw an exception if maximum value of fitIdx is greater that number of columns of matrix, sparse centroids, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ distanceType: "Cos", k: 2, centroidType: "Sparse", fitIdx: [0, 5] });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
assert.throws(function () {
KMeans.fit(X);
});
});
});
describe("Predict Tests", function () {
it("should not throw an exception dense centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1], [0, -1]]);
assert.doesNotThrow(function () {
KMeans.predict(matrix);
});
});
it("should not throw an exception dense centroids, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[1, 1]], [[0, -1]]]);
assert.doesNotThrow(function () {
KMeans.predict(matrix);
});
});
it("should return the predictions of the matrix dense centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[-1, 2, 1], [0, 1, -3]]);
var prediction = KMeans.predict(matrix);
assert.strictEqual(prediction.length, 3);
});
it("should return the predictions of the matrix dense centroids, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[0, 1]], [[1, 1]], [[0, 2], [1, 4]]]);
var prediction = KMeans.predict(matrix);
assert.strictEqual(prediction.length, 3);
});
it("should throw an exception if the matrix has less rows dense centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1]]);
assert.throws(function () {
KMeans.predict(matrix);
});
});
it("should throw an exception if the matrix has too many rows dense centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1], [0, -1], [0, 0]]);
assert.throws(function () {
KMeans.predict(matrix);
});
});
it("should throw an exception if the matrix has too many rows dense centroids, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[4, 1]]]);
assert.throws(function () {
KMeans.predict(matrix);
});
});
it("should not throw an exception sparse centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse" });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1], [0, -1]]);
assert.doesNotThrow(function () {
KMeans.predict(matrix);
});
});
it("should not throw an exception sparse centroids, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse" });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[1, 1]], [[0, -1]]]);
assert.doesNotThrow(function () {
KMeans.predict(matrix);
});
});
it("should return the predictions of the matrix sparse centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse" });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[-1, 2, 1], [0, 1, -3]]);
var prediction = KMeans.predict(matrix);
assert.strictEqual(prediction.length, 3);
});
it("should return the predictions of the matrix sparse centroids, sparse matrix", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse" });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[0, 1]], [[1, 1]], [[0, 2], [1, 4]]]);
var prediction = KMeans.predict(matrix);
assert.strictEqual(prediction.length, 3);
});
it("should throw an exception if the matrix has less rows sparse centroids, dense matrix", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse" });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1]]);
assert.throws(function () {
KMeans.predict(matrix);
});
});
// distanceType: 'Cos'
it("should not throw an exception dense centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1], [0, -1]]);
assert.doesNotThrow(function () {
KMeans.predict(matrix);
});
});
it("should not throw an exception dense centroids, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[1, 1]], [[0, -1]]]);
assert.doesNotThrow(function () {
KMeans.predict(matrix);
});
});
it("should return the predictions of the matrix dense centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[-1, 2, 1], [0, 1, -3]]);
var prediction = KMeans.predict(matrix);
assert.strictEqual(prediction.length, 3);
});
it("should return the predictions of the matrix dense centroids, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[0, 1]], [[1, 1]], [[0, 2], [1, 4]]]);
var prediction = KMeans.predict(matrix);
assert.strictEqual(prediction.length, 3);
});
it("should throw an exception if the matrix has less rows dense centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1]]);
assert.throws(function () {
KMeans.predict(matrix);
});
});
it("should throw an exception if the matrix has too many rows dense centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1], [0, -1], [0, 0]]);
assert.throws(function () {
KMeans.predict(matrix);
});
});
it("should throw an exception if the matrix has too many rows dense centroids, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[4, 1]]]);
assert.throws(function () {
KMeans.predict(matrix);
});
});
it("should not throw an exception sparse centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse", distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1], [0, -1]]);
assert.doesNotThrow(function () {
KMeans.predict(matrix);
});
});
it("should not throw an exception sparse centroids, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse", distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[1, 1]], [[0, -1]]]);
assert.doesNotThrow(function () {
KMeans.predict(matrix);
});
});
it("should return the predictions of the matrix sparse centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse", distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[-1, 2, 1], [0, 1, -3]]);
var prediction = KMeans.predict(matrix);
assert.strictEqual(prediction.length, 3);
});
it("should return the predictions of the matrix sparse centroids, sparse matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse", distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.SparseMatrix([[[0, 1]], [[1, 1]], [[0, 2], [1, 4]]]);
var prediction = KMeans.predict(matrix);
assert.strictEqual(prediction.length, 3);
});
it("should throw an exception if the matrix has less rows sparse centroids, dense matrix, distanceType Cos", function () {
var KMeans = new analytics.KMeans({ k: 3, centroidType: "Sparse", distanceType: 'Cos' });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X);
var matrix = new la.Matrix([[1, 1]]);
assert.throws(function () {
KMeans.predict(matrix);
});
});
});
describe("Explain Tests", function () {
it("should not throw an exception", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X, [1,2,3]);
var matrix = new la.Matrix([[1, 1], [0, -1]]);
assert.doesNotThrow(function () {
KMeans.explain(matrix);
});
});
it("should return the predictions of the matrix", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
KMeans.fit(X, [341,422,122]);
var matrix = new la.Matrix([[-1, 2, 1], [0, 1, -3]]);
var explanation = KMeans.explain(matrix);
assert.strictEqual(explanation[0].medoidID, 422);
assert.strictEqual(explanation[1].medoidID, 341);
assert.strictEqual(explanation[2].medoidID, 122);
});
it("should throw an exception if the matrix columns do not match record ids", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1, -2, -1], [1, 1, -3]]);
assert.throws(function () {
KMeans.fit(X , [0, 1]);
});
});
});
describe("Transform Tests", function () {
it("should not throw an exception", function () {
var KMeans = new analytics.KMeans({ k: 3 });
var X = new la.Matrix([[1,