How to use the ml-matrix.SVD 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
this._adjust(dataset, ignoreZeroVariance);
    switch (method) {
      case 'covarianceMatrix': {
        // User provided a dataset but wants us to compute and use the covariance matrix.
        const covarianceMatrix = new MatrixTransposeView(dataset)
          .mmul(dataset)
          .div(dataset.rows - 1);
        this._computeFromCovarianceMatrix(covarianceMatrix);
        break;
      }
      case 'NIPALS': {
        this._computeWithNIPALS(dataset, nCompNIPALS);
        break;
      }
      case 'SVD': {
        const svd = new SVD(dataset, {
          computeLeftSingularVectors: false,
          computeRightSingularVectors: true,
          autoTranspose: true,
        });

        this.U = svd.rightSingularVectors;

        const singularValues = svd.diagonal;
        const eigenvalues = [];
        for (const singularValue of singularValues) {
          eigenvalues.push((singularValue * singularValue) / (dataset.rows - 1));
        }
        this.S = eigenvalues;
        break;
      }
      default: {
github image-js / image-js / src / image / compute / svd.js View on Github external
export default function getSvd() {
  this.checkProcessable('getSvd', {
    bitDepth: [1]
  });

  return new SVD(this.points);
}
github mljs / regression / src / regression / poly-fit-regression2d.js View on Github external
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
    });

    var qqs = Matrix.rowVector(svd.diagonal);
    qqs = qqs.apply(function (i, j) {
      if (this.get(i, j) >= 1e-15) this.set(i, j, 1 / this.get(i, j));
      else this.set(i, j, 0);
    });

    var qqs1 = Matrix.zeros(examples, coefficients);
    for (i = 0; i < coefficients; ++i) {
      qqs1.set(i, i, qqs.get(0, i));
    }
github image-js / image-js / src / image / operator / getSeparatedKernel.js View on Github external
export default function getSeparatedKernel(kernel) {
  const svd = new SVD(kernel, { autoTranspose: true });
  if (svd.rank !== 1) return null;
  const s = Math.sqrt(svd.s[0]);
  const v = svd.U.map((v) => v[0] * s);
  const h = svd.V.map((h) => h[0] * s);
  return [v, h];
}