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§

source§

impl<F: Float, L: Label> DecisionTree<F, L>

source

pub fn iter_nodes(&self) -> NodeIter<'_, F, L>

Create a node iterator in level-order (BFT)

source

pub fn features(&self) -> Vec<usize>

Return features_idx of this tree (BFT)

source

pub fn mean_impurity_decrease(&self) -> Vec<F>

Return the mean impurity decrease for each feature

source

pub fn relative_impurity_decrease(&self) -> Vec<F>

Return the relative impurity decrease for each feature

source

pub fn feature_importance(&self) -> Vec<F>

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

source

pub fn root_node(&self) -> &TreeNode<F, L>

Return root node of the tree

source

pub fn max_depth(&self) -> usize

Return max depth of the tree

source

pub fn num_leaves(&self) -> usize

Return the number of leaves in this tree

source

pub fn export_to_tikz(&self) -> Tikz<'_, F, L>

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

  • legend=false
  • complete=true
source§

impl<F: Float, L: Label> DecisionTree<F, L>

source

pub fn params() -> DecisionTreeParams<F, L>

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§

source§

impl<F: Clone + Float, L: Clone + Label> Clone for DecisionTree<F, L>

source§

fn clone(&self) -> DecisionTree<F, L>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<F: Debug + Float, L: Debug + Label> Debug for DecisionTree<F, L>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<F: PartialEq + Float, L: PartialEq + Label> PartialEq for DecisionTree<F, L>

source§

fn eq(&self, other: &DecisionTree<F, L>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<F: Float, L: Label + Default, D: Data<Elem = F>> PredictInplace<ArrayBase<D, Dim<[usize; 2]>>, ArrayBase<OwnedRepr<L>, Dim<[usize; 1]>>> for DecisionTree<F, L>

source§

fn predict_inplace(&self, x: &ArrayBase<D, Ix2>, y: &mut Array1<L>)

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

source§

fn default_target(&self, x: &ArrayBase<D, Ix2>) -> Array1<L>

Create targets that predict_inplace works with.
source§

impl<F: Float, L: Label> StructuralPartialEq for DecisionTree<F, L>

Auto Trait Implementations§

§

impl<F, L> RefUnwindSafe for DecisionTree<F, L>

§

impl<F, L> Send for DecisionTree<F, L>
where L: Send,

§

impl<F, L> Sync for DecisionTree<F, L>
where L: Sync,

§

impl<F, L> Unpin for DecisionTree<F, L>
where L: Unpin,

§

impl<F, L> UnwindSafe for DecisionTree<F, L>
where F: UnwindSafe, L: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<'a, F, D, DM, T, O> Predict<&'a ArrayBase<D, DM>, T> for O
where D: Data<Elem = F>, DM: Dimension, O: PredictInplace<ArrayBase<D, DM>, T>,

source§

fn predict(&self, records: &'a ArrayBase<D, DM>) -> T

source§

impl<'a, F, R, T, S, O> Predict<&'a DatasetBase<R, T>, S> for O
where R: Records<Elem = F>, O: PredictInplace<R, S>,

source§

fn predict(&self, ds: &'a DatasetBase<R, T>) -> S

source§

impl<F, D, E, T, O> Predict<ArrayBase<D, Dim<[usize; 2]>>, DatasetBase<ArrayBase<D, Dim<[usize; 2]>>, T>> for O
where D: Data<Elem = F>, T: AsTargets<Elem = E>, O: PredictInplace<ArrayBase<D, Dim<[usize; 2]>>, T>,

source§

fn predict( &self, records: ArrayBase<D, Dim<[usize; 2]>> ) -> DatasetBase<ArrayBase<D, Dim<[usize; 2]>>, T>

source§

impl<F, R, T, E, S, O> Predict<DatasetBase<R, T>, DatasetBase<R, S>> for O
where R: Records<Elem = F>, S: AsTargets<Elem = E>, O: PredictInplace<R, S>,

source§

fn predict(&self, ds: DatasetBase<R, T>) -> DatasetBase<R, S>

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V