How to use the ml-matrix.Matrix.random function in ml-matrix

To help you get started, we’ve selected a few ml-matrix examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mljs / regression / src / __tests__ / kernel-ridge-regression.js View on Github external
import { Matrix } from 'ml-matrix';

import { KernelRidgeRegression } from '..';

var nSamples = 10;
var nVars = 2;

var Xs = Matrix.random(nSamples, nVars);
Xs.sub(0.5);
var Ys = Matrix.zeros(nSamples, 1);
for (var i = 0; i < nSamples; i++) {
  Ys.set(
    i,
    0,
    Xs.get(i, 0) * Xs.get(i, 0) +
      2 * Xs.get(i, 0) * Xs.get(i, 1) +
      Xs.get(i, 1) * Xs.get(i, 1)
  );
}

describe('Kernel ridge regression', function () {
  it('constant outputs', function () {
    var model = new KernelRidgeRegression([[0, 0], [1, 1]], [[0], [0]]);
    expect(model.predict([[1, 1], [2, 5], [4, 7]])).toStrictEqual([