npm地址:https://www.npmjs.com/package/ml-regression-multivariate-linear

1、安装

npm install ml-regression-multivariate-linear

2、使用

  1. import MLR from "ml-regression-multivariate-linear";
  2. const x = [ //自变量
  3. [0, 0], //代表两个自变量的每一项
  4. [1, 2],
  5. [2, 3],
  6. [3, 4]
  7. ];
  8. // Y0 = X0 * 2, Y1 = X1 * 2, Y2 = X0 + X1
  9. const y = [ //因变量
  10. [0, 0, 0], //代表三个因变量的每一项
  11. [2, 4, 3],
  12. [4, 6, 5],
  13. [6, 8, 7]
  14. ];
  15. const mlr = new MLR(x, y);
  16. console.log(mlr.predict([3, 3]));

3、new MLR(x, y) 实例如下

  1. //weights: MultivariateLinearRegression
  2. {
  3. statistics: true,
  4. weights: [ //非标准化回归系数
  5. [ 0.5357142857141639, 3.608193277309965 ],
  6. [ 0.035714285714306016, -1.5829831932769025 ],
  7. [ 12.642857142855064, -58.11134453779414 ]
  8. ],
  9. inputs: 2,
  10. outputs: 2,
  11. intercept: true,
  12. stdError: Infinity,
  13. stdErrorMatrix: Matrix {
  14. [
  15. Infinity -Infiniy -Infiniy
  16. -Infiniy Infinity Infinity
  17. -Infiniy Infinity Infinity
  18. ]
  19. rows: 3
  20. columns: 3
  21. },
  22. stdErrors: [ Infinity, Infinity, Infinity ],
  23. tStats: [ 0, 0, 0 ]
  24. }

4、计算标准化回归系数

c
标准化回归系数 = 未标准化回归系数 * 该自变量的标准差 / 因变量的标准差

计算标准差方法:math.std(…nums) //其中nums为一组自变量或一组因变量

5、csv

如果数据使用csv引入,可以使用库 csvtojson