楼主: ReneeBK
1115 2

Principal Component Analysis using JavaScript [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4900份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49655 个
通用积分
55.9937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2016-6-27 00:01:28 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. ml-pca
  2. NPM version  build status  David deps  npm download

  3. Principal component analysis (PCA)

  4. Installation

  5. $ npm install ml-pca

  6. Methods

  7. new PCA(dataset, options)

  8. Arguments

  9. dataset - Data to get the PCA. It must be a two-dimensional array with observations as rows and variables as columns.
  10. Options

  11. center - Center the dataset (default: true)
  12. scale - Standardize the dataset, i.e. divide by the standard deviation after centering (default: false)
  13. predict(dataset)

  14. Project the dataset in the PCA space

  15. Arguments

  16. dataset - A Matrix of the dataset to project.
  17. getExplainedVariance()

  18. Returns the percentage of variance explained by each component.

  19. getCumulativeVariance()

  20. Returns the cumulative explained variance.

  21. getStandardDeviations()

  22. Returns the standard deviations of each component.

  23. getEigenvectors()

  24. Get the eigenvectors of the covariance matrix.

  25. getEigenvalues()

  26. Get the eigenvalues on the diagonal.

  27. getLoadings()

  28. Get the loadings matrix (each row is a component and each column is a variable)

  29. License
复制代码

本帖隐藏的内容

pca-master.zip (7.29 KB)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Javascript Component PRINCIPAL Analysis Analysi default standard version status false

本帖被以下文库推荐

沙发
ReneeBK 发表于 2016-6-27 00:02:10
  1. 'use strict';

  2. const Matrix = require('ml-matrix');
  3. const EVD = Matrix.DC.EVD;
  4. const SVD = Matrix.DC.SVD;
  5. const Stat = require('ml-stat').matrix;
  6. const mean = Stat.mean;
  7. const stdev = Stat.standardDeviation;

  8. const defaultOptions = {
  9.     isCovarianceMatrix: false,
  10.     center: true,
  11.     scale: false
  12. };

  13. class PCA {
  14.     /**
  15.      * Creates new PCA (Principal Component Analysis) from the dataset
  16.      * @param {Matrix} dataset
  17.      * @param {Object} options - options for the PCA algorithm
  18.      * @param {boolean} reload - for load purposes
  19.      * @param {Object} model - for load purposes
  20.      * @constructor
  21.      * */
  22.     constructor(dataset, options, reload, model) {
  23.         if (reload) {
  24.             this.center = model.center;
  25.             this.scale = model.scale;
  26.             this.means = model.means;
  27.             this.stdevs = model.stdevs;
  28.             this.U = Matrix.checkMatrix(model.U);
  29.             this.S = model.S;
  30.             return;
  31.         }

  32.         options = Object.assign({}, defaultOptions, options);

  33.         this.center = false;
  34.         this.scale = false;
  35.         this.means = null;
  36.         this.stdevs = null;

  37.         if (options.isCovarianceMatrix) { // user provided a covariance matrix instead of dataset
  38.             this._computeFromCovarianceMatrix(dataset);
  39.             return;
  40.         }

  41.         var useCovarianceMatrix;
  42.         if (typeof options.useCovarianceMatrix === 'boolean') {
  43.             useCovarianceMatrix = options.useCovarianceMatrix;
  44.         } else {
  45.             useCovarianceMatrix = dataset.length > dataset[0].length;
  46.         }
  47.         
  48.         if (useCovarianceMatrix) { // user provided a dataset but wants us to compute and use the covariance matrix
  49.             dataset = this._adjust(dataset, options);
  50.             const covarianceMatrix = dataset.transpose().mmul(dataset).div(dataset.rows - 1);
  51.             this._computeFromCovarianceMatrix(covarianceMatrix);
  52.         } else {
  53.             dataset = this._adjust(dataset, options);
  54.             var svd = new SVD(dataset, {
  55.                 computeLeftSingularVectors: false,
  56.                 computeRightSingularVectors: true,
  57.                 autoTranspose: true
  58.             });

  59.             this.U = svd.rightSingularVectors;

  60.             const singularValues = svd.diagonal;
  61.             const eigenvalues = new Array(singularValues.length);
  62.             for (var i = 0; i < singularValues.length; i++) {
  63.                 eigenvalues[i] = singularValues[i] * singularValues[i] / (dataset.length - 1);
  64.             }
  65.             this.S = eigenvalues;
  66.         }
  67.     }

  68.     /**
  69.      * Load a PCA model from JSON
  70.      * @oaram {Object} model
  71.      * @return {PCA}
  72.      */
  73.     static load(model) {
  74.         if (model.name !== 'PCA')
  75.             throw new RangeError('Invalid model: ' + model.name);
  76.         return new PCA(null, null, true, model);
  77.     }

  78.     /**
  79.      * Exports the current model to an Object
  80.      * @return {Object} model
  81.      */
  82.     toJSON() {
  83.         return {
  84.             name: 'PCA',
  85.             center: this.center,
  86.             scale: this.scale,
  87.             means: this.means,
  88.             stdevs: this.stdevs,
  89.             U: this.U,
  90.             S: this.S,
  91.         };
  92.     }

  93.     /**
  94.      * Projects the dataset into new space of k dimensions.
  95.      * @param {Matrix} dataset
  96.      * @return {Matrix} dataset projected in the PCA space.
  97.      */
  98.     predict(dataset) {
  99.         dataset = new Matrix(dataset);

  100.         if (this.center) {
  101.             dataset.subRowVector(this.means);
  102.             if (this.scale) {
  103.                 dataset.divRowVector(this.stdevs);
  104.             }
  105.         }
  106.         
  107.         return dataset.mmul(this.U);
  108.     }

  109.     /**
  110.      * Returns the proportion of variance for each component.
  111.      * @return {[number]}
  112.      */
  113.     getExplainedVariance() {
  114.         var sum = 0;
  115.         for (var i = 0; i < this.S.length; i++) {
  116.             sum += this.S[i];
  117.         }
  118.         return this.S.map(value => value / sum);
  119.     }

  120.     /**
  121.      * Returns the cumulative proportion of variance.
  122.      * @return {[number]}
  123.      */
  124.     getCumulativeVariance() {
  125.         var explained = this.getExplainedVariance();
  126.         for (var i = 1; i < explained.length; i++) {
  127.             explained[i] += explained[i - 1];
  128.         }
  129.         return explained;
  130.     }

  131.     /**
  132.      * Returns the Eigenvectors of the covariance matrix.
  133.      * @returns {Matrix}
  134.      */
  135.     getEigenvectors() {
  136.         return this.U;
  137.     }

  138.     /**
  139.      * Returns the Eigenvalues (on the diagonal).
  140.      * @returns {[number]}
  141.      */
  142.     getEigenvalues() {
  143.         return this.S;
  144.     }

  145.     /**
  146.      * Returns the standard deviations of the principal components
  147.      * @returns {[number]}
  148.      */
  149.     getStandardDeviations() {
  150.         return this.S.map(x => Math.sqrt(x));
  151.     }

  152.     /**
  153.      * Returns the loadings matrix
  154.      * @return {Matrix}
  155.      */
  156.     getLoadings() {
  157.         return this.U.transpose();
  158.     }

  159.     _adjust(dataset, options) {
  160.         this.center = !!options.center;
  161.         this.scale = !!options.scale;

  162.         dataset = new Matrix(dataset);

  163.         if (this.center) {
  164.             const means = mean(dataset);
  165.             const stdevs = this.scale ? stdev(dataset, means, true) : null;
  166.             this.means = means;
  167.             dataset.subRowVector(means);
  168.             if (this.scale) {
  169.                 for (var i = 0; i < stdevs.length; i++) {
  170.                     if (stdevs[i] === 0) {
  171.                         throw new RangeError('Cannot scale the dataset (standard deviation is zero at index ' + i);
  172.                     }
  173.                 }
  174.                 this.stdevs = stdevs;
  175.                 dataset.divRowVector(stdevs);
  176.             }
  177.         }

  178.         return dataset;
  179.     }

  180.     _computeFromCovarianceMatrix(dataset) {
  181.         const evd = new EVD(dataset, {assumeSymmetric: true});
  182.         this.U = evd.eigenvectorMatrix;
  183.         for (var i = 0; i < this.U.length; i++) {
  184.             this.U[i].reverse();
  185.         }
  186.         this.S = evd.realEigenvalues.reverse();
  187.     }
  188. }

  189. module.exports = PCA;
复制代码

藤椅
reflets 发表于 2016-6-27 00:44:19
多谢楼主分享
已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
扫码
拉您进交流群
GMT+8, 2026-1-28 13:20