use super::config::StackingConfig;
use crate::simd_stacking;
use scirs2_core::ndarray::{s, Array1, Array2};
use scirs2_core::RngExt;
use sklears_core::{
error::{Result, SklearsError},
prelude::Predict,
traits::{Fit, Trained, Untrained},
types::Float,
};
use std::marker::PhantomData;
#[derive(Debug)]
pub struct SimpleStackingClassifier<State = Untrained> {
pub(crate) config: StackingConfig,
pub(crate) state: PhantomData<State>,
pub(crate) base_weights_: Option<Array2<Float>>, pub(crate) base_intercepts_: Option<Array1<Float>>, pub(crate) meta_weights_: Option<Array1<Float>>, pub(crate) meta_intercept_: Option<Float>, pub(crate) n_base_estimators_: Option<usize>,
pub(crate) classes_: Option<Array1<i32>>,
pub(crate) n_features_in_: Option<usize>,
}
impl SimpleStackingClassifier<Untrained> {
pub fn new(n_base_estimators: usize) -> Self {
Self {
config: StackingConfig::default(),
state: PhantomData,
base_weights_: None,
base_intercepts_: None,
meta_weights_: None,
meta_intercept_: None,
n_base_estimators_: Some(n_base_estimators),
classes_: None,
n_features_in_: None,
}
}
pub fn cv(mut self, cv: usize) -> Self {
self.config.cv = cv;
self
}
pub fn use_probabilities(mut self, use_probabilities: bool) -> Self {
self.config.use_probabilities = use_probabilities;
self
}
pub fn random_state(mut self, random_state: u64) -> Self {
self.config.random_state = Some(random_state);
self
}
pub fn passthrough(mut self, passthrough: bool) -> Self {
self.config.passthrough = passthrough;
self
}
}
impl Fit<Array2<Float>, Array1<i32>> for SimpleStackingClassifier<Untrained> {
type Fitted = SimpleStackingClassifier<Trained>;
fn fit(self, x: &Array2<Float>, y: &Array1<i32>) -> Result<Self::Fitted> {
if x.nrows() != y.len() {
return Err(SklearsError::ShapeMismatch {
expected: format!("{} samples", x.nrows()),
actual: format!("{} samples", y.len()),
});
}
let (n_samples, n_features) = x.dim();
let _n_base_estimators = self.n_base_estimators_.expect("operation should succeed");
if n_samples < 10 {
return Err(SklearsError::InvalidInput(
"Stacking requires at least 10 samples".to_string(),
));
}
let mut classes: Vec<i32> = y.to_vec();
classes.sort_unstable();
classes.dedup();
let classes_array = Array1::from_vec(classes.clone());
let n_classes = classes.len();
if n_classes < 2 {
return Err(SklearsError::InvalidInput(
"Need at least 2 classes for classification".to_string(),
));
}
let y_float: Array1<Float> = y.mapv(|v| v as Float);
let (base_weights, base_intercepts) = self.train_base_estimators(x, &y_float)?;
let meta_features =
self.generate_meta_features(x, &y_float, &base_weights, &base_intercepts)?;
let (meta_weights, meta_intercept) = self.train_meta_learner(&meta_features, &y_float)?;
Ok(SimpleStackingClassifier {
config: self.config,
state: PhantomData,
base_weights_: Some(base_weights),
base_intercepts_: Some(base_intercepts),
meta_weights_: Some(meta_weights),
meta_intercept_: Some(meta_intercept),
n_base_estimators_: self.n_base_estimators_,
classes_: Some(classes_array),
n_features_in_: Some(n_features),
})
}
}
impl SimpleStackingClassifier<Untrained> {
fn train_base_estimators(
&self,
x: &Array2<Float>,
y: &Array1<Float>,
) -> Result<(Array2<Float>, Array1<Float>)> {
let (_n_samples, n_features) = x.dim();
let n_base_estimators = self.n_base_estimators_.expect("operation should succeed");
let mut base_weights = Array2::<Float>::zeros((n_base_estimators, n_features));
let mut base_intercepts = Array1::<Float>::zeros(n_base_estimators);
for i in 0..n_base_estimators {
let seed = self.config.random_state.unwrap_or(42) + i as u64;
let mut rng = scirs2_core::random::Random::seed(seed);
for j in 0..n_features {
base_weights[[i, j]] = (rng.random::<f64>() - 0.5) * 2.0;
}
base_intercepts[i] = y.mean().unwrap_or(0.0);
}
Ok((base_weights, base_intercepts))
}
fn generate_meta_features(
&self,
x: &Array2<Float>,
_y: &Array1<Float>,
base_weights: &Array2<Float>,
base_intercepts: &Array1<Float>,
) -> Result<Array2<Float>> {
let (n_samples, _) = x.dim();
let n_base_estimators = base_weights.nrows();
let holdout_size = n_samples / self.config.cv;
let train_size = n_samples - holdout_size;
if train_size < 5 {
return Err(SklearsError::InvalidInput(
"Insufficient samples for cross-validation".to_string(),
));
}
let mut meta_features = Array2::<Float>::zeros((n_samples, n_base_estimators));
for i in 0..n_base_estimators {
let weights = base_weights.row(i);
let intercept = base_intercepts[i];
for j in 0..n_samples {
let x_sample = x.row(j);
let prediction = self.predict_linear(&weights, intercept, &x_sample);
meta_features[[j, i]] = prediction;
}
}
Ok(meta_features)
}
fn train_meta_learner(
&self,
meta_features: &Array2<Float>,
y: &Array1<Float>,
) -> Result<(Array1<Float>, Float)> {
let (n_samples, n_meta_features) = meta_features.dim();
let mut x_with_intercept = Array2::<Float>::ones((n_samples, n_meta_features + 1));
x_with_intercept
.slice_mut(s![.., ..n_meta_features])
.assign(meta_features);
let mut xtx = Array2::<Float>::zeros((n_meta_features + 1, n_meta_features + 1));
for i in 0..(n_meta_features + 1) {
for j in 0..(n_meta_features + 1) {
for k in 0..n_samples {
xtx[[i, j]] += x_with_intercept[[k, i]] * x_with_intercept[[k, j]];
}
}
xtx[[i, i]] += 0.001;
}
let mut xty = Array1::<Float>::zeros(n_meta_features + 1);
for i in 0..(n_meta_features + 1) {
for j in 0..n_samples {
xty[i] += x_with_intercept[[j, i]] * y[j];
}
}
let params = self.solve_linear_system(&xtx, &xty)?;
let intercept = params[n_meta_features];
let weights = params.slice(s![..n_meta_features]).to_owned();
Ok((weights, intercept))
}
fn solve_linear_system(&self, a: &Array2<Float>, b: &Array1<Float>) -> Result<Array1<Float>> {
let n = a.nrows();
if n != a.ncols() || n != b.len() {
return Err(SklearsError::InvalidInput(
"Matrix dimensions don't match".to_string(),
));
}
let mut aug = Array2::<Float>::zeros((n, n + 1));
for i in 0..n {
for j in 0..n {
aug[[i, j]] = a[[i, j]];
}
aug[[i, n]] = b[i];
}
for i in 0..n {
let mut max_row = i;
for k in (i + 1)..n {
if aug[[k, i]].abs() > aug[[max_row, i]].abs() {
max_row = k;
}
}
if max_row != i {
for j in 0..(n + 1) {
let temp = aug[[i, j]];
aug[[i, j]] = aug[[max_row, j]];
aug[[max_row, j]] = temp;
}
}
if aug[[i, i]].abs() < 1e-10 {
return Err(SklearsError::NumericalError(
"Singular matrix in linear system".to_string(),
));
}
for k in (i + 1)..n {
let factor = aug[[k, i]] / aug[[i, i]];
for j in i..(n + 1) {
aug[[k, j]] -= factor * aug[[i, j]];
}
}
}
let mut x = Array1::<Float>::zeros(n);
for i in (0..n).rev() {
x[i] = aug[[i, n]];
for j in (i + 1)..n {
x[i] -= aug[[i, j]] * x[j];
}
x[i] /= aug[[i, i]];
}
Ok(x)
}
fn predict_linear(
&self,
weights: &scirs2_core::ndarray::ArrayView1<Float>,
intercept: Float,
x: &scirs2_core::ndarray::ArrayView1<Float>,
) -> Float {
simd_stacking::simd_linear_prediction(x, weights, intercept)
}
}
impl SimpleStackingClassifier<Trained> {
fn predict_linear(
&self,
weights: &scirs2_core::ndarray::ArrayView1<Float>,
intercept: Float,
x: &scirs2_core::ndarray::ArrayView1<Float>,
) -> Float {
simd_stacking::simd_linear_prediction(x, weights, intercept)
}
}
impl Predict<Array2<Float>, Array1<i32>> for SimpleStackingClassifier<Trained> {
fn predict(&self, x: &Array2<Float>) -> Result<Array1<i32>> {
if x.ncols() != self.n_features_in_.expect("operation should succeed") {
return Err(SklearsError::FeatureMismatch {
expected: self.n_features_in_.expect("operation should succeed"),
actual: x.ncols(),
});
}
let n_samples = x.nrows();
let n_base_estimators = self.n_base_estimators_.expect("operation should succeed");
let base_weights = self
.base_weights_
.as_ref()
.expect("operation should succeed");
let base_intercepts = self
.base_intercepts_
.as_ref()
.expect("operation should succeed");
let meta_weights = self
.meta_weights_
.as_ref()
.expect("operation should succeed");
let meta_intercept = self.meta_intercept_.expect("operation should succeed");
let classes = self.classes_.as_ref().expect("operation should succeed");
let meta_features = simd_stacking::simd_generate_meta_features(
&x.view(),
&base_weights.view(),
&base_intercepts.view(),
)
.unwrap_or_else(|_| {
let mut meta_features = Array2::<Float>::zeros((n_samples, n_base_estimators));
for i in 0..n_base_estimators {
let weights = base_weights.row(i);
let intercept = base_intercepts[i];
for j in 0..n_samples {
let x_sample = x.row(j);
let prediction = self.predict_linear(&weights, intercept, &x_sample);
meta_features[[j, i]] = prediction;
}
}
meta_features
});
let raw_predictions = simd_stacking::simd_aggregate_predictions(
&meta_features.view(),
&meta_weights.view(),
meta_intercept,
)
.unwrap_or_else(|_| {
let mut predictions = Array1::<Float>::zeros(n_samples);
for i in 0..n_samples {
let meta_sample = meta_features.row(i);
predictions[i] = meta_weights.dot(&meta_sample) + meta_intercept;
}
predictions
});
let mut predictions = Array1::<i32>::zeros(n_samples);
for i in 0..n_samples {
let raw_prediction = raw_predictions[i];
let class_pred = if raw_prediction >= 0.5 {
classes[classes.len() - 1] } else {
classes[0] };
predictions[i] = class_pred;
}
Ok(predictions)
}
}
impl SimpleStackingClassifier<Trained> {
pub fn classes(&self) -> &Array1<i32> {
self.classes_.as_ref().expect("operation should succeed")
}
pub fn n_features_in(&self) -> usize {
self.n_features_in_.expect("operation should succeed")
}
pub fn n_base_estimators(&self) -> usize {
self.n_base_estimators_.expect("operation should succeed")
}
pub fn base_weights(&self) -> &Array2<Float> {
self.base_weights_
.as_ref()
.expect("operation should succeed")
}
pub fn base_intercepts(&self) -> &Array1<Float> {
self.base_intercepts_
.as_ref()
.expect("operation should succeed")
}
pub fn meta_weights(&self) -> &Array1<Float> {
self.meta_weights_
.as_ref()
.expect("operation should succeed")
}
pub fn meta_intercept(&self) -> Float {
self.meta_intercept_.expect("operation should succeed")
}
}
pub use SimpleStackingClassifier as StackingClassifier;
#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
use super::*;
use scirs2_core::ndarray::array;
#[test]
fn test_stacking_creation() {
let stacking = StackingClassifier::new(3)
.cv(5)
.random_state(42)
.passthrough(true);
assert_eq!(stacking.config.cv, 5);
assert_eq!(stacking.config.random_state, Some(42));
assert!(stacking.config.passthrough);
assert_eq!(
stacking
.n_base_estimators_
.expect("operation should succeed"),
3
);
}
#[test]
fn test_stacking_fit_predict() {
let x = array![
[1.0, 2.0],
[3.0, 4.0],
[5.0, 6.0],
[7.0, 8.0],
[9.0, 10.0],
[11.0, 12.0],
[13.0, 14.0],
[15.0, 16.0],
[17.0, 18.0],
[19.0, 20.0],
[21.0, 22.0],
[23.0, 24.0]
];
let y = array![0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1];
let stacking = StackingClassifier::new(2);
let fitted_model = stacking.fit(&x, &y).expect("model fitting should succeed");
assert_eq!(fitted_model.n_features_in(), 2);
assert_eq!(fitted_model.classes().len(), 2);
let predictions = fitted_model.predict(&x).expect("prediction should succeed");
assert_eq!(predictions.len(), 12);
}
#[test]
fn test_shape_mismatch() {
let x = array![[1.0, 2.0], [3.0, 4.0]];
let y = array![0];
let stacking = StackingClassifier::new(1);
let result = stacking.fit(&x, &y);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Shape mismatch"));
}
#[test]
fn test_feature_mismatch() {
let x_train = array![
[1.0, 2.0],
[3.0, 4.0],
[5.0, 6.0],
[7.0, 8.0],
[9.0, 10.0],
[11.0, 12.0],
[13.0, 14.0],
[15.0, 16.0],
[17.0, 18.0],
[19.0, 20.0],
[21.0, 22.0],
[23.0, 24.0]
];
let y_train = array![0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
let x_test = array![[1.0, 2.0, 3.0]];
let stacking = StackingClassifier::new(1);
let fitted_model = stacking
.fit(&x_train, &y_train)
.expect("model fitting should succeed");
let result = fitted_model.predict(&x_test);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Feature"));
}
}