Skip to content

Commit aeddbc8

Browse files
authored
Merge pull request #25 from morenol/lmm/utils
Add capability to convert a slice to a BaseVector
2 parents 82464f4 + 6587ac0 commit aeddbc8

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

src/linalg/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ pub trait BaseVector<T: RealNumber>: Clone + Debug {
8383
self.len() == 0
8484
}
8585

86+
/// Create a new vector from a &[T]
87+
/// ```
88+
/// use smartcore::linalg::naive::dense_matrix::*;
89+
/// let a: [f64; 5] = [0., 0.5, 2., 3., 4.];
90+
/// let v: Vec<f64> = BaseVector::from_array(&a);
91+
/// assert_eq!(v, vec![0., 0.5, 2., 3., 4.]);
92+
/// ```
93+
fn from_array(f: &[T]) -> Self {
94+
let mut v = Self::zeros(f.len());
95+
for (i, elem) in f.iter().enumerate() {
96+
v.set(i, *elem);
97+
}
98+
v
99+
}
100+
86101
/// Return a vector with the elements of the one-dimensional array.
87102
fn to_vec(&self) -> Vec<T>;
88103

src/math/vector.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::math::num::RealNumber;
22
use std::collections::HashMap;
33

4+
use crate::linalg::BaseVector;
45
pub trait RealNumberVector<T: RealNumber> {
5-
fn unique(&self) -> (Vec<T>, Vec<usize>);
6+
fn unique_with_indices(&self) -> (Vec<T>, Vec<usize>);
67
}
78

8-
impl<T: RealNumber> RealNumberVector<T> for Vec<T> {
9-
fn unique(&self) -> (Vec<T>, Vec<usize>) {
10-
let mut unique = self.clone();
9+
impl<T: RealNumber, V: BaseVector<T>> RealNumberVector<T> for V {
10+
fn unique_with_indices(&self) -> (Vec<T>, Vec<usize>) {
11+
let mut unique = self.to_vec();
1112
unique.sort_by(|a, b| a.partial_cmp(b).unwrap());
1213
unique.dedup();
1314

@@ -17,8 +18,8 @@ impl<T: RealNumber> RealNumberVector<T> for Vec<T> {
1718
}
1819

1920
let mut unique_index = Vec::with_capacity(self.len());
20-
for e in self {
21-
unique_index.push(index[&e.to_i64().unwrap()]);
21+
for idx in 0..self.len() {
22+
unique_index.push(index[&self.get(idx).to_i64().unwrap()]);
2223
}
2324

2425
(unique, unique_index)
@@ -30,11 +31,11 @@ mod tests {
3031
use super::*;
3132

3233
#[test]
33-
fn unique() {
34+
fn unique_with_indices() {
3435
let v1 = vec![0.0, 0.0, 1.0, 1.0, 2.0, 0.0, 4.0];
3536
assert_eq!(
3637
(vec!(0.0, 1.0, 2.0, 4.0), vec!(0, 0, 1, 1, 2, 0, 3)),
37-
v1.unique()
38+
v1.unique_with_indices()
3839
);
3940
}
4041
}

src/metrics/cluster_helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub fn contingency_matrix<T: RealNumber>(
77
labels_true: &Vec<T>,
88
labels_pred: &Vec<T>,
99
) -> Vec<Vec<usize>> {
10-
let (classes, class_idx) = labels_true.unique();
11-
let (clusters, cluster_idx) = labels_pred.unique();
10+
let (classes, class_idx) = labels_true.unique_with_indices();
11+
let (clusters, cluster_idx) = labels_pred.unique_with_indices();
1212

1313
let mut contingency_matrix = Vec::with_capacity(classes.len());
1414

src/naive_bayes/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ impl<T: RealNumber, M: Matrix<T>, D: NBDistribution<T, M>> BaseNaiveBayes<T, M,
5858
*prediction
5959
})
6060
.collect::<Vec<T>>();
61-
let mut y_hat = M::RowVector::zeros(rows);
62-
for (i, prediction) in predictions.iter().enumerate().take(rows) {
63-
y_hat.set(i, *prediction);
64-
}
61+
let y_hat = M::RowVector::from_array(&predictions);
6562
Ok(y_hat)
6663
}
6764
}

0 commit comments

Comments
 (0)