tree.DecisionTreeClassifier
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
⊕ DecisionTreeClassifier(__namedParameters: `object`)
Defined in tree/tree.ts:152
Parameters:
Param | Type | Default | Description |
---|---|---|---|
options.featureLabels | any[] | null | |
options.random_state | number | null | |
options.verbose | boolean | false |
Returns: DecisionTreeClassifier
Methods
λ fit
Fit date, which builds a tree
Defined in tree/tree.ts:183
Parameters:
Param | Type | Default | Description |
---|---|---|---|
X | string[][] or number[][] or boolean[][] | null | 2D Matrix of training |
y | string[] or number[] or boolean[] | null | 1D Vector of target |
Returns:
void
λ fromJSON
Restores the model from a checkpoint
Defined in tree/tree.ts:243
Parameters:
Param | Type | Default | Description |
---|---|---|---|
options.featureLabels | string[] | null | |
options.random_state | number | null | |
options.tree | any | null | |
options.verbose | boolean | false |
Returns:
void
λ predict
Predict multiple rows
Defined in tree/tree.ts:196
Parameters:
Param | Type | Default | Description |
---|---|---|---|
X | string[][] or number[][] or boolean[][] | [] | 2D Matrix of testing data |
Returns:
any[]
λ printTree
Recursively print the tree into console
Defined in tree/tree.ts:264
Parameters:
Param | Type | Default | Description |
---|---|---|---|
spacing | string | '' | Spacing used when printing the tree into the terminal |
Returns:
void
λ toJSON
Returns the model checkpoint
Defined in tree/tree.ts:210
Returns:
Param | Type | Description |
---|---|---|
featureLabels | string[] | Literal names for each feature to be used while printing the tree out as a string |
random_state | number | A seed value for the random engine |
tree | any | The model's state |
verbose | boolean | Logs the progress of the tree construction as console.info |