use scirs2_core::ndarray::{Array1, Array2};
use sklears_core::{
error::{Result, SklearsError},
types::Float,
};
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct CSRMatrix {
pub data: Vec<Float>,
pub indices: Vec<usize>,
pub indptr: Vec<usize>,
pub nrows: usize,
pub ncols: usize,
}
impl CSRMatrix {
pub fn new(nrows: usize, ncols: usize) -> Self {
Self {
data: Vec::new(),
indices: Vec::new(),
indptr: vec![0; nrows + 1],
nrows,
ncols,
}
}
pub fn from_dense(dense: &Array2<Float>) -> Self {
let (nrows, ncols) = dense.dim();
let mut data = Vec::new();
let mut indices = Vec::new();
let mut indptr = vec![0; nrows + 1];
for (row_idx, row) in dense.outer_iter().enumerate() {
indptr[row_idx] = data.len();
for (col_idx, &value) in row.iter().enumerate() {
if value.abs() > Float::EPSILON {
data.push(value);
indices.push(col_idx);
}
}
}
indptr[nrows] = data.len();
Self {
data,
indices,
indptr,
nrows,
ncols,
}
}
pub fn to_dense(&self) -> Array2<Float> {
let mut dense = Array2::<Float>::zeros((self.nrows, self.ncols));
for row_idx in 0..self.nrows {
let start = self.indptr[row_idx];
let end = self.indptr[row_idx + 1];
for data_idx in start..end {
let col_idx = self.indices[data_idx];
let value = self.data[data_idx];
dense[[row_idx, col_idx]] = value;
}
}
dense
}
pub fn get(&self, row: usize, col: usize) -> Float {
if row >= self.nrows || col >= self.ncols {
return 0.0;
}
let start = self.indptr[row];
let end = self.indptr[row + 1];
for data_idx in start..end {
if self.indices[data_idx] == col {
return self.data[data_idx];
}
if self.indices[data_idx] > col {
break;
}
}
0.0
}
pub fn set(&mut self, row: usize, col: usize, value: Float) -> Result<()> {
if row >= self.nrows || col >= self.ncols {
return Err(SklearsError::InvalidInput(
"Row or column index out of bounds".to_string(),
));
}
let start = self.indptr[row];
let end = self.indptr[row + 1];
let mut insert_pos = end;
for data_idx in start..end {
if self.indices[data_idx] == col {
if value.abs() > Float::EPSILON {
self.data[data_idx] = value;
} else {
self.data.remove(data_idx);
self.indices.remove(data_idx);
for i in (row + 1)..=self.nrows {
self.indptr[i] -= 1;
}
}
return Ok(());
}
if self.indices[data_idx] > col {
insert_pos = data_idx;
break;
}
}
if value.abs() > Float::EPSILON {
self.data.insert(insert_pos, value);
self.indices.insert(insert_pos, col);
for i in (row + 1)..=self.nrows {
self.indptr[i] += 1;
}
}
Ok(())
}
pub fn get_row(&self, row: usize) -> SparseVector {
if row >= self.nrows {
return SparseVector::new(self.ncols);
}
let start = self.indptr[row];
let end = self.indptr[row + 1];
let indices = self.indices[start..end].to_vec();
let data = self.data[start..end].to_vec();
SparseVector {
indices,
data,
length: self.ncols,
}
}
pub fn sparsity(&self) -> Float {
let total_elements = self.nrows * self.ncols;
let non_zero_elements = self.data.len();
1.0 - (non_zero_elements as Float / total_elements as Float)
}
pub fn matvec_sparse(&self, vector: &SparseVector) -> Result<Array1<Float>> {
if vector.length != self.ncols {
return Err(SklearsError::InvalidInput(
"Vector length must match matrix columns".to_string(),
));
}
let mut result = Array1::<Float>::zeros(self.nrows);
let mut vector_map: HashMap<usize, Float> = HashMap::new();
for (i, &idx) in vector.indices.iter().enumerate() {
vector_map.insert(idx, vector.data[i]);
}
for row_idx in 0..self.nrows {
let start = self.indptr[row_idx];
let end = self.indptr[row_idx + 1];
let mut dot_product = 0.0;
for data_idx in start..end {
let col_idx = self.indices[data_idx];
let matrix_value = self.data[data_idx];
if let Some(&vector_value) = vector_map.get(&col_idx) {
dot_product += matrix_value * vector_value;
}
}
result[row_idx] = dot_product;
}
Ok(result)
}
pub fn matvec_dense(&self, vector: &Array1<Float>) -> Result<Array1<Float>> {
if vector.len() != self.ncols {
return Err(SklearsError::InvalidInput(
"Vector length must match matrix columns".to_string(),
));
}
let mut result = Array1::<Float>::zeros(self.nrows);
for row_idx in 0..self.nrows {
let start = self.indptr[row_idx];
let end = self.indptr[row_idx + 1];
let mut dot_product = 0.0;
for data_idx in start..end {
let col_idx = self.indices[data_idx];
let matrix_value = self.data[data_idx];
dot_product += matrix_value * vector[col_idx];
}
result[row_idx] = dot_product;
}
Ok(result)
}
pub fn map_nonzero<F>(&mut self, f: F)
where
F: Fn(Float) -> Float,
{
for value in &mut self.data {
*value = f(*value);
}
}
pub fn memory_usage(&self) -> usize {
self.data.len() * std::mem::size_of::<Float>()
+ self.indices.len() * std::mem::size_of::<usize>()
+ self.indptr.len() * std::mem::size_of::<usize>()
}
}
#[derive(Debug, Clone)]
pub struct SparseVector {
pub data: Vec<Float>,
pub indices: Vec<usize>,
pub length: usize,
}
impl SparseVector {
pub fn new(length: usize) -> Self {
Self {
data: Vec::new(),
indices: Vec::new(),
length,
}
}
pub fn from_dense(dense: &Array1<Float>) -> Self {
let mut data = Vec::new();
let mut indices = Vec::new();
for (idx, &value) in dense.iter().enumerate() {
if value.abs() > Float::EPSILON {
data.push(value);
indices.push(idx);
}
}
Self {
data,
indices,
length: dense.len(),
}
}
pub fn to_dense(&self) -> Array1<Float> {
let mut dense = Array1::<Float>::zeros(self.length);
for (i, &idx) in self.indices.iter().enumerate() {
dense[idx] = self.data[i];
}
dense
}
pub fn get(&self, index: usize) -> Float {
if index >= self.length {
return 0.0;
}
for (i, &idx) in self.indices.iter().enumerate() {
if idx == index {
return self.data[i];
}
if idx > index {
break;
}
}
0.0
}
pub fn dot_sparse(&self, other: &SparseVector) -> Float {
if self.length != other.length {
return 0.0;
}
let mut result = 0.0;
let mut i = 0;
let mut j = 0;
while i < self.indices.len() && j < other.indices.len() {
let idx_a = self.indices[i];
let idx_b = other.indices[j];
if idx_a == idx_b {
result += self.data[i] * other.data[j];
i += 1;
j += 1;
} else if idx_a < idx_b {
i += 1;
} else {
j += 1;
}
}
result
}
pub fn dot_dense(&self, dense: &Array1<Float>) -> Float {
if dense.len() != self.length {
return 0.0;
}
let mut result = 0.0;
for (i, &idx) in self.indices.iter().enumerate() {
result += self.data[i] * dense[idx];
}
result
}
pub fn norm(&self) -> Float {
self.data.iter().map(|&x| x * x).sum::<Float>().sqrt()
}
pub fn sparsity(&self) -> Float {
1.0 - (self.data.len() as Float / self.length as Float)
}
}
#[derive(Debug, Clone)]
pub struct SparseTreeData {
pub x_sparse: CSRMatrix,
pub y: Array1<Float>,
pub sample_weight: Option<Array1<Float>>,
pub active_features: Vec<usize>,
}
impl SparseTreeData {
pub fn new(x_sparse: CSRMatrix, y: Array1<Float>) -> Result<Self> {
if x_sparse.nrows != y.len() {
return Err(SklearsError::InvalidInput(
"Number of samples in X and y must match".to_string(),
));
}
let mut active_features = Vec::new();
let mut feature_used = vec![false; x_sparse.ncols];
for &col_idx in &x_sparse.indices {
if !feature_used[col_idx] {
feature_used[col_idx] = true;
active_features.push(col_idx);
}
}
active_features.sort_unstable();
Ok(Self {
x_sparse,
y,
sample_weight: None,
active_features,
})
}
pub fn with_sample_weight(mut self, weights: Array1<Float>) -> Result<Self> {
if weights.len() != self.x_sparse.nrows {
return Err(SklearsError::InvalidInput(
"Sample weights length must match number of samples".to_string(),
));
}
self.sample_weight = Some(weights);
Ok(self)
}
pub fn get_feature_column(&self, feature_idx: usize) -> SparseVector {
let mut data = Vec::new();
let mut indices = Vec::new();
for row_idx in 0..self.x_sparse.nrows {
let value = self.x_sparse.get(row_idx, feature_idx);
if value.abs() > Float::EPSILON {
data.push(value);
indices.push(row_idx);
}
}
SparseVector {
data,
indices,
length: self.x_sparse.nrows,
}
}
pub fn calculate_sparse_feature_importance(&self) -> Array1<Float> {
let mut importance = Array1::<Float>::zeros(self.x_sparse.ncols);
for feature_idx in &self.active_features {
let feature_col = self.get_feature_column(*feature_idx);
let frequency = feature_col.data.len() as Float / self.x_sparse.nrows as Float;
let magnitude = feature_col.data.iter().map(|&x| x.abs()).sum::<Float>();
importance[*feature_idx] = frequency * magnitude;
}
let max_importance = importance.iter().fold(0.0f64, |max, &x| max.max(x));
if max_importance > Float::EPSILON {
importance /= max_importance;
}
importance
}
pub fn subset(&self, sample_indices: &[usize]) -> Result<Self> {
let new_nrows = sample_indices.len();
let mut new_data = Vec::new();
let mut new_indices = Vec::new();
let mut new_indptr = vec![0; new_nrows + 1];
for (new_row_idx, &old_row_idx) in sample_indices.iter().enumerate() {
if old_row_idx >= self.x_sparse.nrows {
return Err(SklearsError::InvalidInput(
"Sample index out of bounds".to_string(),
));
}
new_indptr[new_row_idx] = new_data.len();
let start = self.x_sparse.indptr[old_row_idx];
let end = self.x_sparse.indptr[old_row_idx + 1];
for data_idx in start..end {
new_data.push(self.x_sparse.data[data_idx]);
new_indices.push(self.x_sparse.indices[data_idx]);
}
}
new_indptr[new_nrows] = new_data.len();
let new_x_sparse = CSRMatrix {
data: new_data,
indices: new_indices,
indptr: new_indptr,
nrows: new_nrows,
ncols: self.x_sparse.ncols,
};
let new_y = Array1::from_iter(sample_indices.iter().map(|&i| self.y[i]));
let new_sample_weight = self
.sample_weight
.as_ref()
.map(|weights| Array1::from_iter(sample_indices.iter().map(|&i| weights[i])));
Ok(Self {
x_sparse: new_x_sparse,
y: new_y,
sample_weight: new_sample_weight,
active_features: self.active_features.clone(),
})
}
pub fn memory_usage(&self) -> usize {
self.x_sparse.memory_usage()
+ self.y.len() * std::mem::size_of::<Float>()
+ self
.sample_weight
.as_ref()
.map_or(0, |w| w.len() * std::mem::size_of::<Float>())
+ self.active_features.len() * std::mem::size_of::<usize>()
}
}
pub struct SparseTreeUtils;
impl SparseTreeUtils {
pub fn find_best_sparse_threshold(
feature_values: &SparseVector,
targets: &Array1<Float>,
sample_weight: Option<&Array1<Float>>,
) -> Result<(Float, Float)> {
if feature_values.length != targets.len() {
return Err(SklearsError::InvalidInput(
"Feature values and targets must have same length".to_string(),
));
}
if feature_values.data.is_empty() {
return Ok((0.0, 0.0)); }
let mut value_target_pairs: Vec<(Float, Float, Float)> = Vec::new();
for (i, &idx) in feature_values.indices.iter().enumerate() {
let weight = sample_weight.map_or(1.0, |w| w[idx]);
value_target_pairs.push((feature_values.data[i], targets[idx], weight));
}
value_target_pairs.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));
let mut zero_target_sum = 0.0;
let mut zero_weight_sum = 0.0;
let mut zero_count = 0;
let mut used_indices = vec![false; targets.len()];
for &idx in &feature_values.indices {
used_indices[idx] = true;
}
for (idx, &is_used) in used_indices.iter().enumerate() {
if !is_used {
let weight = sample_weight.map_or(1.0, |w| w[idx]);
zero_target_sum += targets[idx] * weight;
zero_weight_sum += weight;
zero_count += 1;
}
}
let mut best_threshold = 0.0;
let mut best_gain = 0.0;
for i in 0..value_target_pairs.len() - 1 {
let current_value = value_target_pairs[i].0;
let next_value = value_target_pairs[i + 1].0;
if (next_value - current_value).abs() < Float::EPSILON {
continue; }
let threshold = (current_value + next_value) / 2.0;
let mut left_sum = zero_target_sum; let mut left_weight = zero_weight_sum;
let mut right_sum = 0.0;
let mut right_weight = 0.0;
for j in 0..value_target_pairs.len() {
let (value, target, weight) = value_target_pairs[j];
if value <= threshold {
left_sum += target * weight;
left_weight += weight;
} else {
right_sum += target * weight;
right_weight += weight;
}
}
if left_weight > 0.0 && right_weight > 0.0 {
let left_mean = left_sum / left_weight;
let right_mean = right_sum / right_weight;
let total_weight = left_weight + right_weight;
let total_mean = (left_sum + right_sum) / total_weight;
let gain = (left_weight / total_weight) * (left_mean - total_mean).powi(2)
+ (right_weight / total_weight) * (right_mean - total_mean).powi(2);
if gain > best_gain {
best_gain = gain;
best_threshold = threshold;
}
}
}
if !value_target_pairs.is_empty() {
let threshold = 0.0;
let left_sum = zero_target_sum;
let left_weight = zero_weight_sum;
let right_sum: Float = value_target_pairs
.iter()
.map(|(_, target, weight)| target * weight)
.sum();
let right_weight: Float = value_target_pairs
.iter()
.map(|(_, _, weight)| *weight)
.sum();
if left_weight > 0.0 && right_weight > 0.0 {
let left_mean = left_sum / left_weight;
let right_mean = right_sum / right_weight;
let total_weight = left_weight + right_weight;
let total_mean = (left_sum + right_sum) / total_weight;
let gain = (left_weight / total_weight) * (left_mean - total_mean).powi(2)
+ (right_weight / total_weight) * (right_mean - total_mean).powi(2);
if gain > best_gain {
best_gain = gain;
best_threshold = threshold;
}
}
}
Ok((best_threshold, best_gain))
}
pub fn densify_if_beneficial(
sparse_data: &CSRMatrix,
sparsity_threshold: Float,
) -> Option<Array2<Float>> {
if sparse_data.sparsity() < sparsity_threshold {
Some(sparse_data.to_dense())
} else {
None
}
}
pub fn estimate_memory_savings(dense: &Array2<Float>) -> (usize, usize, Float) {
let (nrows, ncols) = dense.dim();
let dense_memory = nrows * ncols * std::mem::size_of::<Float>();
let sparse = CSRMatrix::from_dense(dense);
let sparse_memory = sparse.memory_usage();
let savings_ratio = 1.0 - (sparse_memory as Float / dense_memory as Float);
(dense_memory, sparse_memory, savings_ratio)
}
}
#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
use super::*;
use scirs2_core::ndarray::arr2;
#[test]
fn test_csr_matrix_creation() {
let dense = arr2(&[[1.0, 0.0, 3.0], [0.0, 2.0, 0.0], [4.0, 0.0, 5.0]]);
let sparse = CSRMatrix::from_dense(&dense);
assert_eq!(sparse.data, vec![1.0, 3.0, 2.0, 4.0, 5.0]);
assert_eq!(sparse.indices, vec![0, 2, 1, 0, 2]);
assert_eq!(sparse.indptr, vec![0, 2, 3, 5]);
}
#[test]
fn test_csr_matrix_get_set() {
let dense = arr2(&[[1.0, 0.0, 3.0], [0.0, 2.0, 0.0]]);
let mut sparse = CSRMatrix::from_dense(&dense);
assert_eq!(sparse.get(0, 0), 1.0);
assert_eq!(sparse.get(0, 1), 0.0);
assert_eq!(sparse.get(1, 1), 2.0);
sparse.set(0, 1, 7.0).expect("parsing should succeed");
assert_eq!(sparse.get(0, 1), 7.0);
}
#[test]
fn test_sparse_vector_operations() {
let dense = Array1::from(vec![1.0, 0.0, 3.0, 0.0, 5.0]);
let sparse = SparseVector::from_dense(&dense);
assert_eq!(sparse.data, vec![1.0, 3.0, 5.0]);
assert_eq!(sparse.indices, vec![0, 2, 4]);
assert_eq!(sparse.get(0), 1.0);
assert_eq!(sparse.get(1), 0.0);
assert_eq!(sparse.get(2), 3.0);
let reconstructed = sparse.to_dense();
assert_eq!(reconstructed, dense);
}
#[test]
fn test_sparse_matvec() {
let dense = arr2(&[[1.0, 0.0, 3.0], [0.0, 2.0, 0.0], [4.0, 0.0, 5.0]]);
let sparse_matrix = CSRMatrix::from_dense(&dense);
let vector = Array1::from(vec![1.0, 2.0, 3.0]);
let result_sparse = sparse_matrix.matvec_dense(&vector).expect("parsing should succeed");
let result_dense = dense.dot(&vector);
for (a, b) in result_sparse.iter().zip(result_dense.iter()) {
assert!((a - b).abs() < 1e-10);
}
}
}