ml.js
ml 是很多庫的一個(gè)集合,大約有如下這么種庫:
ml分類.png
基本涵蓋數(shù)組運(yùn)算、矩陣運(yùn)算硬纤、各種線性回歸及非線性回歸〖巫ィ現(xiàn)在來逐一解釋每個(gè)包的功能,以后逐一進(jìn)行實(shí)踐刻恭。
名詞解釋
ml-array
ml-array-max 求得數(shù)組中最大元素。
import max from 'ml-array-max';
const result = max([1, 5, 3, 2, 4]);
//result = 5
ml-array-min 求得數(shù)組中最小元素。
import min from 'ml-array-min';
const result = min([1, 5, 3, 2, 4]);
//result = 1
ml-array-rescale 進(jìn)行數(shù)組乘除變化新翎。
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 核函數(shù)計(jì)算相關(guān)包。
常用的有以下幾種:
-
linear
線性核函數(shù)估計(jì) -
gaussian
或rbf
高斯核函數(shù)估計(jì) -
polynomial
或poly
多項(xiàng)核函數(shù)估計(jì) -
exponential
指數(shù)核函數(shù)估計(jì) -
sigmoid
sigmoid核函數(shù)估計(jì)
ml-matrix
ml-matrix 包含所有矩陣運(yùn)算需要的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 多項(xiàng)式回歸
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));