[機(jī)器學(xué)習(xí)][線性回歸]ml.js 庫說明

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ì)
  • gaussianrbf 高斯核函數(shù)估計(jì)
  • polynomialpoly 多項(xiàng)核函數(shù)估計(jì)
  • exponential 指數(shù)核函數(shù)估計(jì)
  • sigmoidsigmoid核函數(shù)估計(jì)

ml-matrix

ml-matrix 包含所有矩陣運(yùn)算需要的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 多項(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));

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末住练,一起剝皮案震驚了整個(gè)濱河市地啰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌讲逛,老刑警劉巖亏吝,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異盏混,居然都是意外死亡蔚鸥,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門许赃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來止喷,“玉大人,你說我怎么就攤上這事混聊〉” “怎么了?”我有些...
    開封第一講書人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長预愤。 經(jīng)常有香客問我沟于,道長,這世上最難降的妖魔是什么鳖粟? 我笑而不...
    開封第一講書人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任社裆,我火速辦了婚禮,結(jié)果婚禮上向图,老公的妹妹穿的比我還像新娘泳秀。我一直安慰自己,他們只是感情好榄攀,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開白布嗜傅。 她就那樣靜靜地躺著,像睡著了一般檩赢。 火紅的嫁衣襯著肌膚如雪吕嘀。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,624評(píng)論 1 305
  • 那天贞瞒,我揣著相機(jī)與錄音偶房,去河邊找鬼。 笑死军浆,一個(gè)胖子當(dāng)著我的面吹牛棕洋,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乒融,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼掰盘,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了赞季?” 一聲冷哼從身側(cè)響起愧捕,我...
    開封第一講書人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎申钩,沒想到半個(gè)月后次绘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡撒遣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年断盛,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片愉舔。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖伙菜,靈堂內(nèi)的尸體忽然破棺而出轩缤,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布火的,位于F島的核電站壶愤,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏馏鹤。R本人自食惡果不足惜征椒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望湃累。 院中可真熱鬧勃救,春花似錦、人聲如沸治力。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宵统。三九已至晕讲,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間马澈,已是汗流浹背瓢省。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留痊班,地道東北人勤婚。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像辩块,于是被迫代替她去往敵國和親蛔六。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容