Skip to content

mljs/decision-tree-cart

Repository files navigation

ml-cart (Classification and regression trees)

NPM version build status npm download

Decision trees using CART implementation.

Installation

npm i ml-cart

Usage

As a classifier

import irisDataset from 'ml-dataset-iris';
import { DecisionTreeClassifier as DTClassifier } from 'ml-cart';

const trainingSet = irisDataset.getNumbers();
const predictions = irisDataset
  .getClasses()
  .map((elem) => irisDataset.getDistinctClasses().indexOf(elem));

const options = {
  gainFunction: 'gini',
  maxDepth: 10,
  minNumSamples: 3,
};

const classifier = new DTClassifier(options);
classifier.train(trainingSet, predictions);
const result = classifier.predict(trainingSet);

As a regression

import { DecisionTreeRegression as DTRegression } from 'ml-cart';

const x = new Array(100);
const y = new Array(100);
const val = 0.0;
for (let i = 0; i < x.length; ++i) {
  x[i] = val;
  y[i] = Math.sin(x[i]);
  val += 0.01;
}

const reg = new DTRegression();
reg.train(x, y);
const estimations = reg.predict(x);

License

MIT