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>
impl<F: Float, L: Label> DecisionTree<F, L>
Sourcepub fn iter_nodes(&self) -> NodeIter<'_, F, L> ⓘ
pub fn iter_nodes(&self) -> NodeIter<'_, F, L> ⓘ
Create a node iterator in level-order (BFT)
Sourcepub fn mean_impurity_decrease(&self) -> Vec<F>
pub fn mean_impurity_decrease(&self) -> Vec<F>
Return the mean impurity decrease for each feature
Sourcepub fn relative_impurity_decrease(&self) -> Vec<F>
pub fn relative_impurity_decrease(&self) -> Vec<F>
Return the relative impurity decrease for each feature
Sourcepub fn feature_importance(&self) -> Vec<F>
pub fn feature_importance(&self) -> Vec<F>
Return the feature importance, i.e. the relative impurity decrease, for each feature
Sourcepub fn num_leaves(&self) -> usize
pub fn num_leaves(&self) -> usize
Return the number of leaves in this tree
Sourcepub fn export_to_tikz(&self) -> Tikz<'_, F, L>
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>
impl<F: Float, L: Label> DecisionTree<F, L>
Sourcepub fn params() -> DecisionTreeParams<F, L>
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>
impl<F: Clone + Float, L: Clone + Label> Clone for DecisionTree<F, L>
Source§fn clone(&self) -> DecisionTree<F, L>
fn clone(&self) -> DecisionTree<F, L>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§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>
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>
impl<F: Float, L: Label> StructuralPartialEq for DecisionTree<F, L>
Auto Trait Implementations§
impl<F, L> Freeze for DecisionTree<F, L>
impl<F, L> RefUnwindSafe for DecisionTree<F, L>where
F: RefUnwindSafe,
L: RefUnwindSafe,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<'a, F, R, T, S, O> Predict<&'a DatasetBase<R, T>, S> for Owhere
R: Records<Elem = F>,
O: PredictInplace<R, S>,
impl<'a, F, R, T, S, O> Predict<&'a DatasetBase<R, T>, S> for Owhere
R: Records<Elem = F>,
O: PredictInplace<R, S>,
§impl<F, D, E, T, O> Predict<ArrayBase<D, Dim<[usize; 2]>>, DatasetBase<ArrayBase<D, Dim<[usize; 2]>>, T>> for O
impl<F, D, E, T, O> Predict<ArrayBase<D, Dim<[usize; 2]>>, DatasetBase<ArrayBase<D, Dim<[usize; 2]>>, T>> for O
§impl<F, R, T, E, S, O> Predict<DatasetBase<R, T>, DatasetBase<R, S>> for Owhere
R: Records<Elem = F>,
S: AsTargets<Elem = E>,
O: PredictInplace<R, S>,
impl<F, R, T, E, S, O> Predict<DatasetBase<R, T>, DatasetBase<R, S>> for Owhere
R: Records<Elem = F>,
S: AsTargets<Elem = E>,
O: PredictInplace<R, S>,
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.