Merge pull request #25 from morenol/lmm/utils
Add capability to convert a slice to a BaseVector
This commit is contained in:
@@ -83,6 +83,21 @@ pub trait BaseVector<T: RealNumber>: Clone + Debug {
|
|||||||
self.len() == 0
|
self.len() == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new vector from a &[T]
|
||||||
|
/// ```
|
||||||
|
/// use smartcore::linalg::naive::dense_matrix::*;
|
||||||
|
/// let a: [f64; 5] = [0., 0.5, 2., 3., 4.];
|
||||||
|
/// let v: Vec<f64> = BaseVector::from_array(&a);
|
||||||
|
/// assert_eq!(v, vec![0., 0.5, 2., 3., 4.]);
|
||||||
|
/// ```
|
||||||
|
fn from_array(f: &[T]) -> Self {
|
||||||
|
let mut v = Self::zeros(f.len());
|
||||||
|
for (i, elem) in f.iter().enumerate() {
|
||||||
|
v.set(i, *elem);
|
||||||
|
}
|
||||||
|
v
|
||||||
|
}
|
||||||
|
|
||||||
/// Return a vector with the elements of the one-dimensional array.
|
/// Return a vector with the elements of the one-dimensional array.
|
||||||
fn to_vec(&self) -> Vec<T>;
|
fn to_vec(&self) -> Vec<T>;
|
||||||
|
|
||||||
|
|||||||
+9
-8
@@ -1,13 +1,14 @@
|
|||||||
use crate::math::num::RealNumber;
|
use crate::math::num::RealNumber;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::linalg::BaseVector;
|
||||||
pub trait RealNumberVector<T: RealNumber> {
|
pub trait RealNumberVector<T: RealNumber> {
|
||||||
fn unique(&self) -> (Vec<T>, Vec<usize>);
|
fn unique_with_indices(&self) -> (Vec<T>, Vec<usize>);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: RealNumber> RealNumberVector<T> for Vec<T> {
|
impl<T: RealNumber, V: BaseVector<T>> RealNumberVector<T> for V {
|
||||||
fn unique(&self) -> (Vec<T>, Vec<usize>) {
|
fn unique_with_indices(&self) -> (Vec<T>, Vec<usize>) {
|
||||||
let mut unique = self.clone();
|
let mut unique = self.to_vec();
|
||||||
unique.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
unique.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||||
unique.dedup();
|
unique.dedup();
|
||||||
|
|
||||||
@@ -17,8 +18,8 @@ impl<T: RealNumber> RealNumberVector<T> for Vec<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut unique_index = Vec::with_capacity(self.len());
|
let mut unique_index = Vec::with_capacity(self.len());
|
||||||
for e in self {
|
for idx in 0..self.len() {
|
||||||
unique_index.push(index[&e.to_i64().unwrap()]);
|
unique_index.push(index[&self.get(idx).to_i64().unwrap()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
(unique, unique_index)
|
(unique, unique_index)
|
||||||
@@ -30,11 +31,11 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unique() {
|
fn unique_with_indices() {
|
||||||
let v1 = vec![0.0, 0.0, 1.0, 1.0, 2.0, 0.0, 4.0];
|
let v1 = vec![0.0, 0.0, 1.0, 1.0, 2.0, 0.0, 4.0];
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(vec!(0.0, 1.0, 2.0, 4.0), vec!(0, 0, 1, 1, 2, 0, 3)),
|
(vec!(0.0, 1.0, 2.0, 4.0), vec!(0, 0, 1, 1, 2, 0, 3)),
|
||||||
v1.unique()
|
v1.unique_with_indices()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ pub fn contingency_matrix<T: RealNumber>(
|
|||||||
labels_true: &Vec<T>,
|
labels_true: &Vec<T>,
|
||||||
labels_pred: &Vec<T>,
|
labels_pred: &Vec<T>,
|
||||||
) -> Vec<Vec<usize>> {
|
) -> Vec<Vec<usize>> {
|
||||||
let (classes, class_idx) = labels_true.unique();
|
let (classes, class_idx) = labels_true.unique_with_indices();
|
||||||
let (clusters, cluster_idx) = labels_pred.unique();
|
let (clusters, cluster_idx) = labels_pred.unique_with_indices();
|
||||||
|
|
||||||
let mut contingency_matrix = Vec::with_capacity(classes.len());
|
let mut contingency_matrix = Vec::with_capacity(classes.len());
|
||||||
|
|
||||||
|
|||||||
@@ -58,10 +58,7 @@ impl<T: RealNumber, M: Matrix<T>, D: NBDistribution<T, M>> BaseNaiveBayes<T, M,
|
|||||||
*prediction
|
*prediction
|
||||||
})
|
})
|
||||||
.collect::<Vec<T>>();
|
.collect::<Vec<T>>();
|
||||||
let mut y_hat = M::RowVector::zeros(rows);
|
let y_hat = M::RowVector::from_array(&predictions);
|
||||||
for (i, prediction) in predictions.iter().enumerate().take(rows) {
|
|
||||||
y_hat.set(i, *prediction);
|
|
||||||
}
|
|
||||||
Ok(y_hat)
|
Ok(y_hat)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user