Merge potential next release v0.4 (#187) Breaking Changes
* First draft of the new n-dimensional arrays + NB use case * Improves default implementation of multiple Array methods * Refactors tree methods * Adds matrix decomposition routines * Adds matrix decomposition methods to ndarray and nalgebra bindings * Refactoring + linear regression now uses array2 * Ridge & Linear regression * LBFGS optimizer & logistic regression * LBFGS optimizer & logistic regression * Changes linear methods, metrics and model selection methods to new n-dimensional arrays * Switches KNN and clustering algorithms to new n-d array layer * Refactors distance metrics * Optimizes knn and clustering methods * Refactors metrics module * Switches decomposition methods to n-dimensional arrays * Linalg refactoring - cleanup rng merge (#172) * Remove legacy DenseMatrix and BaseMatrix implementation. Port the new Number, FloatNumber and Array implementation into module structure. * Exclude AUC metrics. Needs reimplementation * Improve developers walkthrough New traits system in place at `src/numbers` and `src/linalg` Co-authored-by: Lorenzo <tunedconsulting@gmail.com> * Provide SupervisedEstimator with a constructor to avoid explicit dynamical box allocation in 'cross_validate' and 'cross_validate_predict' as required by the use of 'dyn' as per Rust 2021 * Implement getters to use as_ref() in src/neighbors * Implement getters to use as_ref() in src/naive_bayes * Implement getters to use as_ref() in src/linear * Add Clone to src/naive_bayes * Change signature for cross_validate and other model_selection functions to abide to use of dyn in Rust 2021 * Implement ndarray-bindings. Remove FloatNumber from implementations * Drop nalgebra-bindings support (as decided in conf-call to go for ndarray) * Remove benches. Benches will have their own repo at smartcore-benches * Implement SVC * Implement SVC serialization. Move search parameters in dedicated module * Implement SVR. Definitely too slow * Fix compilation issues for wasm (#202) Co-authored-by: Luis Moreno <morenol@users.noreply.github.com> * Fix tests (#203) * Port linalg/traits/stats.rs * Improve methods naming * Improve Display for DenseMatrix Co-authored-by: Montana Low <montanalow@users.noreply.github.com> Co-authored-by: VolodymyrOrlov <volodymyr.orlov@gmail.com>
This commit is contained in:
@@ -0,0 +1,286 @@
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::ops::Range;
|
||||
|
||||
use crate::linalg::basic::arrays::{
|
||||
Array as BaseArray, Array2, ArrayView1, ArrayView2, MutArray, MutArrayView2,
|
||||
};
|
||||
|
||||
use crate::linalg::traits::cholesky::CholeskyDecomposable;
|
||||
use crate::linalg::traits::evd::EVDDecomposable;
|
||||
use crate::linalg::traits::lu::LUDecomposable;
|
||||
use crate::linalg::traits::qr::QRDecomposable;
|
||||
use crate::linalg::traits::svd::SVDDecomposable;
|
||||
use crate::numbers::basenum::Number;
|
||||
use crate::numbers::realnum::RealNumber;
|
||||
|
||||
use ndarray::{s, Array, ArrayBase, ArrayView, ArrayViewMut, Ix2, OwnedRepr};
|
||||
|
||||
impl<T: Debug + Display + Copy + Sized> BaseArray<T, (usize, usize)>
|
||||
for ArrayBase<OwnedRepr<T>, Ix2>
|
||||
{
|
||||
fn get(&self, pos: (usize, usize)) -> &T {
|
||||
&self[[pos.0, pos.1]]
|
||||
}
|
||||
|
||||
fn shape(&self) -> (usize, usize) {
|
||||
(self.nrows(), self.ncols())
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.len() > 0
|
||||
}
|
||||
|
||||
fn iterator<'b>(&'b self, axis: u8) -> Box<dyn Iterator<Item = &'b T> + 'b> {
|
||||
assert!(
|
||||
axis == 1 || axis == 0,
|
||||
"For two dimensional array `axis` should be either 0 or 1"
|
||||
);
|
||||
match axis {
|
||||
0 => Box::new(self.iter()),
|
||||
_ => Box::new(
|
||||
(0..self.ncols()).flat_map(move |c| (0..self.nrows()).map(move |r| &self[[r, c]])),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Debug + Display + Copy + Sized> MutArray<T, (usize, usize)>
|
||||
for ArrayBase<OwnedRepr<T>, Ix2>
|
||||
{
|
||||
fn set(&mut self, pos: (usize, usize), x: T) {
|
||||
self[[pos.0, pos.1]] = x
|
||||
}
|
||||
|
||||
fn iterator_mut<'b>(&'b mut self, axis: u8) -> Box<dyn Iterator<Item = &'b mut T> + 'b> {
|
||||
let ptr = self.as_mut_ptr();
|
||||
let stride = self.strides();
|
||||
let (rstride, cstride) = (stride[0] as usize, stride[1] as usize);
|
||||
match axis {
|
||||
0 => Box::new(self.iter_mut()),
|
||||
_ => Box::new((0..self.ncols()).flat_map(move |c| {
|
||||
(0..self.nrows()).map(move |r| unsafe { &mut *ptr.add(r * rstride + c * cstride) })
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Debug + Display + Copy + Sized> ArrayView2<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
|
||||
|
||||
impl<T: Debug + Display + Copy + Sized> MutArrayView2<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
|
||||
|
||||
impl<'a, T: Debug + Display + Copy + Sized> BaseArray<T, (usize, usize)> for ArrayView<'a, T, Ix2> {
|
||||
fn get(&self, pos: (usize, usize)) -> &T {
|
||||
&self[[pos.0, pos.1]]
|
||||
}
|
||||
|
||||
fn shape(&self) -> (usize, usize) {
|
||||
(self.nrows(), self.ncols())
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.len() > 0
|
||||
}
|
||||
|
||||
fn iterator<'b>(&'b self, axis: u8) -> Box<dyn Iterator<Item = &'b T> + 'b> {
|
||||
assert!(
|
||||
axis == 1 || axis == 0,
|
||||
"For two dimensional array `axis` should be either 0 or 1"
|
||||
);
|
||||
match axis {
|
||||
0 => Box::new(self.iter()),
|
||||
_ => Box::new(
|
||||
(0..self.ncols()).flat_map(move |c| (0..self.nrows()).map(move |r| &self[[r, c]])),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Debug + Display + Copy + Sized> Array2<T> for ArrayBase<OwnedRepr<T>, Ix2> {
|
||||
fn get_row<'a>(&'a self, row: usize) -> Box<dyn ArrayView1<T> + 'a> {
|
||||
Box::new(self.row(row))
|
||||
}
|
||||
|
||||
fn get_col<'a>(&'a self, col: usize) -> Box<dyn ArrayView1<T> + 'a> {
|
||||
Box::new(self.column(col))
|
||||
}
|
||||
|
||||
fn slice<'a>(&'a self, rows: Range<usize>, cols: Range<usize>) -> Box<dyn ArrayView2<T> + 'a> {
|
||||
Box::new(self.slice(s![rows, cols]))
|
||||
}
|
||||
|
||||
fn slice_mut<'a>(
|
||||
&'a mut self,
|
||||
rows: Range<usize>,
|
||||
cols: Range<usize>,
|
||||
) -> Box<dyn MutArrayView2<T> + 'a>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
Box::new(self.slice_mut(s![rows, cols]))
|
||||
}
|
||||
|
||||
fn fill(nrows: usize, ncols: usize, value: T) -> Self {
|
||||
Array::from_elem([nrows, ncols], value)
|
||||
}
|
||||
|
||||
fn from_iterator<I: Iterator<Item = T>>(iter: I, nrows: usize, ncols: usize, axis: u8) -> Self {
|
||||
let a = Array::from_iter(iter.take(nrows * ncols))
|
||||
.into_shape((nrows, ncols))
|
||||
.unwrap();
|
||||
match axis {
|
||||
0 => a,
|
||||
_ => a.reversed_axes().into_shape((nrows, ncols)).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
fn transpose(&self) -> Self {
|
||||
self.t().to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Number + RealNumber> QRDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
|
||||
impl<T: Number + RealNumber> CholeskyDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
|
||||
impl<T: Number + RealNumber> EVDDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
|
||||
impl<T: Number + RealNumber> LUDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
|
||||
impl<T: Number + RealNumber> SVDDecomposable<T> for ArrayBase<OwnedRepr<T>, Ix2> {}
|
||||
|
||||
impl<'a, T: Debug + Display + Copy + Sized> ArrayView2<T> for ArrayView<'a, T, Ix2> {}
|
||||
|
||||
impl<'a, T: Debug + Display + Copy + Sized> BaseArray<T, (usize, usize)>
|
||||
for ArrayViewMut<'a, T, Ix2>
|
||||
{
|
||||
fn get(&self, pos: (usize, usize)) -> &T {
|
||||
&self[[pos.0, pos.1]]
|
||||
}
|
||||
|
||||
fn shape(&self) -> (usize, usize) {
|
||||
(self.nrows(), self.ncols())
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.len() > 0
|
||||
}
|
||||
|
||||
fn iterator<'b>(&'b self, axis: u8) -> Box<dyn Iterator<Item = &'b T> + 'b> {
|
||||
assert!(
|
||||
axis == 1 || axis == 0,
|
||||
"For two dimensional array `axis` should be either 0 or 1"
|
||||
);
|
||||
match axis {
|
||||
0 => Box::new(self.iter()),
|
||||
_ => Box::new(
|
||||
(0..self.ncols()).flat_map(move |c| (0..self.nrows()).map(move |r| &self[[r, c]])),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Debug + Display + Copy + Sized> MutArray<T, (usize, usize)>
|
||||
for ArrayViewMut<'a, T, Ix2>
|
||||
{
|
||||
fn set(&mut self, pos: (usize, usize), x: T) {
|
||||
self[[pos.0, pos.1]] = x
|
||||
}
|
||||
|
||||
fn iterator_mut<'b>(&'b mut self, axis: u8) -> Box<dyn Iterator<Item = &'b mut T> + 'b> {
|
||||
let ptr = self.as_mut_ptr();
|
||||
let stride = self.strides();
|
||||
let (rstride, cstride) = (stride[0] as usize, stride[1] as usize);
|
||||
match axis {
|
||||
0 => Box::new(self.iter_mut()),
|
||||
_ => Box::new((0..self.ncols()).flat_map(move |c| {
|
||||
(0..self.nrows()).map(move |r| unsafe { &mut *ptr.add(r * rstride + c * cstride) })
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: Debug + Display + Copy + Sized> MutArrayView2<T> for ArrayViewMut<'a, T, Ix2> {}
|
||||
|
||||
impl<'a, T: Debug + Display + Copy + Sized> ArrayView2<T> for ArrayViewMut<'a, T, Ix2> {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ndarray::{arr2, Array2 as NDArray2};
|
||||
|
||||
#[test]
|
||||
fn test_get_set() {
|
||||
let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||
|
||||
assert_eq!(*BaseArray::get(&a, (1, 1)), 5);
|
||||
a.set((1, 1), 9);
|
||||
assert_eq!(a, arr2(&[[1, 2, 3], [4, 9, 6]]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator() {
|
||||
let a = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||
|
||||
let v: Vec<i32> = a.iterator(0).map(|&v| v).collect();
|
||||
assert_eq!(v, vec!(1, 2, 3, 4, 5, 6));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mut_iterator() {
|
||||
let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||
|
||||
a.iterator_mut(0).enumerate().for_each(|(i, v)| *v = i);
|
||||
assert_eq!(a, arr2(&[[0, 1, 2], [3, 4, 5]]));
|
||||
a.iterator_mut(1).enumerate().for_each(|(i, v)| *v = i);
|
||||
assert_eq!(a, arr2(&[[0, 2, 4], [1, 3, 5]]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice() {
|
||||
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||
let x_slice = Array2::slice(&x, 0..2, 1..2);
|
||||
assert_eq!((2, 1), x_slice.shape());
|
||||
let v: Vec<i32> = x_slice.iterator(0).map(|&v| v).collect();
|
||||
assert_eq!(v, [2, 5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice_iter() {
|
||||
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||
let x_slice = Array2::slice(&x, 0..2, 0..3);
|
||||
assert_eq!(
|
||||
x_slice.iterator(0).map(|&v| v).collect::<Vec<i32>>(),
|
||||
vec![1, 2, 3, 4, 5, 6]
|
||||
);
|
||||
assert_eq!(
|
||||
x_slice.iterator(1).map(|&v| v).collect::<Vec<i32>>(),
|
||||
vec![1, 4, 2, 5, 3, 6]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_slice_mut_iter() {
|
||||
let mut x = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||
{
|
||||
let mut x_slice = Array2::slice_mut(&mut x, 0..2, 0..3);
|
||||
x_slice
|
||||
.iterator_mut(0)
|
||||
.enumerate()
|
||||
.for_each(|(i, v)| *v = i);
|
||||
}
|
||||
assert_eq!(x, arr2(&[[0, 1, 2], [3, 4, 5]]));
|
||||
{
|
||||
let mut x_slice = Array2::slice_mut(&mut x, 0..2, 0..3);
|
||||
x_slice
|
||||
.iterator_mut(1)
|
||||
.enumerate()
|
||||
.for_each(|(i, v)| *v = i);
|
||||
}
|
||||
assert_eq!(x, arr2(&[[0, 2, 4], [1, 3, 5]]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_c_from_iterator() {
|
||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
||||
let a: NDArray2<i32> = Array2::from_iterator(data.clone().into_iter(), 4, 3, 0);
|
||||
println!("{}", a);
|
||||
let a: NDArray2<i32> = Array2::from_iterator(data.into_iter(), 4, 3, 1);
|
||||
println!("{}", a);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user