楼主: ReneeBK
1292 8

【JavaScript】Classification and Regression Trees using JavaScript [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8704
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
CART (Classification and regression trees)

Decision trees using CART implementation.

Installation

npm install ml-cart

DocumentationLicense

MIT

本帖隐藏的内容

decision-tree-cart-master.zip (1.16 MB)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Javascript regression regressio regress script

本帖被以下文库推荐

沙发
ReneeBK 发表于 2017-4-16 04:25:53 |只看作者 |坛友微信交流群
  1. 'use strict';

  2. var Tree = require('./TreeNode');
  3. var Matrix = require('ml-matrix');

  4. class DecisionTreeClassifier {

  5.     /**
  6.      * Create new Decision Tree Classifier with CART implementation with the given options
  7.      * @param {object} options
  8.      * @param {string} [options.gainFunction="gini"] - gain function to get the best split, "gini" the only one supported.
  9.      * @param {string} [options.splitFunction] - given two integers from a split feature, get the value to split, "mean" the only one supported.
  10.      * @param {number} [options.minNumSamples] - minimum number of samples to create a leaf node to decide a class. Default 3.
  11.      * @param {number} [options.maxDepth] - Max depth of the tree. Default Infinity.
  12.      * @param {object} model - for load purposes.
  13.      * @constructor
  14.      */
  15.     constructor(options, model) {
  16.         if (options === true) {
  17.             this.options = model.options;
  18.             this.root = new Tree(model.options);
  19.             this.root.setNodeParameters(model.root);
  20.         } else {
  21.             if (options === undefined) options = {};
  22.             if (options.gainFunction === undefined) options.gainFunction = 'gini';
  23.             if (options.splitFunction === undefined) options.splitFunction = 'mean';
  24.             if (options.minNumSamples === undefined) options.minNumSamples = 3;
  25.             if (options.maxDepth === undefined) options.maxDepth = Infinity;

  26.             options.kind = 'classifier';
  27.             this.options = options;
  28.         }
  29.     }

  30.     /**
  31.      * Train the decision tree with the given training set and labels.
  32.      * @param {Matrix} trainingSet
  33.      * @param {Array} trainingLabels
  34.      */
  35.     train(trainingSet, trainingLabels) {
  36.         this.root = new Tree(this.options);
  37.         if (!Matrix.isMatrix(trainingSet)) trainingSet = new Matrix(trainingSet);
  38.         this.root.train(trainingSet, trainingLabels, 0, null);
  39.     }

  40.     /**
  41.      * Predicts the output given the matrix to predict.
  42.      * @param {Matrix} toPredict
  43.      * @return {Array} predictions
  44.      */
  45.     predict(toPredict) {
  46.         var predictions = new Array(toPredict.length);

  47.         for (var i = 0; i < toPredict.length; ++i) {
  48.             predictions[i] = this.root.classify(toPredict[i]).maxRowIndex(0)[1];
  49.         }

  50.         return predictions;
  51.     }

  52.     /**
  53.      * Export the current model to JSON.
  54.      * @return {object} - Current model.
  55.      */
  56.     toJSON() {
  57.         var toSave = {
  58.             options: this.options,
  59.             root: {},
  60.             name: 'DTClassifier'
  61.         };

  62.         toSave.root = this.root;
  63.         return toSave;
  64.     }

  65.     /**
  66.      * Load a Decision tree classifier with the given model.
  67.      * @param {object} model
  68.      * @return {DecisionTreeClassifier}
  69.      */
  70.     static load(model) {
  71.         if (model.name !== 'DTClassifier') {
  72.             throw new RangeError('Invalid model: ' + model.name);
  73.         }

  74.         return new DecisionTreeClassifier(true, model);
  75.     }
  76. }

  77. module.exports = DecisionTreeClassifier;
复制代码

使用道具

藤椅
ReneeBK 发表于 2017-4-16 04:26:25 |只看作者 |坛友微信交流群
  1. 'use strict';

  2. var Tree = require('./TreeNode');
  3. var Matrix = require('ml-matrix');

  4. class DecisionTreeRegression {

  5.     /**
  6.      * Create new Decision Tree Regression with CART implementation with the given options.
  7.      * @param {object} options
  8.      * @param {string} [options.gainFunction="regression"] - gain function to get the best split, "regression" the only one supported.
  9.      * @param {string} [options.splitFunction] - given two integers from a split feature, get the value to split, "mean" the only one supported.
  10.      * @param {number} [options.minNumSamples] - minimum number of samples to create a leaf node to decide a class. Default 3.
  11.      * @param {number} [options.maxDepth] - Max depth of the tree. Default Infinity.
  12.      * @param {object} model - for load purposes.
  13.      */
  14.     constructor(options, model) {
  15.         if (options === true) {
  16.             this.options = model.options;
  17.             this.root = new Tree(model.options);
  18.             this.root.setNodeParameters(model.root);
  19.         } else {
  20.             if (options === undefined) options = {};
  21.             if (options.gainFunction === undefined) options.gainFunction = 'regression';
  22.             if (options.splitFunction === undefined) options.splitFunction = 'mean';
  23.             if (options.minNumSamples === undefined) options.minNumSamples = 3;
  24.             if (options.maxDepth === undefined) options.maxDepth = Infinity;

  25.             options.kind = 'regression';
  26.             this.options = options;
  27.         }
  28.     }

  29.     /**
  30.      * Train the decision tree with the given training set and values.
  31.      * @param {Matrix} trainingSet
  32.      * @param {Array} trainingValues
  33.      */
  34.     train(trainingSet, trainingValues) {
  35.         this.root = new Tree(this.options);
  36.         if (trainingSet[0].length === undefined) trainingSet = Matrix.columnVector(trainingSet);
  37.         if (!Matrix.isMatrix(trainingSet)) trainingSet = new Matrix(trainingSet);
  38.         this.root.train(trainingSet, trainingValues, 0);
  39.     }

  40.     /**
  41.      * Predicts the values given the matrix to predict.
  42.      * @param {Matrix} toPredict
  43.      * @return {Array} predictions
  44.      */
  45.     predict(toPredict) {
  46.         if (toPredict[0].length === undefined) toPredict = Matrix.columnVector(toPredict);
  47.         var predictions = new Array(toPredict.length);

  48.         for (var i = 0; i < toPredict.length; ++i) {
  49.             predictions[i] = this.root.classify(toPredict[i]);
  50.         }

  51.         return predictions;
  52.     }

  53.     /**
  54.      * Export the current model to JSON.
  55.      * @return {object} - Current model.
  56.      */
  57.     toJSON() {
  58.         var toSave = {
  59.             options: this.options,
  60.             root: {},
  61.             name: 'DTRegression'
  62.         };

  63.         toSave.root = this.root;
  64.         return toSave;
  65.     }

  66.     /**
  67.      * Load a Decision tree regression with the given model.
  68.      * @param {object} model
  69.      * @return {DecisionTreeRegression}
  70.      */
  71.     static load(model) {
  72.         if (model.name !== 'DTRegression') {
  73.             throw new RangeError('Invalid model:' + model.name);
  74.         }

  75.         return new DecisionTreeRegression(true, model);
  76.     }
  77. }

  78. module.exports = DecisionTreeRegression;
复制代码

使用道具

板凳
ekscheng 发表于 2017-4-16 08:52:24 |只看作者 |坛友微信交流群
已有 1 人评分论坛币 收起 理由
Nicolle + 20 精彩帖子

总评分: 论坛币 + 20   查看全部评分

使用道具

报纸
20115326 学生认证  发表于 2017-4-16 12:00:48 |只看作者 |坛友微信交流群
好东西啊,学习一下

使用道具

地板
20115326 学生认证  发表于 2017-4-16 12:01:01 |只看作者 |坛友微信交流群
好东西啊,学习一下

使用道具

7
franky_sas 发表于 2017-4-16 13:39:24 |只看作者 |坛友微信交流群

使用道具

8
bocm 发表于 2017-4-18 01:58:16 |只看作者 |坛友微信交流群
thanks for sharing

使用道具

9
WFMZZ 发表于 2017-4-19 09:56:06 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 03:14