Example #1
0
        it('should return correct weights', function () {
            var regFact = 0.1;
            var forgetFact = 0.90;
            var n = 1000;
            var dim = 3;

            var linreg = new analytics.RecLinReg({ dim: dim, regFact: regFact, forgetFact: forgetFact });

            var X = new la.Matrix({ rows: n, cols: dim });
            var yArr = [];

            for (var rowN = 0; rowN < n; rowN++) {
                var rowArr = [1];
                for (var colN = 1; colN < dim; colN++) {
                    rowArr.push(Math.random());
                }

                var xi = new la.Vector(rowArr);
                var yi = Math.random();

                linreg.partialFit(xi, yi);

                X.setRow(rowN, xi);
                yArr.push(yi);
            }

            var y = new la.Vector(yArr);

            var wgts = linreg.weights;
            var expectedWgts = linregWgtsFromMatrixForgetFact(X, y, regFact, forgetFact);

            assert.eqtol(wgts[0], expectedWgts[0], tol);
            assert.eqtol(wgts[1], expectedWgts[1], tol);
            assert.eqtol(wgts[2], expectedWgts[2], tol);
        })
Example #2
0
File: pca.js Project: blazs/qminer
 it("should achive perfect reconstruction", function () {
     var A = new la.Matrix([[10, 20, 110, 20, 10], [3, 4, 13, 63, 1]]);
     var pca = new analytics.PCA({ k: 2, iter: 1000 });
     pca.fit(A);
     var pA = pca.transform(A);
     var rA = pca.inverseTransform(pA);
     assert(A.minus(rA).frob() < 1e-6);
 });
Example #3
0
it("should make test number 52", function () {

     // import la module
     var la = require('qminer').la;
     // create a new matrix
     var mat = new la.Matrix([[1, 2], [3, 4]]);
     // create a JavaScript array out of matrix
     var arr = mat.toArray(); // returns an array [[1, 2], [3, 4]]
    
});
Example #4
0
it("should make test number 20", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create the matrix
	 var mat = new la.Matrix([[1, 2], [3, 1], [-4, 5]]);
	 // get the second column of the matrix
	 var col = mat.getCol(1);
	
});
Example #5
0
it("should make test number 19", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create the matrix
	 var mat = new la.Matrix([[1, 2], [3, 1], [-4, 5]]);
	 // get the row id of the maximum value of the second column
	 var maxRow = mat.colMaxIdx(1); // returns the value 2
	
});
Example #6
0
it("should make test number 15", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create the matrix
	 var mat = new la.Matrix([[1, 2], [3, 4]]);
	 // get the frobenious norm of the matrix
	 var frob = mat.frob(); // returns the value Math.sqrt(30)
	
});
Example #7
0
it("should make test number 24", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a new matrix
	 var mat = new la.Matrix([[1, -1, 0], [15, 8, 3], [0, 1, 0]]);
	 // call diag function
	 var vec = mat.diag(); // returns a vector [1, 8, 0]
	
});
Example #8
0
it("should make test number 3", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a new matrix
	 var mat = new la.Matrix([[2, 3], [-2, -2], [-3, 1]]);
	 // get the value at the index (2, 1)
	 var value = mat.at(2, 1); // returns the value 1
	
});
Example #9
0
it("should make test number 13", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a new matrix
	 var mat = new la.Matrix([[1, 2], [3, 5]]);
	 // get matrix as string
	 var text = mat.toString(); // returns `1 2 \n3 5 \n\n`
	
});
Example #10
0
it("should make test number 14", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create the matrix
	 var mat = new la.Matrix([[1, 2], [0, 3], [-4, 0]]);
	 // transform the matrix into the sparse form
	 var spMat = mat.sparse();
	
});
Example #11
0
it("should make test number 22", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create the matrix
	 var mat = new la.Matrix([[1, 2], [3, 1], [-4, 5]]);
	 // get the first row of the matrix
	 var row = mat.getRow(1);
	
});
Example #12
0
it("should make test number 12", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a new matrix
	 var mat = new la.Matrix([[3, 4], [4, 15/2]]);
	 // get the row norms of the matrix
	 var rowNorms = mat.colNorms(); // returns the vector [5, 17/2]
	
});
Example #13
0
it("should make test number 10", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a new matrix
	 var M = new la.Matrix([[1, 2], [-1, -5]]);
	 // create a new vector
	 var b = new la.Vector([-1, -6]);
	 // solve the linear system M*x = b
	 var x = M.solve(b); // returns vector [1, -1]
	
});
Example #14
0
it("should make test number 6", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a new matrix
	 var mat = new la.Matrix([[1, 2], [-1, 5]]);
	 // create a new vector
	 var vec = new la.Vector([1, -1]);
	 //multiply mat and vec
	 var vec2 = mat.multiplyT(vec); // returns vector [2, 7]
	
});
Example #15
0
it("should make test number 9", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a matrix
	 var mat = new la.Matrix([[2, -5], [3, 1]]);
	 // transpose the matrix
	 // the return matrix is
	 //  2   3
	 // -5   1
	 var trans = mat.transpose();
	
});
Example #16
0
it("should make test number 49", function () {

     // import la module
     var la = require('qminer').la;
	 // create a new matrix
	 var mat = new la.Matrix([[1, 2], [3, 4]]);
	 // print the matrix
     // each row represents a row in the matrix. For this example:
     // 1  2
     // 3  4
	 mat.print();
	
});
Example #17
0
it("should make test number 15", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a new matrix
	 var mat = new la.Matrix([[3, 4], [4, 15/2]]);
	 // normalize the columns of the matrix
	 // the matrix is going to be of the form:
	 // 3/5     8/17
	 // 4/5    15/17
	 mat.normalizeCols();
	
});
Example #18
0
it("should make test number 25", function () {

	 // import the modules
	 var fs = require('qminer').fs;
	 var la = require('qminer').la;
	 // create new matrix
	 var mat = new la.Matrix([[1, 2], [3, 4]]);
	 // open write stream
	 var fout = fs.openWrite('mat.dat');
	 // save matrix and close write stream
	 mat.save(fout).close();
	
});
Example #19
0
it("should make test number 26", function () {

	 // import the modules
	 var fs = require('qminer').fs;
	 var la = require('qminer').la;
	 // create an empty matrix
	 var mat = new la.Matrix();
	 // open a read stream ('mat.dat' is pre-saved)
	 var fin = fs.openRead('mat.dat');
	 // load the matrix
	 mat.load(fin);
	
});
Example #20
0
it("should make test number 8", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create two matrices
	 var mat = new la.Matrix([[1, 2], [-1, 5]]);
	 var mat2 = new la.Matrix([[1, -1], [3, 2]]);
	 // substract the matrices
	 // the return matrix is
	 //  0   3
	 // -4   3
	 var diff = mat.minus(mat2);
	
});
Example #21
0
it("should make test number 4", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a new matrix
	 var mat = new la.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
	 var arg = new la.Matrix([[10, 11], [12, 13]]);
	 mat.put(0, 1, arg);
	 // updates the matrix to
     // 1  10  11
     // 4  12  13
     // 7   8   9   
	
});
Example #22
0
it("should make test number 7", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create two matrices
	 var mat = new la.Matrix([[1, 2], [-1, 5]]);
	 var mat2 = new la.Matrix([[1, -1], [3, 2]]);
	 // add the matrices 
	 // the return matrix is
	 // 2   1
	 // 2   7
	 var sum = mat.plus(mat2);
	
});
Example #23
0
it("should make test number 23", function () {

	 // import la module
	 var la = require('qminer').la;
	 // create a matrix
	 var mat = new la.Matrix([[1, -3, 2], [9, 2, -4],  [-2, 3, 3]]);
	 // create a vector
	 var vec = new la.Vector([-3, 2, 2]);
	 // set the first row of the matrix with the vector
	 // the changed matrix is now
	 // -3    2    2
	 //  9    2   -4
	 // -2    3    3
	 mat.setRow(0, vec);
	
});
Example #24
0
 it('should find a soft margin and be biased towards positive examples', function () {
     X = [[-10, 1],
          [-3, 0],
          [-20, -1],
          [20, 1],
          [3, 0],
          [10, -1]];
     var y = [1, -1, 1, -1, 1, -1];
     
     var matrix = new la.Matrix(X);
     matrix = matrix.transpose();
     var vec = new la.Vector(y);
     // unbalance: positive examples are 1000 times more important
     var SVC = new analytics.SVC({ algorithm: "LIBSVM", c: 1e-3, j: 1e+3 });
     SVC.fit(matrix, vec);
     assert.eqtol(SVC.predict(matrix).minus(new la.Vector([1, 1, 1, 1, 1, 1])).norm(), 0, 1e-6);
 });
Example #25
0
 it('should find a soft margin', function () {
     X = [[-10, 1],
          [-3, 0],
          [-20, -1],
          [20, 1],
          [3, 0],
          [10, -1]];
     var y = [1, -1, 1, -1, 1, -1];
     
     var matrix = new la.Matrix(X);
     matrix = matrix.transpose();
     var vec = new la.Vector(y);
     
     var SVC = new analytics.SVC({ algorithm: "LIBSVM", c: 1e-3 });
     SVC.fit(matrix, vec);
     assert.eqtol(SVC.predict(matrix).minus(new la.Vector([1, 1, 1, -1, -1, -1])).norm(), 0, 1e-6);
 });
Example #26
0
 it('should find a fit with RBF kernel', function () {
      X = [[1, 0],
           [0, 1],
           [-1, 0],
           [0, -1],
           [2, 0],
           [0, 2],
           [-2, 0],
           [0, -2]];
      var y = [1, 1, 1, 1, -1, -1, -1, -1];
      
      var matrix = new la.Matrix(X);
      matrix = matrix.transpose();
      var vec = new la.Vector(y);
      var SVC = new analytics.SVC({ algorithm: "LIBSVM", kernel: "RBF", p:10e-6 });
      SVC.fit(matrix, vec);
      assert.eqtol(SVC.predict(matrix).minus(new la.Vector([1, 1, 1, 1, -1, -1, -1, -1])).norm(), 0, 1e-6);
  });
Example #27
0
 it('should find a fit with polynomial kernel', function () {
      X = [[-3],
           [-2],
           [-1],
           [0],
           [1],
           [2],
           [3]];
      var y = [9, 4, 1, 0, 4, 1, 9];
      
      var matrix = new la.Matrix(X);
      matrix = matrix.transpose();
      var vec = new la.Vector(y);
      // unbalance: positive examples are 1000 times more important
      var SVR = new analytics.SVR({ algorithm: "LIBSVM", kernel: "POLY", degree: 2, p: 10e-3 });
      SVR.fit(matrix, vec);
      assert.eqtol(SVR.predict(matrix).minus(new la.Vector([9, 4, 1, 0, 1, 4, 9])).norm(), 0, 1e-1);
  });
Example #28
0
 it('should find a fit with RBF kernel', function () {
      X = [[1, 0],
           [0, 1],
           [-1, 0],
           [0, -1],
           [2, 0],
           [0, 2],
           [-2, 0],
           [0, -2]];
      var y = [1, 1, 1, 1, 2, 2, 2, 2];
      
      var matrix = new la.Matrix(X);
      matrix = matrix.transpose();
      var vec = new la.Vector(y);
      // unbalance: positive examples are 1000 times more important
      var SVR = new analytics.SVR({ algorithm: "LIBSVM", kernel: "RBF", p: 10e-3 });
      SVR.fit(matrix, vec);
      assert.eqtol(SVR.predict(matrix).minus(new la.Vector([1, 1, 1, 1, 2, 2, 2, 2])).norm(), 0, 1e-1);
  });
Example #29
0
 it('should fit a model on iris dataset (dense), class=setosa, features:sepal length, sepal width', function () {
      var X = require('./irisX.json');
      var y = require('./irisY.json');
      
      var matrix = new la.Matrix(X);
      matrix = matrix.transpose();
      var vec = new la.Vector(y);
      var SVC = new analytics.SVC({ algorithm: "LIBSVM", c: 10000 });
      SVC.fit(matrix, vec);
      // based on matlab
      //load fisheriris
      //X = [meas(:,1), meas(:,2)];
      //Y = nominal(ismember(species,'setosa'));
      //y = 2*(double(Y)-1.5);
      //s=fitcsvm(X,y,'Standardize',false, 'BoxConstraint',10000);
      // weights = s.SupportVectors'* (s.Alpha.*(y(s.IsSupportVector)))
      // bias = s.Bias
      assert.eqtol(SVC.weights.minus(new la.Vector([-8.5680, 7.1408])).norm(), 0, 1e-3);
      assert.eqtol(Math.abs(SVC.bias - 23.1314), 0, 1e-3);
  });
Example #30
0
 it('should find a fit with polynomial kernel', function () {
      X = [[-3, 5],
           [-1, 0.5],
           [0, -0.5],
           [1, 0.5],
           [3, 5],
           [-2, 5],
           [-0.5, 1],
           [0, 0.5],
           [0.5, 1],
           [2, 5]];
      var y = [1, 1, 1, 1, 1, -1, -1, -1, -1, -1];
      
      var matrix = new la.Matrix(X);
      matrix = matrix.transpose();
      var vec = new la.Vector(y);
      var SVC = new analytics.SVC({ algorithm: "LIBSVM", kernel: "POLY", degree: 2, p:10e-6, eps:10e-6 });
      SVC.fit(matrix, vec); 
      assert.eqtol(SVC.predict(matrix).minus(new la.Vector([1, 1, 1, 1, 1, -1, -1, -1, -1, -1])).norm(), 0, 1e-6);
  });