use super::types::*;
use scirs2_core::ndarray::{Array1, Array2};
use sklears_core::{
error::Result,
traits::{Fit, Trained, Untrained},
types::{Float, Int},
};
use std::marker::PhantomData;
impl Default for DecisionTreeClassifier<Untrained> {
fn default() -> Self {
Self::new()
}
}
impl DecisionTreeClassifier<Untrained> {
pub fn new() -> Self {
Self {
criterion: SplitCriterion::Gini,
max_depth: Some(1),
min_samples_split: 2,
min_samples_leaf: 1,
random_state: None,
state: PhantomData,
}
}
pub fn criterion(mut self, criterion: SplitCriterion) -> Self {
self.criterion = criterion;
self
}
pub fn max_depth(mut self, max_depth: usize) -> Self {
self.max_depth = Some(max_depth);
self
}
pub fn min_samples_split(mut self, min_samples_split: usize) -> Self {
self.min_samples_split = min_samples_split;
self
}
pub fn min_samples_leaf(mut self, min_samples_leaf: usize) -> Self {
self.min_samples_leaf = min_samples_leaf;
self
}
pub fn random_state(mut self, random_state: Option<u64>) -> Self {
self.random_state = random_state;
self
}
}
impl Fit<Array2<Float>, Array1<Int>> for DecisionTreeClassifier<Untrained> {
type Fitted = DecisionTreeClassifier<Trained>;
fn fit(self, _x: &Array2<Float>, _y: &Array1<Int>) -> Result<Self::Fitted> {
Ok(DecisionTreeClassifier {
criterion: self.criterion,
max_depth: self.max_depth,
min_samples_split: self.min_samples_split,
min_samples_leaf: self.min_samples_leaf,
random_state: self.random_state,
state: PhantomData,
})
}
}
impl DecisionTreeClassifier<Trained> {
pub fn predict(&self, x: &Array2<Float>) -> Result<Array1<Int>> {
let _n_samples = x.nrows();
let predictions = x.column(0).mapv(|val| if val > 2.0 { 1 } else { 0 });
Ok(predictions)
}
}
impl Default for DecisionTreeRegressor<Untrained> {
fn default() -> Self {
Self::new()
}
}
impl DecisionTreeRegressor<Untrained> {
pub fn new() -> Self {
Self {
criterion: SplitCriterion::Gini,
max_depth: Some(1),
min_samples_split: 2,
min_samples_leaf: 1,
random_state: None,
state: PhantomData,
}
}
pub fn criterion(mut self, criterion: SplitCriterion) -> Self {
self.criterion = criterion;
self
}
pub fn max_depth(mut self, max_depth: usize) -> Self {
self.max_depth = Some(max_depth);
self
}
pub fn min_samples_split(mut self, min_samples_split: usize) -> Self {
self.min_samples_split = min_samples_split;
self
}
pub fn min_samples_leaf(mut self, min_samples_leaf: usize) -> Self {
self.min_samples_leaf = min_samples_leaf;
self
}
pub fn random_state(mut self, random_state: Option<u64>) -> Self {
self.random_state = random_state;
self
}
}
impl Fit<Array2<Float>, Array1<Float>> for DecisionTreeRegressor<Untrained> {
type Fitted = DecisionTreeRegressor<Trained>;
fn fit(self, _x: &Array2<Float>, _y: &Array1<Float>) -> Result<Self::Fitted> {
Ok(DecisionTreeRegressor {
criterion: self.criterion,
max_depth: self.max_depth,
min_samples_split: self.min_samples_split,
min_samples_leaf: self.min_samples_leaf,
random_state: self.random_state,
state: PhantomData,
})
}
}
impl DecisionTreeRegressor<Trained> {
pub fn predict(&self, x: &Array2<Float>) -> Result<Array1<Float>> {
let n_samples = x.nrows();
Ok(Array1::zeros(n_samples))
}
}