Home
API
Examples
Github
Home
API
Examples
Github
  • API

    • cluster

      • KMeans
    • datasets

      • Boston
      • HeartDisease
      • Iris
    • decomposition

      • PCA
    • ensemble

      • BaggingClassifier
      • RandomForestClassifier
    • feature_extraction

      • CountVectorizer
    • linear_model

      • Lasso
      • LinearRegression
      • LogisticRegression
      • Ridge
      • SGDClassifier
      • SGDRegressor
    • metrics

      • accuracyScore
      • confusion_matrix
      • mean_absolute_error
      • mean_squared_error
      • mean_squared_log_error
      • zeroOneLoss
    • model_selection

      • KFold
      • train_test_split
    • naive_bayes

      • GaussianNB
      • MultinomialNB
    • neighbors

      • KNeighborsClassifier
    • preprocessing

      • Binarizer
      • Imputer
      • LabelEncoder
      • MinMaxScaler
      • OneHotEncoder
      • PolynomialFeatures
      • add_dummy_feature
      • normalize
    • svm

      • BaseSVM
      • NuSVC
      • NuSVR
      • OneClassSVM
      • SVC
      • SVR
    • tree

      • DecisionTreeClassifier

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

  • constructor

Methods

  • fit

  • fromJSON

  • predict

  • printTree

  • toJSON

Constructors


constructor

⊕ DecisionTreeClassifier(__namedParameters: `object`)

Defined in tree/tree.ts:152

Parameters:

ParamTypeDefaultDescription
options.featureLabelsany[]null
options.random_statenumbernull
options.verbosebooleanfalse

Returns: DecisionTreeClassifier

Methods


λ fit

Fit date, which builds a tree

Defined in tree/tree.ts:183

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:243

Parameters:

ParamTypeDefaultDescription
options.featureLabelsstring[]null
options.random_statenumbernull
options.treeanynull
options.verbosebooleanfalse

Returns:

void

λ predict

Predict multiple rows

Defined in tree/tree.ts:196

Parameters:

ParamTypeDefaultDescription
Xstring[][] 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:

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

Returns:

void

λ toJSON

Returns the model checkpoint

Defined in tree/tree.ts:210

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