tree.DecisionTreeClassifier

A decision tree classifier.

Usage

import { DecisionTreeClassifier } from 'machinelearn/tree';
const features = ['color', 'diameter', 'label'];
const decision = new DecisionTreeClassifier({ featureLabels: features });

const X = [['Green', 3], ['Yellow', 3], ['Red', 1], ['Red', 1], ['Yellow', 3]];
const y = ['Apple', 'Apple', 'Grape', 'Grape', 'Lemon'];
decision.fit({ X, y });
decision.printTree(); // try it out yourself! =)

decision.predict({ X: [['Green', 3]] }); // [ 'Apple' ]
decision.predict({ X }); // [ [ 'Apple' ], [ 'Apple', 'Lemon' ], [ 'Grape', 'Grape' ], [ 'Grape', 'Grape' ], [ 'Apple', 'Lemon' ] ]
import { DecisionTreeClassifier } from 'machinelearn/tree';
const decision = new DecisionTreeClassifier({ featureLabels: null });

const X = [[0, 0], [1, 1]];
const Y = [0, 1];
decision.fit({ X, y });
decision2.predict({ row: [[2, 2]] }); // [ 1 ]

Constructors

Methods

Constructors


constructor

new DecisionTreeClassifier(__namedParameters: `object`)

Defined in tree/tree.ts:148

Parameters:

ParamTypeDefaultDescription
options.featureLabelsany[]nullLiteral names for each feature to be used while printing the tree out as a string
options.randomstatenumbernullA seed value for the random engine
options.verbosebooleanfalseLogs the progress of the tree construction as console.info

Returns: DecisionTreeClassifier

Methods


λ fit

Fit date, which builds a tree

Defined in tree/tree.ts:187

Parameters:

ParamTypeDefaultDescription
Xstring[][] or number[][] or boolean[][]null2D Matrix of training
ystring[] or number[] or boolean[]null1D Vector of target

Returns:

void

λ fromJSON

Restores the model from a checkpoint

Defined in tree/tree.ts:247

Parameters:

ParamTypeDefaultDescription
options.featureLabelsstring[]nullLiteral names for each feature to be used while printing the tree out as a string
options.randomstatenumbernullA seed value for the random engine
options.treeanynullThe model's state
options.verbosebooleanfalseLogs the progress of the tree construction as console.info

Returns:

void

λ predict

Predict multiple rows

Defined in tree/tree.ts:200

Parameters:

ParamTypeDefaultDescription
Xstring[][] or boolean[][] or number[][][]2D Matrix of testing data

Returns:

any[]

λ printTree

Recursively print the tree into console

Defined in tree/tree.ts:268

Parameters:

ParamTypeDefaultDescription
spacingstring""Spacing used when printing the tree into the terminal

Returns:

void

λ toJSON

Returns the model checkpoint

Defined in tree/tree.ts:214

Returns:

ParamTypeDescription
featureLabelsstring[]Literal names for each feature to be used while printing the tree out as a string
random_statenumberA seed value for the random engine
treeanyThe model's state
verbosebooleanLogs the progress of the tree construction as console.info