[机器学习][线性回归]ml.js 库说明

ml.js

ml 是很多库的一个集合,大约有如下这么种库:

[机器学习][线性回归]ml.js 库说明_第1张图片
ml分类.png

基本涵盖数组运算、矩阵运算、各种线性回归及非线性回归。现在来逐一解释每个包的功能,以后逐一进行实践。

名词解释

ml-array

ml-array-max 求得数组中最大元素。

import max from 'ml-array-max';
 
const result = max([1, 5, 3, 2, 4]);
//result = 5

ml-array-min 求得数组中最小元素。

import min from 'ml-array-min';
 
const result = min([1, 5, 3, 2, 4]);
//result = 1

ml-array-rescale 进行数组乘除变化。

import rescale from 'ml-array-rescale';
 
const result = rescale([0, 1, 2, 3, 4]);
// [0, 0.25, 0.5, 0.75, 1] 

ml-distance-euclidean 求两向量的欧式距离

euclidean(p, q)
//Returns the Euclidean distance between vectors p and q.

euclidean.squared(p, q)
//Returns the squared Euclidean distance between vectors p and q.

ml-kernel

ml-kernel 核函数计算相关包。
常用的有以下几种:

  • linear 线性核函数估计
  • gaussianrbf 高斯核函数估计
  • polynomialpoly 多项核函数估计
  • exponential 指数核函数估计
  • sigmoidsigmoid核函数估计

ml-matrix

ml-matrix 包含所有矩阵运算需要的API

ml-matrix API

const {Matrix} = require('ml-matrix');

var A = new Matrix([[1, 1], [2, 2]]);
var B = new Matrix([[3, 3], [1, 1]]);
var C = new Matrix([[3, 3], [1, 1]]);

// ============================ 
// Operations with the matrix : 
// ============================= 
 
// operations : 
const addition = Matrix.add(A, B); // addition = Matrix [[4, 4], [3, 3], rows: 2, columns: 2] 
const substraction = Matrix.sub(A, B); // substraction = Matrix [[-2, -2], [1, 1], rows: 2, columns: 2] 
const multiplication = A.mmul(B); // multiplication = Matrix [[4, 4], [8, 8], rows: 2, columns: 2] 
const mulByNumber = Matrix.mul(A, 10); // mulByNumber = Matrix [[10, 10], [20, 20], rows: 2, columns: 2] 
const divByNumber = Matrix.div(A, 10); // divByNumber = Matrix [[0.1, 0.1], [0.2, 0.2], rows: 2, columns: 2] 
const modulo = Matrix.mod(B, 2); // modulo = Matrix [[ 1, 1], [1, 1], rows: 2, columns: 2] 
const maxMatrix = Matrix.max(A, B); // max = Matrix [[3, 3], [2, 2], rows: 2, columns: 2] 
const minMatrix = Matrix.min(A, B); // max = Matrix [[1, 1], [1, 1], rows: 2, columns: 2] 
 
// Inplace operations : (consider that Cinit = C before all the operations below) 
C.add(A); // => C = Cinit + A 
C.sub(A); // => C = Cinit 
C.mul(10); // => C = 10 * Cinit 
C.div(10); // => C = Cinit 
C.mod(2); // => C = Cinit % 2 
 
// Standard Math operations : (abs, cos, round, etc.) 
var A = new Matrix([[1, 1], [-1, -1]]);
var expon = Matrix.exp(A); // expon = Matrix [[Math.exp(1), Math.exp(1)], [Math.exp(-1), Math.exp(-1)], rows: 2, columns: 2].  
var cosinus = Matrix.cos(A); // cosinus = Matrix [[Math.cos(1), Math.cos(1)], [Math.cos(-1), Math.cos(-1)], rows: 2, columns: 2].  
var absolute = Matrix.abs(A); // expon = absolute [[1, 1], [1, 1], rows: 2, columns: 2].  
// you can use 'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cbrt', 'ceil', 'clz32', 'cos', 'cosh', 'exp', 'expm1', 'floor', 'fround', 'log', 'log1p', 'log10', 'log2', 'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc' 
// Note : you can do it inplace too as A.abs() 
 
// ============================ 
// Manipulation of the matrix : 
// ============================= 
 
var numberRows = A.rows; // A has 2 rows 
var numberCols = A.columns; // A has 2 columns 
var firstValue = A.get(0, 0); // get(rows, columns) 
var numberElements = A.size; // 2 * 2 = 4 elements 
var isRow = A.isRowVector(); // false because A has more that 1 row 
var isColumn = A.isColumnVector(); // false because A has more that 1 column 
var isSquare = A.isSquare(); // true, because A is 2 * 2 matrix 
var isSym = A.isSymmetric(); // false, because A is not symmetric 
// remember : A = Matrix [[1, 1], [-1, -1], rows: 2, columns: 2] 
A.set(1, 0, 10); // A = Matrix [[1, 1], [10, -1], rows: 2, columns: 2]. We have change the second row and the first column 
var diag = A.diag(); // diag = [1, -1], i.e values in the diagonal. 
var m = A.mean(); // m = 2.75 
var product = A.prod(); // product = -10, i.e product of all values of the matrix 
var norm = A.norm(); // norm = 10.14889156509222, i.e Frobenius norm of the matrix 
var transpose = A.transpose(); // tranpose = Matrix [[1, 10], [1, -1], rows: 2, columns: 2] 
 
// ============================ 
// Instanciation of matrix : 
// ============================= 
 
var z = Matrix.zeros(3, 2); // z = Matrix [[0, 0], [0, 0], [0, 0], rows: 3, columns: 2] 
var z = Matrix.ones(2, 3); // z = Matrix [[1, 1, 1], [1, 1, 1], rows: 2, columns: 3] 
var z = Matrix.eye(3, 4); // Matrix [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], rows: 3, columns: 4]. there are 1 only in the diagonal 

ml-regression

ml-regression-simple-linear 一元线性回归

import SimpleLinearRegression from 'ml-regression-simple-linear';
 
const x = [0.5, 1, 1.5, 2, 2.5];
const y = [0, 1, 2, 3, 4];
 
const regression = new SimpleLinearRegression(x, y);
 
regression.slope // 2 
regression.intercept // -1 
regression.coefficients // [-1, 2] 
 
regression.predict(3); // 5 
regression.computeX(3.5); // 2.25 
 
regression.toString(); // 'f(x) = 2 * x - 1' 
 
regression.score(x, y);
// { r: 1, r2: 1, chi2: 0, rmsd: 0 } 
 
const json = regression.toJSON();
// { name: 'simpleLinearRegression', slope: 2, intercept: -1 } 
const loaded = SimpleLinearRegression.load(json);
loaded.predict(5) // 9 

ml-regression-multivariate-linear 多元线性回归

import MLR from 'ml-regression-multivariate-linear';
 
const x = [[0, 0], [1, 2], [2, 3], [3, 4]];
// Y0 = X0 * 2, Y1 = X1 * 2, Y2 = X0 + X1 
const y = [[0, 0, 0], [2, 4, 3], [4, 6, 5], [6, 8, 7]];
const mlr = new MLR(x, y);
console.log(mlr.predict([3, 3]));

ml-regression-polynomial 多项式回归

import PolynomialRegression from 'ml-regression-polynomial';
 
const x = [50, 50, 50, 70, 70, 70, 80, 80, 80, 90, 90, 90, 100, 100, 100];
const y = [3.3, 2.8, 2.9, 2.3, 2.6, 2.1, 2.5, 2.9, 2.4, 3.0, 3.1, 2.8, 3.3, 3.5, 3.0];
const degree = 5; // setup the maximum degree of the polynomial 
 
const regression = new PolynomialRegression(x, y, degree);
 
console.log(regression.predict(80)); // Apply the model to some x value. Prints 2.6. 
console.log(regression.coefficients); // Prints the coefficients in increasing order of power (from 0 to degree). 
console.log(regression.toString(3)); // Prints a human-readable version of the function. 
console.log(regression.toLaTeX());
console.log(regression.score(x, y));

你可能感兴趣的:([机器学习][线性回归]ml.js 库说明)