use super::parameters::{MaxFeatures, RandomForestClassifierParams, RandomForestRegressorParams};
use super::training::{Classification, ForestConfig, Regression, train_forest};
use super::tree::{FEATURE_MASK, PackedTree};
use crate::api::{Classifier, Estimator, HasParams, ModelError, Regressor};
use crate::data::{BinaryTargets, MatrixView, RegressionTargets};
#[derive(Clone, Debug, PartialEq)]
pub struct RandomForestClassifier {
pub(super) n_features_in: usize,
pub(super) params: RandomForestClassifierParams,
pub(super) classes: Vec<u8>,
pub(super) trees: Vec<PackedTree>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct RandomForestRegressor {
pub(super) n_features_in: usize,
pub(super) params: RandomForestRegressorParams,
pub(super) trees: Vec<PackedTree>,
}
impl RandomForestClassifier {
#[inline]
pub fn n_features_in(&self) -> usize {
self.n_features_in
}
#[inline]
pub fn get_params(&self) -> &RandomForestClassifierParams {
&self.params
}
#[inline]
pub fn classes(&self) -> &[u8] {
&self.classes
}
pub fn fit(
data: &MatrixView<'_>,
targets: &BinaryTargets,
params: RandomForestClassifierParams,
) -> Result<Self, ModelError> {
let config = ForestConfig::from(¶ms);
validate_common(data, targets.as_slice().len(), &config)?;
for (index, &value) in targets.as_slice().iter().enumerate() {
if value > 1 {
return Err(ModelError::InvalidBinaryTarget { index, value });
}
}
let saw_zero = targets.as_slice().contains(&0);
let saw_one = targets.as_slice().contains(&1);
let classes = match (saw_zero, saw_one) {
(true, true) => vec![0, 1],
(true, false) => vec![0],
(false, true) => vec![1],
(false, false) => unreachable!("non-empty validated binary targets"),
};
let trees = train_forest(data, targets.as_slice(), &config, Classification)?;
Ok(Self {
n_features_in: data.columns(),
params,
classes,
trees,
})
}
pub fn predict_one(&self, row: &[f32]) -> Result<u8, ModelError> {
check_row(row, self.n_features_in)?;
if self.classes.len() == 1 {
return Ok(self.classes[0]);
}
let positive = mean_tree_prediction(&self.trees, row).clamp(0.0, 1.0);
Ok(u8::from(positive > 0.5))
}
pub fn predict(&self, data: &MatrixView<'_>) -> Result<Vec<u8>, ModelError> {
if self.classes.len() == 1 {
check_prediction_data(data, data.rows(), data.rows(), self.n_features_in)?;
return Ok(vec![self.classes[0]; data.rows()]);
}
let mut scores = vec![0.0; data.rows()];
self.accumulate_positive_into(data, &mut scores)?;
Ok(scores
.into_iter()
.map(|positive| u8::from(positive > 0.5))
.collect())
}
pub fn predict_into(&self, data: &MatrixView<'_>, output: &mut [u8]) -> Result<(), ModelError> {
check_prediction_data(data, output.len(), data.rows(), self.n_features_in)?;
if self.classes.len() == 1 {
output.fill(self.classes[0]);
return Ok(());
}
for (row, slot) in data.iter_rows().zip(output) {
let positive = mean_tree_prediction(&self.trees, row).clamp(0.0, 1.0);
*slot = u8::from(positive > 0.5);
}
Ok(())
}
pub fn predict_proba_one(&self, row: &[f32]) -> Result<Vec<f32>, ModelError> {
let mut output = vec![0.0; self.classes.len()];
self.predict_proba_one_into(row, &mut output)?;
Ok(output)
}
pub fn predict_proba_one_into(
&self,
row: &[f32],
output: &mut [f32],
) -> Result<(), ModelError> {
check_row(row, self.n_features_in)?;
check_output_len(output.len(), self.classes.len())?;
if self.classes.len() == 1 {
output[0] = 1.0;
} else {
let positive = mean_tree_prediction(&self.trees, row).clamp(0.0, 1.0);
output[0] = 1.0 - positive;
output[1] = positive;
}
Ok(())
}
pub fn predict_proba(&self, data: &MatrixView<'_>) -> Result<Vec<f32>, ModelError> {
<Self as Classifier>::predict_proba(self, data)
}
pub fn predict_proba_into(
&self,
data: &MatrixView<'_>,
output: &mut [f32],
) -> Result<(), ModelError> {
let expected = probability_output_len(data.rows(), self.classes.len())?;
check_prediction_data(data, output.len(), expected, self.n_features_in)?;
if self.classes.len() == 1 {
output.fill(1.0);
return Ok(());
}
output.fill(0.0);
for tree in &self.trees {
for (row, probabilities) in data.iter_rows().zip(output.chunks_exact_mut(2)) {
probabilities[1] += tree.predict(row);
}
}
for probabilities in output.chunks_exact_mut(2) {
let positive = (probabilities[1] / self.trees.len() as f32).clamp(0.0, 1.0);
probabilities[0] = 1.0 - positive;
probabilities[1] = positive;
}
Ok(())
}
pub fn predict_class_proba_one(&self, row: &[f32], class: u8) -> Result<f32, ModelError> {
check_row(row, self.n_features_in)?;
let class_index = self.class_index(class)?;
if self.classes.len() == 1 {
return Ok(1.0);
}
let positive = mean_tree_prediction(&self.trees, row).clamp(0.0, 1.0);
Ok(if class_index == 0 {
1.0 - positive
} else {
positive
})
}
pub fn predict_class_proba(
&self,
data: &MatrixView<'_>,
class: u8,
) -> Result<Vec<f32>, ModelError> {
<Self as Classifier>::predict_class_proba(self, data, class)
}
pub fn predict_class_proba_into(
&self,
data: &MatrixView<'_>,
class: u8,
output: &mut [f32],
) -> Result<(), ModelError> {
let class_index = self.class_index(class)?;
check_prediction_data(data, output.len(), data.rows(), self.n_features_in)?;
if self.classes.len() == 1 {
output.fill(1.0);
return Ok(());
}
self.accumulate_positive_into(data, output)?;
if class_index == 0 {
for slot in output {
*slot = 1.0 - *slot;
}
}
Ok(())
}
fn accumulate_positive_into(
&self,
data: &MatrixView<'_>,
output: &mut [f32],
) -> Result<(), ModelError> {
check_prediction_data(data, output.len(), data.rows(), self.n_features_in)?;
output.fill(0.0);
for tree in &self.trees {
for (row, slot) in data.iter_rows().zip(output.iter_mut()) {
*slot += tree.predict(row);
}
}
for slot in output {
*slot = (*slot / self.trees.len() as f32).clamp(0.0, 1.0);
}
Ok(())
}
#[inline]
fn class_index(&self, class: u8) -> Result<usize, ModelError> {
self.classes
.binary_search(&class)
.map_err(|_| ModelError::UnknownClass { class })
}
pub fn predict_positive_proba(&self, row: &[f32]) -> Result<f32, ModelError> {
check_row(row, self.n_features_in)?;
if self.classes.len() == 1 {
return Ok(f32::from(self.classes[0]));
}
Ok(mean_tree_prediction(&self.trees, row).clamp(0.0, 1.0))
}
pub fn predict_positive_proba_into(
&self,
data: &MatrixView<'_>,
output: &mut [f32],
) -> Result<(), ModelError> {
check_prediction_data(data, output.len(), data.rows(), self.n_features_in)?;
if self.classes.len() == 1 {
output.fill(f32::from(self.classes[0]));
return Ok(());
}
for (index, slot) in output.iter_mut().enumerate() {
*slot =
mean_tree_prediction(&self.trees, data.row(index).expect("validated row index"))
.clamp(0.0, 1.0);
}
Ok(())
}
#[cfg(test)]
pub(crate) fn to_bytes(&self) -> Vec<u8> {
packed_model_bytes(self.n_features_in, &self.trees, b"FRFC")
}
}
impl Estimator for RandomForestClassifier {
fn n_features_in(&self) -> usize {
self.n_features_in
}
}
impl Classifier for RandomForestClassifier {
fn classes(&self) -> &[u8] {
&self.classes
}
fn predict_into(&self, data: &MatrixView<'_>, output: &mut [u8]) -> Result<(), ModelError> {
RandomForestClassifier::predict_into(self, data, output)
}
fn predict_proba_into(
&self,
data: &MatrixView<'_>,
output: &mut [f32],
) -> Result<(), ModelError> {
RandomForestClassifier::predict_proba_into(self, data, output)
}
fn predict_class_proba_into(
&self,
data: &MatrixView<'_>,
class: u8,
output: &mut [f32],
) -> Result<(), ModelError> {
RandomForestClassifier::predict_class_proba_into(self, data, class, output)
}
}
impl HasParams for RandomForestClassifier {
type Params = RandomForestClassifierParams;
fn get_params(&self) -> &Self::Params {
&self.params
}
}
impl RandomForestRegressor {
#[inline]
pub fn n_features_in(&self) -> usize {
self.n_features_in
}
#[inline]
pub fn get_params(&self) -> &RandomForestRegressorParams {
&self.params
}
pub fn fit(
data: &MatrixView<'_>,
targets: &RegressionTargets,
params: RandomForestRegressorParams,
) -> Result<Self, ModelError> {
let config = ForestConfig::from(¶ms);
validate_common(data, targets.as_slice().len(), &config)?;
for (index, value) in targets.as_slice().iter().enumerate() {
if !value.is_finite() {
return Err(ModelError::NonFiniteTarget { index });
}
}
let trees = train_forest(data, targets.as_slice(), &config, Regression)?;
Ok(Self {
n_features_in: data.columns(),
params,
trees,
})
}
pub fn predict_one(&self, row: &[f32]) -> Result<f32, ModelError> {
check_row(row, self.n_features_in)?;
Ok(mean_tree_prediction(&self.trees, row))
}
pub fn predict(&self, data: &MatrixView<'_>) -> Result<Vec<f32>, ModelError> {
<Self as Regressor>::predict(self, data)
}
pub fn predict_into(
&self,
data: &MatrixView<'_>,
output: &mut [f32],
) -> Result<(), ModelError> {
check_prediction_data(data, output.len(), data.rows(), self.n_features_in)?;
for (index, slot) in output.iter_mut().enumerate() {
*slot =
mean_tree_prediction(&self.trees, data.row(index).expect("validated row index"));
}
Ok(())
}
#[cfg(test)]
pub(crate) fn to_bytes(&self) -> Vec<u8> {
packed_model_bytes(self.n_features_in, &self.trees, b"FRFR")
}
}
impl Estimator for RandomForestRegressor {
fn n_features_in(&self) -> usize {
self.n_features_in
}
}
impl Regressor for RandomForestRegressor {
fn predict_into(&self, data: &MatrixView<'_>, output: &mut [f32]) -> Result<(), ModelError> {
RandomForestRegressor::predict_into(self, data, output)
}
}
impl HasParams for RandomForestRegressor {
type Params = RandomForestRegressorParams;
fn get_params(&self) -> &Self::Params {
&self.params
}
}
fn mean_tree_prediction(trees: &[PackedTree], row: &[f32]) -> f32 {
let sum: f32 = trees.iter().map(|tree| tree.predict(row)).sum();
sum / trees.len() as f32
}
fn check_row(row: &[f32], expected: usize) -> Result<(), ModelError> {
if row.len() != expected {
return Err(ModelError::FeatureDimension {
expected,
actual: row.len(),
});
}
if let Some(column) = row.iter().position(|value| !value.is_finite()) {
return Err(ModelError::NonFiniteFeature { row: 0, column });
}
Ok(())
}
fn check_prediction_data(
data: &MatrixView<'_>,
output_len: usize,
expected_output_len: usize,
expected_features: usize,
) -> Result<(), ModelError> {
if data.columns() != expected_features {
return Err(ModelError::FeatureDimension {
expected: expected_features,
actual: data.columns(),
});
}
check_output_len(output_len, expected_output_len)
}
fn check_output_len(actual: usize, expected: usize) -> Result<(), ModelError> {
if actual != expected {
return Err(ModelError::OutputLength { expected, actual });
}
Ok(())
}
fn probability_output_len(rows: usize, classes: usize) -> Result<usize, ModelError> {
rows.checked_mul(classes)
.ok_or(ModelError::OutputShapeOverflow {
rows,
columns: classes,
})
}
fn validate_common(
data: &MatrixView<'_>,
target_len: usize,
config: &ForestConfig,
) -> Result<(), ModelError> {
if data.rows() == 0 || data.columns() == 0 {
return Err(ModelError::EmptyData);
}
if target_len == 0 {
return Err(ModelError::EmptyTargets);
}
if target_len != data.rows() {
return Err(ModelError::TargetLength {
rows: data.rows(),
targets: target_len,
});
}
if config.n_estimators == 0 {
return Err(ModelError::InvalidEstimatorCount);
}
if config.max_depth == Some(0) {
return Err(ModelError::InvalidMaxDepth);
}
if config.min_samples_split < 2 {
return Err(ModelError::InvalidMinSamplesSplit);
}
if config.min_samples_leaf == 0 {
return Err(ModelError::InvalidMinSamplesLeaf);
}
if config.n_jobs == 0 {
return Err(ModelError::InvalidJobCount);
}
if data.rows() > u32::MAX as usize {
return Err(ModelError::TooManyRows);
}
if data.columns() > FEATURE_MASK as usize {
return Err(ModelError::TooManyFeatures);
}
if let MaxFeatures::Count(requested) = config.max_features
&& (requested == 0 || requested > data.columns())
{
return Err(ModelError::InvalidMaxFeatures {
requested,
available: data.columns(),
});
}
for row in 0..data.rows() {
for (column, value) in data
.row(row)
.expect("validated row index")
.iter()
.enumerate()
{
if !value.is_finite() {
return Err(ModelError::NonFiniteFeature { row, column });
}
}
}
Ok(())
}
#[cfg(test)]
fn packed_model_bytes(n_features: usize, trees: &[PackedTree], magic: &[u8; 4]) -> Vec<u8> {
let node_count: usize = trees.iter().map(|tree| tree.nodes.len()).sum();
let mut bytes = Vec::with_capacity(24 + trees.len() * 13 + node_count * 16);
bytes.extend_from_slice(magic);
bytes.extend_from_slice(&3u32.to_le_bytes());
bytes.extend_from_slice(&(n_features as u64).to_le_bytes());
bytes.extend_from_slice(&(trees.len() as u64).to_le_bytes());
for tree in trees {
bytes.push(u8::from(tree.root_leaf.is_some()));
bytes.extend_from_slice(&tree.root_leaf.unwrap_or_default().to_bits().to_le_bytes());
bytes.extend_from_slice(&(tree.nodes.len() as u64).to_le_bytes());
for node in &tree.nodes {
bytes.extend_from_slice(&node.feature_and_flags.to_le_bytes());
bytes.extend_from_slice(&node.left.to_le_bytes());
bytes.extend_from_slice(&node.right.to_le_bytes());
bytes.extend_from_slice(&node.threshold.to_bits().to_le_bytes());
}
}
bytes
}