Skip to main content

Module random_forest_classifier

Module random_forest_classifier 

Source
Expand description

Random forest classifier

§Random Forest Classifier

A random forest is an ensemble estimator that fits multiple decision trees to random subsets of the dataset and averages predictions to improve the predictive accuracy and control over-fitting. See ensemble models for more details.

Bigger number of estimators in general improves performance of the algorithm with an increased cost of training time. The random sample of m predictors is typically set to be \(\sqrt{p}\) from the full set of p predictors.

Example:

use smartcore::linalg::basic::matrix::DenseMatrix;
use smartcore::ensemble::random_forest_classifier::RandomForestClassifier;

// Iris dataset
let x = DenseMatrix::from_2d_array(&[
             &[5.1, 3.5, 1.4, 0.2],
             &[4.9, 3.0, 1.4, 0.2],
             &[4.7, 3.2, 1.3, 0.2],
             &[4.6, 3.1, 1.5, 0.2],
             &[5.0, 3.6, 1.4, 0.2],
             &[5.4, 3.9, 1.7, 0.4],
             &[4.6, 3.4, 1.4, 0.3],
             &[5.0, 3.4, 1.5, 0.2],
             &[4.4, 2.9, 1.4, 0.2],
             &[4.9, 3.1, 1.5, 0.1],
             &[7.0, 3.2, 4.7, 1.4],
             &[6.4, 3.2, 4.5, 1.5],
             &[6.9, 3.1, 4.9, 1.5],
             &[5.5, 2.3, 4.0, 1.3],
             &[6.5, 2.8, 4.6, 1.5],
             &[5.7, 2.8, 4.5, 1.3],
             &[6.3, 3.3, 4.7, 1.6],
             &[4.9, 2.4, 3.3, 1.0],
             &[6.6, 2.9, 4.6, 1.3],
             &[5.2, 2.7, 3.9, 1.4],
        ]).unwrap();
let y = vec![
             0, 0, 0, 0, 0, 0, 0, 0,
             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        ];

let classifier = RandomForestClassifier::fit(&x, &y, Default::default()).unwrap();
let y_hat = classifier.predict(&x).unwrap(); // use the same data for prediction

Structs§

RandomForestClassifier
Random Forest Classifier
RandomForestClassifierParameters
Parameters of the Random Forest algorithm. Some parameters here are passed directly into base estimator.
RandomForestClassifierSearchParameters
RandomForestClassifier grid search parameters
RandomForestClassifierSearchParametersIterator
RandomForestClassifier grid search iterator