pub struct DecisionTree<F: Float, L: Label> { /* private fields */ }
Expand description

A fitted decision tree model for classification.

Structure

A decision tree structure is a binary tree where:

  • Each internal node specifies a decision, represented by a choice of a feature and a “split value” such that all observations for which feature <= split_value is true fall in the left subtree, while the others fall in the right subtree.

  • leaf nodes make predictions, and their prediction is the most popular label in the node

Algorithm

Starting with a single root node, decision trees are trained recursively by applying the following rule to every node considered:

  • Find the best split value for each feature of the observations belonging in the node;
  • Select the feature (and its best split value) that maximizes the quality of the split;
  • If the score of the split is sufficiently larger than the score of the unsplit node, then two child nodes are generated, the left one containing all observations with feature <= split value and the right one containing the rest.
  • If no suitable split is found, the node is marked as leaf and its prediction is set to be the most common label in the node;

The quality score used can be specified in the parameters.

Predictions

To predict the label of a sample, the tree is traversed from the root to a leaf, choosing between left and right children according to the values of the features of the sample. The final prediction for the sample is the prediction of the reached leaf.

Additional constraints

In order to avoid overfitting the training data, some additional constraints on the quality/quantity of splits can be added to the tree. A description of these additional rules is provided in the parameters page.

Example

Here is an example on how to train a decision tree from its parameters:


use linfa_trees::DecisionTree;
use linfa::prelude::*;
use linfa_datasets;

// Load the dataset
let dataset = linfa_datasets::iris();
// Fit the tree
let tree = DecisionTree::params().fit(&dataset).unwrap();
// Get accuracy on training set
let accuracy = tree.predict(&dataset).confusion_matrix(&dataset).unwrap().accuracy();

assert!(accuracy > 0.9);

Implementations

Create a node iterator in level-order (BFT)

Return features_idx of this tree (BFT)

Return the mean impurity decrease for each feature

Return the relative impurity decrease for each feature

Return the feature importance, i.e. the relative impurity decrease, for each feature

Return root node of the tree

Return max depth of the tree

Return the number of leaves in this tree

Generates a Tikz structure to print the fitted tree in Tex using tikz and forest, with the following default parameters:

  • legend=false
  • complete=true

Defaults are provided if the optional parameters are not specified:

  • split_quality = SplitQuality::Gini
  • max_depth = None
  • min_weight_split = 2.0
  • min_weight_leaf = 1.0
  • min_impurity_decrease = 0.00001

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Make predictions for each row of a matrix of features x.

Create targets that predict_inplace works with.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.