How to use the ml-matrix.Matrix 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 / pca / src / pca.js View on Github external
_computeWithNIPALS(dataset, nCompNIPALS) {
    this.U = new Matrix(nCompNIPALS, dataset.columns);
    this.S = [];

    let x = dataset;
    for (let i = 0; i < nCompNIPALS; i++) {
      let dc = new NIPALS(x);

      this.U.setRow(i, dc.w.transpose());
      this.S.push(Math.pow(dc.s.get(0, 0), 2));

      x = dc.xResidual;
    }
    this.U = this.U.transpose(); // to be compatible with API
  }
}
github image-js / image-js / src / image / transform / warping.js View on Github external
[x2, y2, 1, 0, 0, 0, -x2 * X2, -y2 * X2],
    [x3, y3, 1, 0, 0, 0, -x3 * X3, -y1 * X3],
    [x4, y4, 1, 0, 0, 0, -x4 * X4, -y4 * X4],
    [0, 0, 0, x1, y1, 1, -x1 * Y1, -y1 * Y1],
    [0, 0, 0, x2, y2, 1, -x2 * Y2, -y2 * Y2],
    [0, 0, 0, x3, y3, 1, -x3 * Y3, -y3 * Y3],
    [0, 0, 0, x4, y4, 1, -x4 * Y4, -y4 * Y4]
  ]);

  let D = Matrix.columnVector([X1, X2, X3, X4, Y1, Y2, Y3, Y4]);

  let svd = new SingularValueDecomposition(S);
  let T = svd.solve(D); // solve S*T = D
  let [a, b, c, d, e, f, g, h] = T.to1DArray();

  let Xt = new Matrix(heightRect, widthRect);

  for (let channel = 0; channel < this.channels; channel++) {
    for (let i = 0; i < heightRect; i++) {
      for (let j = 0; j < widthRect; j++) {
        Xt.set(i, j, projectionPoint(i, j, a, b, c, d, e, f, g, h, this, channel));
      }
    }
    newImage.setMatrix(Xt, { channel: channel });
  }

  return newImage;
}
github image-js / image-js / src / image / transform / warping.js View on Github external
} else {
    widthRect = Math.ceil(Math.max(distance2Points(tl, tr), distance2Points(bl, br)));
    heightRect = Math.ceil(Math.max(distance2Points(tl, bl), distance2Points(tr, br)));
  }
  let newImage = Image.createFrom(this, { width: widthRect, height: heightRect });

  let [X1, Y1] = tl;
  let [X2, Y2] = tr;
  let [X3, Y3] = br;
  let [X4, Y4] = bl;
  let [x1, y1] = [0, 0];
  let [x2, y2] = [0, widthRect - 1];
  let [x3, y3] = [heightRect - 1, widthRect - 1];
  let [x4, y4] = [heightRect - 1, 0];

  let S = new Matrix([
    [x1, y1, 1, 0, 0, 0, -x1 * X1, -y1 * X1],
    [x2, y2, 1, 0, 0, 0, -x2 * X2, -y2 * X2],
    [x3, y3, 1, 0, 0, 0, -x3 * X3, -y1 * X3],
    [x4, y4, 1, 0, 0, 0, -x4 * X4, -y4 * X4],
    [0, 0, 0, x1, y1, 1, -x1 * Y1, -y1 * Y1],
    [0, 0, 0, x2, y2, 1, -x2 * Y2, -y2 * Y2],
    [0, 0, 0, x3, y3, 1, -x3 * Y3, -y3 * Y3],
    [0, 0, 0, x4, y4, 1, -x4 * Y4, -y4 * Y4]
  ]);

  let D = Matrix.columnVector([X1, X2, X3, X4, Y1, Y2, Y3, Y4]);

  let svd = new SingularValueDecomposition(S);
  let T = svd.solve(D); // solve S*T = D
  let [a, b, c, d, e, f, g, h] = T.to1DArray();
github mljs / pca / src / __tests__ / iris.js View on Github external
describe('iris dataset with provided covariance matrix', function () {
  var dataset = new Matrix(iris);
  var mean = dataset.mean('column');
  var stdevs = dataset.standardDeviation('column', { mean });
  dataset.subRowVector(mean).divRowVector(stdevs);
  var covarianceMatrix = dataset
    .transpose()
    .mmul(dataset)
    .divS(dataset.rows - 1);
  var pca = new PCA(covarianceMatrix, { isCovarianceMatrix: true });
  it('loadings', function () {
    var loadings = pca
      .getLoadings()
      .to2DArray()
      .map((x) => x.map((y) => Math.abs(y)));
    expect(loadings).toBeDeepCloseTo(expectedLoadings, 3);
  });
});
github mljs / pca / test / iris.js View on Github external
describe('iris dataset with provided covariance matrix', function () {
    var dataset = new Matrix(iris);
    var means = mean(dataset);
    var stdevs = stdev(dataset, means, true);
    dataset.subRowVector(means).divRowVector(stdevs);
    var covarianceMatrix = dataset.transpose().mmul(dataset).divS(dataset.rows - 1);
    var pca = new PCA(covarianceMatrix, {isCovarianceMatrix: true});
    it('loadings', function () {
        checkLoadings(pca);
    });
});
github image-js / mrz-detection / src / getMrz.js View on Github external
function getDiffVector(p1, p2) {
  const v1 = new Matrix([p1]);
  const v2 = new Matrix([p2]);
  const dv = v2.sub(v1);
  return dv;
}
github mljs / pca / src / pca.js View on Github external
constructor(dataset, options = {}) {
    if (dataset === true) {
      const model = options;
      this.center = model.center;
      this.scale = model.scale;
      this.means = model.means;
      this.stdevs = model.stdevs;
      this.U = Matrix.checkMatrix(model.U);
      this.S = model.S;
      this.R = model.R;
      this.excludedFeatures = model.excludedFeatures || [];
      return;
    }

    dataset = new Matrix(dataset);

    const {
      isCovarianceMatrix = false,
      method = 'SVD',
      nCompNIPALS = 2,
      center = true,
      scale = false,
      ignoreZeroVariance = false,
    } = options;

    this.center = center;
    this.scale = scale;
    this.means = null;
    this.stdevs = null;
    this.excludedFeatures = [];
github mljs / regression / src / regression / poly-fit-regression2d.js View on Github external
x2
        .clone()
        .abs()
        .max();
    var scaleY =
      1.0 /
      y
        .clone()
        .abs()
        .max();

    x1.mulColumn(0, scaleX1);
    x2.mulColumn(0, scaleX2);
    y.mulColumn(0, scaleY);

    var A = new Matrix(examples, coefficients);
    var col = 0;

    for (var i = 0; i <= this.order; ++i) {
      var limit = this.order - i;
      for (var j = 0; j <= limit; ++j) {
        var result = powColVector(x1, i).mulColumnVector(powColVector(x2, j));
        A.setColumn(col, result);
        col++;
      }
    }

    var svd = new SVD(A.transpose(), {
      computeLeftSingularVectors: true,
      computeRightSingularVectors: true,
      autoTranspose: false
    });