Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added per-class probability prediction for random forests #138

Closed
33 changes: 32 additions & 1 deletion src/ensemble/random_forest_classifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ use serde::{Deserialize, Serialize};

use crate::api::{Predictor, SupervisedEstimator};
use crate::error::{Failed, FailedError};
use crate::linalg::Matrix;
use crate::linalg::naive::dense_matrix::DenseMatrix;
use crate::linalg::{BaseMatrix, Matrix};
use crate::math::num::RealNumber;
use crate::tree::decision_tree_classifier::{
which_max, DecisionTreeClassifier, DecisionTreeClassifierParameters, SplitCriterion,
Expand Down Expand Up @@ -316,6 +317,36 @@ impl<T: RealNumber> RandomForestClassifier<T> {
which_max(&result)
}

/// Predict the per-class probabilties for each observation. The probability is calculated as the fraction of trees that predicted a given class
pub fn predict_probs<M: Matrix<T>>(&self, x: &M) -> Result<DenseMatrix<f64>, Failed> {
Mec-iS marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in scikit it is called predict_proba, I think that it is better to keep the same name

let mut result = DenseMatrix::<f64>::zeros(x.shape().0, self.classes.len());

let (n, _) = x.shape();

for i in 0..n {
let row_probs = self.predict_probs_for_row(x, i);

for j in 0..row_probs.len() {
result.set(i, j, row_probs[j]);
}
}

Ok(result)
}

fn predict_probs_for_row<M: Matrix<T>>(&self, x: &M, row: usize) -> Vec<f64> {
let mut result = vec![0; self.classes.len()];

for tree in self.trees.iter() {
result[tree.predict_for_row(x, row)] += 1;
}

result
.iter()
.map(|n| *n as f64 / self.trees.len() as f64)
.collect()
}

fn sample_with_replacement(y: &[usize], num_classes: usize, rng: &mut impl Rng) -> Vec<usize> {
let class_weight = vec![1.; num_classes];
let nrows = y.len();
Expand Down