machine_learning, 面向 node.js的机器学习库

分享于 

12分钟阅读

GitHub

  繁體 雙語
Machine learning library for Node.js
  • 源代码名称:machine_learning
  • 源代码网址:http://www.github.com/junku901/machine_learning
  • machine_learning源代码文档
  • machine_learning源代码下载
  • Git URL:
    git://www.github.com/junku901/machine_learning.git
    Git Clone代码到本地:
    git clone http://www.github.com/junku901/machine_learning
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/junku901/machine_learning
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    machine_learning

    面向 node.js的机器学习库。 你也可以在浏览器中使用这里库。

    演示在浏览器

    API文档

    安装

    Node.js

    
    $ npm install machine_learning
    
    
    
    

    要在浏览器中使用这里库,请包括 machine_learning.min.js。

    <scriptsrc="/js/machine_learning.min.js"></script>

    演示在浏览器

    下面是 API文档文档。 ( 仍在进行中)

    特性

    • 逻辑回归
    • 多层感知器( 多层感知器)
    • 支持向量机( SVM )
    • KNN ( k 最近邻居)
    • k 表示聚类
    • 3 种优化算法( 爬山。模拟退火。遗传算法)
    • 决策树
    • 非负矩阵分解法

    实现细节

    支持向量机的序列最小优化( SMO ) 及它的培训算法。

    决策树。分类树和回归树( 购物车) 用于构建算法。

    用法

    逻辑回归

    var ml =require('machine_learning');var x = [[1,1,1,0,0,0],
     [1,0,1,0,0,0],
     [1,1,1,0,0,0],
     [0,0,1,1,1,0],
     [0,0,1,1,0,0],
     [0,0,1,1,1,0]];var y = [[1, 0],
     [1, 0],
     [1, 0],
     [0, 1],
     [0, 1],
     [0, 1]];var classifier =newml.LogisticRegression({
     'input': x,
     'label': y,
     'n_in':6,
     'n_out':2});classifier.set('log level',1);var training_epochs =800, lr =0.01;classifier.train({
     'lr': lr,
     'epochs': training_epochs
    });
    x = [[1, 1, 0, 0, 0, 0],
     [0, 0, 0, 1, 1, 0],
     [1, 1, 1, 1, 1, 0]];console.log("Result : ",classifier.predict(x));

    ( 多层感知器)

    var ml =require('machine_learning');var x = [[0.4, 0.5, 0.5, 0., 0., 0.],
     [0.5, 0.3, 0.5, 0., 0., 0.],
     [0.4, 0.5, 0.5, 0., 0., 0.],
     [0., 0., 0.5, 0.3, 0.5, 0.],
     [0., 0., 0.5, 0.4, 0.5, 0.],
     [0., 0., 0.5, 0.5, 0.5, 0.]];var y = [[1, 0],
     [1, 0],
     [1, 0],
     [0, 1],
     [0, 1],
     [0, 1]];var mlp =newml.MLP({
     'input': x,
     'label': y,
     'n_ins':6,
     'n_outs':2,
     'hidden_layer_sizes': [4,4,5]
    });mlp.set('log level',1); // 0 : nothing, 1 : info, 2 : warning.mlp.train({
     'lr':0.6,
     'epochs':20000});
    a = [[0.5, 0.5, 0., 0., 0., 0.],
     [0., 0., 0., 0.5, 0.5, 0.],
     [0.5, 0.5, 0.5, 0.5, 0.5, 0.]];console.log(mlp.predict(a));

    支持向量机( 支持向量机)

    var ml =require('machine_learning');var x = [[0.4, 0.5, 0.5, 0., 0., 0.],
     [0.5, 0.3, 0.5, 0., 0., 0.01],
     [0.4, 0.8, 0.5, 0., 0.1, 0.2],
     [1.4, 0.5, 0.5, 0., 0., 0.],
     [1.5, 0.3, 0.5, 0., 0., 0.],
     [0., 0.9, 1.5, 0., 0., 0.],
     [0., 0.7, 1.5, 0., 0., 0.],
     [0.5, 0.1, 0.9, 0., -1.8, 0.],
     [0.8, 0.8, 0.5, 0., 0., 0.],
     [0., 0.9, 0.5, 0.3, 0.5, 0.2],
     [0., 0., 0.5, 0.4, 0.5, 0.],
     [0., 0., 0.5, 0.5, 0.5, 0.],
     [0.3, 0.6, 0.7, 1.7, 1.3, -0.7],
     [0., 0., 0.5, 0.3, 0.5, 0.2],
     [0., 0., 0.5, 0.4, 0.5, 0.1],
     [0., 0., 0.5, 0.5, 0.5, 0.01],
     [0.2, 0.01, 0.5, 0., 0., 0.9],
     [0., 0., 0.5, 0.3, 0.5, -2.3],
     [0., 0., 0.5, 0.4, 0.5, 4],
     [0., 0., 0.5, 0.5, 0.5, -2]];var y = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1,1,1,1];var svm =newml.SVM({
     x : x,
     y : y
    });svm.train({
     C:1.1, // default : 1.0. C in SVM. tol :1e-5, // default : 1e-4. Higher tolerance --> Higher precision max_passes :20, // default : 20. Higher max_passes --> Higher precision alpha_tol :1e-5, // default : 1e-5. Higher alpha_tolerance --> Higher precision kernel : { type:"polynomial", c:1, d:5}
     // default : {type :"gaussian", sigma : 1.0}// {type :"gaussian", sigma : 0.5}// {type :"linear"}//x*y// {type :"polynomial", c : 1, d : 8}//(x*y + c)^d// Or you can use your own kernel.// kernel : function(vecx,vecy) { return dot(vecx,vecy);}});console.log("Predict : ",svm.predict([1.3, 1.7, 0.5, 0.5, 1.5, 0.4]));

    KNN ( k 最近邻)

    var ml =require('machine_learning');var data = [[1,0,1,0,1,1,1,0,0,0,0,0,1,0],
     [1,1,1,1,1,1,1,0,0,0,0,0,1,0],
     [1,1,1,0,1,1,1,0,1,0,0,0,1,0],
     [1,0,1,1,1,1,1,1,0,0,0,0,1,0],
     [1,1,1,1,1,1,1,0,0,0,0,0,1,1],
     [0,0,1,0,0,1,0,0,1,0,1,1,1,0],
     [0,0,0,0,0,0,1,1,1,0,1,1,1,0],
     [0,0,0,0,0,1,1,1,0,1,0,1,1,0],
     [0,0,1,0,1,0,1,1,1,1,0,1,1,1],
     [0,0,0,0,0,0,1,1,1,1,1,1,1,1],
     [1,0,1,0,0,1,1,1,1,1,0,0,1,0]
     ];var result = [23,12,23,23,45,70,123,73,146,158,64];var knn =newml.KNN({
     data : data,
     result : result
    });var y =knn.predict({
     x : [0,0,0,0,0,0,0,1,1,1,1,1,1,1],
     k :3,
     weightf : {type :'gaussian', sigma :10.0},
     // default : {type : 'gaussian', sigma : 10.0}// {type : 'none'}. weight == 1// Or you can use your own weight f// weightf : function(distance) {return 1./distance} distance : {type :'euclidean'}
     // default : {type : 'euclidean'}// {type : 'pearson'}// Or you can use your own distance function// distance : function(vecx, vecy) {return Math.abs(dot(vecx,vecy));}});console.log(y);

    k-表示群集

    var ml =require('machine_learning');var data = [[1,0,1,0,1,1,1,0,0,0,0,0,1,0],
     [1,1,1,1,1,1,1,0,0,0,0,0,1,0],
     [1,1,1,0,1,1,1,0,1,0,0,0,1,0],
     [1,0,1,1,1,1,1,1,0,0,0,0,1,0],
     [1,1,1,1,1,1,1,0,0,0,0,0,1,1],
     [0,0,1,0,0,1,0,0,1,0,1,1,1,0],
     [0,0,0,0,0,0,1,1,1,0,1,1,1,0],
     [0,0,0,0,0,1,1,1,0,1,0,1,1,0],
     [0,0,1,0,1,0,1,1,1,1,0,1,1,1],
     [0,0,0,0,0,0,1,1,1,1,1,1,1,1],
     [1,0,1,0,0,1,1,1,1,1,0,0,1,0]
     ];var result =ml.kmeans.cluster({
     data : data,
     k :4,
     epochs:100,
     distance : {type :"pearson"}
     // default : {type : 'euclidean'}// {type : 'pearson'}// Or you can use your own distance function// distance : function(vecx, vecy) {return Math.abs(dot(vecx,vecy));}});console.log("clusters : ", result.clusters);console.log("means : ", result.means);

    登山

    var ml =require('machine_learning');varcostf=function(vec) {
     var cost =0;
     for(var i =0; i<14;i++) { // 15-dimensional vector cost += (0.5*i*vec[i]*Math.exp(-vec[i]+vec[i+1])/vec[i+1])
     }
     cost += (3.*vec[14]/vec[0]);
     return cost;
    };var domain = [];for(var i=0;i<15;i++)
     domain.push([1,70]); // domain[idx][0] : minimum of vec[idx], domain[idx][1] : maximum of vec[idx].var vec =ml.optimize.hillclimb({
     domain : domain,
     costf : costf
    });console.log("vec : ",vec);console.log("cost : ",costf(vec));

    模拟退火

    var ml =require('machine_learning');varcostf=function(vec) {
     var cost =0;
     for(var i =0; i<14;i++) { // 15-dimensional vector cost += (0.5*i*vec[i]*Math.exp(-vec[i]+vec[i+1])/vec[i+1])
     }
     cost += (3.*vec[14]/vec[0]);
     return cost;
    };var domain = [];for(var i=0;i<15;i++)
     domain.push([1,70]); // domain[idx][0] : minimum of vec[idx], domain[idx][1] : maximum of vec[idx].var vec =ml.optimize.anneal({
     domain : domain,
     costf : costf,
     temperature :100000.0,
     cool :0.999,
     step :4});console.log("vec : ",vec);console.log("cost : ",costf(vec));

    遗传算法

    var ml =require('machine_learning');varcostf=function(vec) {
     var cost =0;
     for(var i =0; i<14;i++) { // 15-dimensional vector cost += (0.5*i*vec[i]*Math.exp(-vec[i]+vec[i+1])/vec[i+1])
     }
     cost += (3.*vec[14]/vec[0]);
     return cost;
    };var domain = [];for(var i=0;i<15;i++)
     domain.push([1,70]); // domain[idx][0] : minimum of vec[idx], domain[idx][1] : maximum of vec[idx].var vec =ml.optimize.genetic({
     domain : domain,
     costf : costf,
     population :50,
     elite :2, // elitism. number of elite chromosomes. epochs :300,
     q :0.3// Rank-Based Fitness Assignment. fitness = q * (1-q)^(rank-1)// higher q --> higher selection pressure});console.log("vec : ",vec);console.log("cost : ",costf(vec));

    决策树

    // Reference : 'Programming Collective Intellignece' by Toby Segaran.var ml =require('machine_learning');var data =[['slashdot','USA','yes',18],
     ['google','France','yes',23],
     ['digg','USA','yes',24],
     ['kiwitobes','France','yes',23],
     ['google','UK','no',21],
     ['(direct)','New Zealand','no',12],
     ['(direct)','UK','no',21],
     ['google','USA','no',24],
     ['slashdot','France','yes',19],
     ['digg','USA','no',18,],
     ['google','UK','no',18,],
     ['kiwitobes','UK','no',19],
     ['digg','New Zealand','yes',12],
     ['slashdot','UK','no',21],
     ['google','UK','yes',18],
     ['kiwitobes','France','yes',19]];var result = ['None','Premium','Basic','Basic','Premium','None','Basic','Premium','None','None','None','None','Basic','None','Basic','Basic'];var dt =newml.DecisionTree({
     data : data,
     result : result
    });dt.build();// dt.print();console.log("Classify : ", dt.classify(['(direct)','USA','yes',5]));dt.prune(1.0); // 1.0 : mingain.dt.print();

    非负矩阵非负分解( 非负矩阵分解)

    var ml =require('machine_learning');var matrix = [[22,28],
     [49,64]];var result =ml.nmf.factorize({
     matrix : matrix,
     features :3,
     epochs :100});console.log("First Matrix : ",result[0]);console.log("Second Matrix : ",result[1]);

    ##License

    ( MIT许可证)

    版权所有( c ) 2014 joon ku <junku901@gmail.com>

    若要在取得该软件副本时免费授予任何人,如有下列条件的软件,请免费授予该软件的副本,并与相关的文档文件('软件') 进行许可,包括不受限制的权利,包括以下条件:

    上述版权声明和本许可声明须包括在所有的副本或实质性部分的软件。

    软件是'是',没有任何保证,表示或者隐含,包括但不限于销售,适合特定用途和 NONINFRINGEMENT。 作者或者版权持有人在合同。侵权或者它的他与软件或者它的他用户交易的行为。


    mac  learn  MACH  机器学习