Skip to main content

RandomForestClassifier

Struct RandomForestClassifier 

Source
pub struct RandomForestClassifier { /* private fields */ }
Expand description

Random-forest classifier using bootstrap aggregation of CART classifiers.

The default is 100 bootstrap trees with max_features = sqrt(n_features) at each split, matching scikit-learn’s RandomForestClassifier defaults. random_state defaults to zero so fits are reproducible; call with_random_state to choose another seed.

use datarust::ensemble::RandomForestClassifier;
use datarust::traits::{PredictProba, Predictor};
use datarust::Matrix;

let x = Matrix::new(vec![
    vec![0.0, 0.0], vec![0.0, 1.0],
    vec![1.0, 0.0], vec![1.0, 1.0],
])?;
let y = vec![0.0, 1.0, 1.0, 0.0];
let mut forest = RandomForestClassifier::new()
    .with_n_estimators(25)
    .with_random_state(7);
forest.fit(&x, &y)?;
let predictions = forest.predict(&x)?;
let probabilities = forest.predict_proba(&x)?;
assert_eq!(predictions.len(), 4);
assert_eq!(probabilities.ncols(), 2);

Implementations§

Source§

impl RandomForestClassifier

Source

pub fn new() -> Self

Creates a classifier with scikit-learn-style defaults.

Source

pub fn with_n_estimators(self, n: usize) -> Self

Sets the number of trees.

Source

pub fn with_criterion(self, criterion: Criterion) -> Self

Sets the tree split criterion (Gini or Entropy). Criterion::SquaredError is regression-only and is rejected by fit.

Source

pub fn with_max_depth(self, depth: Option<usize>) -> Self

Sets the maximum depth of each tree (None means unlimited).

Source

pub fn with_min_samples_split(self, n: usize) -> Self

Sets the minimum samples required to split a node.

Source

pub fn with_min_samples_leaf(self, n: usize) -> Self

Sets the minimum samples required in each leaf.

Source

pub fn with_min_impurity_decrease(self, value: f64) -> Self

Sets the minimum impurity decrease required for a split.

Source

pub fn with_max_features(self, value: MaxFeatures) -> Self

Sets the number of features considered at each split.

Source

pub fn with_bootstrap(self, bootstrap: bool) -> Self

Enables or disables bootstrap sampling.

Source

pub fn with_random_state(self, seed: u64) -> Self

Sets the deterministic forest seed.

Source

pub fn n_estimators(&self) -> usize

Number of fitted trees.

Source

pub fn n_features_in(&self) -> usize

Number of features seen during fitting.

Source

pub fn trees(&self) -> &[DecisionTreeClassifier]

Borrow the fitted decision trees.

Source

pub fn classes(&self) -> Option<&[f64]>

Sorted original class labels.

Source

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

Regenerates the row indices used to fit each tree (scikit-learn’s estimators_samples_ semantics). With bootstrap enabled, each inner vector contains n_samples draws with replacement; with bootstrap disabled it is the original row order. Indices are derived from the stored per-tree seeds on demand, so the fitted model does not retain n_estimators × n_samples indices.

Source

pub fn feature_importances(&self) -> Result<Vec<f64>>

Mean normalized impurity reduction across the non-trivial fitted trees, matching sklearn’s feature_importances_ behavior.

Trait Implementations§

Source§

impl Classifier for RandomForestClassifier

Source§

fn score(&self, x: &Matrix, y: &[f64]) -> Result<f64>

Mean classification accuracy, mirroring sklearn’s default classifier score.
Source§

impl Clone for RandomForestClassifier

Source§

fn clone(&self) -> RandomForestClassifier

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RandomForestClassifier

Source§

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

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

impl Default for RandomForestClassifier

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Estimator for RandomForestClassifier

Source§

impl Params for RandomForestClassifier

Source§

fn get_params(&self) -> Vec<(&'static str, ParamValue)>

Returns the current hyperparameters as (name, value) pairs.
Source§

fn set_params(&mut self, name: &str, value: ParamValue) -> Result<()>

Applies a named hyperparameter. Returns an error if name is unknown or value is the wrong type for that parameter.
Source§

impl PredictProba for RandomForestClassifier

Source§

fn predict_proba(&self, x: &Matrix) -> Result<Matrix>

Return per-class probability estimates.
Source§

impl Predictor for RandomForestClassifier

Source§

fn fit(&mut self, x: &Matrix, y: &[f64]) -> Result<()>

Fit the estimator on training features and target values.
Source§

fn predict(&self, x: &Matrix) -> Result<Vec<f64>>

Predict one numeric value per input row. For classifiers this is the predicted class label; probability estimates are exposed separately by PredictProba.
Source§

fn is_fitted(&self) -> bool

Whether the estimator has learned fitted state.
Source§

fn fit_predict(&mut self, x: &Matrix, y: &[f64]) -> Result<Vec<f64>>

Convenience: fit then predict on the same data.

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

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

Source§

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>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.