Merge branch 'development' into prdct-prb
This commit is contained in:
@@ -260,8 +260,8 @@ mod tests_fastpair {
|
|||||||
let distances = fastpair.distances;
|
let distances = fastpair.distances;
|
||||||
let neighbours = fastpair.neighbours;
|
let neighbours = fastpair.neighbours;
|
||||||
|
|
||||||
assert!(distances.len() != 0);
|
assert!(!distances.is_empty());
|
||||||
assert!(neighbours.len() != 0);
|
assert!(!neighbours.is_empty());
|
||||||
|
|
||||||
assert_eq!(10, neighbours.len());
|
assert_eq!(10, neighbours.len());
|
||||||
assert_eq!(10, distances.len());
|
assert_eq!(10, distances.len());
|
||||||
@@ -276,17 +276,13 @@ mod tests_fastpair {
|
|||||||
// We expect an error when we run `FastPair` on this dataset,
|
// We expect an error when we run `FastPair` on this dataset,
|
||||||
// becuase `FastPair` currently only works on a minimum of 3
|
// becuase `FastPair` currently only works on a minimum of 3
|
||||||
// points.
|
// points.
|
||||||
let _fastpair = FastPair::new(&dataset);
|
let fastpair = FastPair::new(&dataset);
|
||||||
|
assert!(fastpair.is_err());
|
||||||
|
|
||||||
match _fastpair {
|
if let Err(e) = fastpair {
|
||||||
Err(e) => {
|
let expected_error =
|
||||||
let expected_error =
|
Failed::because(FailedError::FindFailed, "min number of rows should be 3");
|
||||||
Failed::because(FailedError::FindFailed, "min number of rows should be 3");
|
assert_eq!(e, expected_error)
|
||||||
assert_eq!(e, expected_error)
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
assert!(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -582,7 +578,7 @@ mod tests_fastpair {
|
|||||||
};
|
};
|
||||||
for p in dissimilarities.iter() {
|
for p in dissimilarities.iter() {
|
||||||
if p.distance.unwrap() < min_dissimilarity.distance.unwrap() {
|
if p.distance.unwrap() < min_dissimilarity.distance.unwrap() {
|
||||||
min_dissimilarity = p.clone()
|
min_dissimilarity = *p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -511,6 +511,6 @@ mod tests {
|
|||||||
.and_then(|dbscan| dbscan.predict(&x))
|
.and_then(|dbscan| dbscan.predict(&x))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
println!("{:?}", labels);
|
println!("{labels:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -498,8 +498,8 @@ mod tests {
|
|||||||
|
|
||||||
let y: Vec<usize> = kmeans.predict(&x).unwrap();
|
let y: Vec<usize> = kmeans.predict(&x).unwrap();
|
||||||
|
|
||||||
for i in 0..y.len() {
|
for (i, _y_i) in y.iter().enumerate() {
|
||||||
assert_eq!(y[i] as usize, kmeans._y[i]);
|
assert_eq!({ y[i] }, kmeans._y[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ use crate::dataset::Dataset;
|
|||||||
pub fn load_dataset() -> Dataset<f32, f32> {
|
pub fn load_dataset() -> Dataset<f32, f32> {
|
||||||
let (x, y, num_samples, num_features) = match deserialize_data(std::include_bytes!("boston.xy"))
|
let (x, y, num_samples, num_features) = match deserialize_data(std::include_bytes!("boston.xy"))
|
||||||
{
|
{
|
||||||
Err(why) => panic!("Can't deserialize boston.xy. {}", why),
|
Err(why) => panic!("Can't deserialize boston.xy. {why}"),
|
||||||
Ok((x, y, num_samples, num_features)) => (x, y, num_samples, num_features),
|
Ok((x, y, num_samples, num_features)) => (x, y, num_samples, num_features),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ use crate::dataset::Dataset;
|
|||||||
pub fn load_dataset() -> Dataset<f32, u32> {
|
pub fn load_dataset() -> Dataset<f32, u32> {
|
||||||
let (x, y, num_samples, num_features) =
|
let (x, y, num_samples, num_features) =
|
||||||
match deserialize_data(std::include_bytes!("breast_cancer.xy")) {
|
match deserialize_data(std::include_bytes!("breast_cancer.xy")) {
|
||||||
Err(why) => panic!("Can't deserialize breast_cancer.xy. {}", why),
|
Err(why) => panic!("Can't deserialize breast_cancer.xy. {why}"),
|
||||||
Ok((x, y, num_samples, num_features)) => (
|
Ok((x, y, num_samples, num_features)) => (
|
||||||
x,
|
x,
|
||||||
y.into_iter().map(|x| x as u32).collect(),
|
y.into_iter().map(|x| x as u32).collect(),
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ use crate::dataset::Dataset;
|
|||||||
pub fn load_dataset() -> Dataset<f32, u32> {
|
pub fn load_dataset() -> Dataset<f32, u32> {
|
||||||
let (x, y, num_samples, num_features) =
|
let (x, y, num_samples, num_features) =
|
||||||
match deserialize_data(std::include_bytes!("diabetes.xy")) {
|
match deserialize_data(std::include_bytes!("diabetes.xy")) {
|
||||||
Err(why) => panic!("Can't deserialize diabetes.xy. {}", why),
|
Err(why) => panic!("Can't deserialize diabetes.xy. {why}"),
|
||||||
Ok((x, y, num_samples, num_features)) => (
|
Ok((x, y, num_samples, num_features)) => (
|
||||||
x,
|
x,
|
||||||
y.into_iter().map(|x| x as u32).collect(),
|
y.into_iter().map(|x| x as u32).collect(),
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ use crate::dataset::Dataset;
|
|||||||
pub fn load_dataset() -> Dataset<f32, f32> {
|
pub fn load_dataset() -> Dataset<f32, f32> {
|
||||||
let (x, y, num_samples, num_features) = match deserialize_data(std::include_bytes!("digits.xy"))
|
let (x, y, num_samples, num_features) = match deserialize_data(std::include_bytes!("digits.xy"))
|
||||||
{
|
{
|
||||||
Err(why) => panic!("Can't deserialize digits.xy. {}", why),
|
Err(why) => panic!("Can't deserialize digits.xy. {why}"),
|
||||||
Ok((x, y, num_samples, num_features)) => (x, y, num_samples, num_features),
|
Ok((x, y, num_samples, num_features)) => (x, y, num_samples, num_features),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@ use crate::dataset::Dataset;
|
|||||||
pub fn load_dataset() -> Dataset<f32, u32> {
|
pub fn load_dataset() -> Dataset<f32, u32> {
|
||||||
let (x, y, num_samples, num_features): (Vec<f32>, Vec<u32>, usize, usize) =
|
let (x, y, num_samples, num_features): (Vec<f32>, Vec<u32>, usize, usize) =
|
||||||
match deserialize_data(std::include_bytes!("iris.xy")) {
|
match deserialize_data(std::include_bytes!("iris.xy")) {
|
||||||
Err(why) => panic!("Can't deserialize iris.xy. {}", why),
|
Err(why) => panic!("Can't deserialize iris.xy. {why}"),
|
||||||
Ok((x, y, num_samples, num_features)) => (
|
Ok((x, y, num_samples, num_features)) => (
|
||||||
x,
|
x,
|
||||||
y.into_iter().map(|x| x as u32).collect(),
|
y.into_iter().map(|x| x as u32).collect(),
|
||||||
|
|||||||
+1
-1
@@ -78,7 +78,7 @@ pub(crate) fn serialize_data<X: Number + RealNumber, Y: RealNumber>(
|
|||||||
.collect();
|
.collect();
|
||||||
file.write_all(&y)?;
|
file.write_all(&y)?;
|
||||||
}
|
}
|
||||||
Err(why) => panic!("couldn't create {}: {}", filename, why),
|
Err(why) => panic!("couldn't create {filename}: {why}"),
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -231,8 +231,7 @@ impl<T: Number + RealNumber, X: Array2<T> + SVDDecomposable<T> + EVDDecomposable
|
|||||||
|
|
||||||
if parameters.n_components > n {
|
if parameters.n_components > n {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Number of components, n_components should be <= number of attributes ({})",
|
"Number of components, n_components should be <= number of attributes ({n})"
|
||||||
n
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,21 +373,20 @@ mod tests {
|
|||||||
let parameters = PCASearchParameters {
|
let parameters = PCASearchParameters {
|
||||||
n_components: vec![2, 4],
|
n_components: vec![2, 4],
|
||||||
use_correlation_matrix: vec![true, false],
|
use_correlation_matrix: vec![true, false],
|
||||||
..Default::default()
|
|
||||||
};
|
};
|
||||||
let mut iter = parameters.into_iter();
|
let mut iter = parameters.into_iter();
|
||||||
let next = iter.next().unwrap();
|
let next = iter.next().unwrap();
|
||||||
assert_eq!(next.n_components, 2);
|
assert_eq!(next.n_components, 2);
|
||||||
assert_eq!(next.use_correlation_matrix, true);
|
assert!(next.use_correlation_matrix);
|
||||||
let next = iter.next().unwrap();
|
let next = iter.next().unwrap();
|
||||||
assert_eq!(next.n_components, 4);
|
assert_eq!(next.n_components, 4);
|
||||||
assert_eq!(next.use_correlation_matrix, true);
|
assert!(next.use_correlation_matrix);
|
||||||
let next = iter.next().unwrap();
|
let next = iter.next().unwrap();
|
||||||
assert_eq!(next.n_components, 2);
|
assert_eq!(next.n_components, 2);
|
||||||
assert_eq!(next.use_correlation_matrix, false);
|
assert!(!next.use_correlation_matrix);
|
||||||
let next = iter.next().unwrap();
|
let next = iter.next().unwrap();
|
||||||
assert_eq!(next.n_components, 4);
|
assert_eq!(next.n_components, 4);
|
||||||
assert_eq!(next.use_correlation_matrix, false);
|
assert!(!next.use_correlation_matrix);
|
||||||
assert!(iter.next().is_none());
|
assert!(iter.next().is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -572,8 +570,8 @@ mod tests {
|
|||||||
epsilon = 1e-4
|
epsilon = 1e-4
|
||||||
));
|
));
|
||||||
|
|
||||||
for i in 0..pca.eigenvalues.len() {
|
for (i, pca_eigenvalues_i) in pca.eigenvalues.iter().enumerate() {
|
||||||
assert!((pca.eigenvalues[i].abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
|
assert!((pca_eigenvalues_i.abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
|
||||||
}
|
}
|
||||||
|
|
||||||
let us_arrests_t = pca.transform(&us_arrests).unwrap();
|
let us_arrests_t = pca.transform(&us_arrests).unwrap();
|
||||||
@@ -694,8 +692,8 @@ mod tests {
|
|||||||
epsilon = 1e-4
|
epsilon = 1e-4
|
||||||
));
|
));
|
||||||
|
|
||||||
for i in 0..pca.eigenvalues.len() {
|
for (i, pca_eigenvalues_i) in pca.eigenvalues.iter().enumerate() {
|
||||||
assert!((pca.eigenvalues[i].abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
|
assert!((pca_eigenvalues_i.abs() - expected_eigenvalues[i].abs()).abs() < 1e-8);
|
||||||
}
|
}
|
||||||
|
|
||||||
let us_arrests_t = pca.transform(&us_arrests).unwrap();
|
let us_arrests_t = pca.transform(&us_arrests).unwrap();
|
||||||
|
|||||||
@@ -180,8 +180,7 @@ impl<T: Number + RealNumber, X: Array2<T> + SVDDecomposable<T> + EVDDecomposable
|
|||||||
|
|
||||||
if parameters.n_components >= p {
|
if parameters.n_components >= p {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Number of components, n_components should be < number of attributes ({})",
|
"Number of components, n_components should be < number of attributes ({p})"
|
||||||
p
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,8 +201,7 @@ impl<T: Number + RealNumber, X: Array2<T> + SVDDecomposable<T> + EVDDecomposable
|
|||||||
let (p_c, k) = self.components.shape();
|
let (p_c, k) = self.components.shape();
|
||||||
if p_c != p {
|
if p_c != p {
|
||||||
return Err(Failed::transform(&format!(
|
return Err(Failed::transform(&format!(
|
||||||
"Can not transform a {}x{} matrix into {}x{} matrix, incorrect input dimentions",
|
"Can not transform a {n}x{p} matrix into {n}x{k} matrix, incorrect input dimentions"
|
||||||
n, p, n, k
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,7 +225,6 @@ mod tests {
|
|||||||
fn search_parameters() {
|
fn search_parameters() {
|
||||||
let parameters = SVDSearchParameters {
|
let parameters = SVDSearchParameters {
|
||||||
n_components: vec![10, 100],
|
n_components: vec![10, 100],
|
||||||
..Default::default()
|
|
||||||
};
|
};
|
||||||
let mut iter = parameters.into_iter();
|
let mut iter = parameters.into_iter();
|
||||||
let next = iter.next().unwrap();
|
let next = iter.next().unwrap();
|
||||||
|
|||||||
+1
-1
@@ -98,7 +98,7 @@ impl fmt::Display for FailedError {
|
|||||||
FailedError::SolutionFailed => "Can't find solution",
|
FailedError::SolutionFailed => "Can't find solution",
|
||||||
FailedError::ParametersError => "Error in input, check parameters",
|
FailedError::ParametersError => "Error in input, check parameters",
|
||||||
};
|
};
|
||||||
write!(f, "{}", failed_err_str)
|
write!(f, "{failed_err_str}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -3,7 +3,8 @@
|
|||||||
clippy::too_many_arguments,
|
clippy::too_many_arguments,
|
||||||
clippy::many_single_char_names,
|
clippy::many_single_char_names,
|
||||||
clippy::unnecessary_wraps,
|
clippy::unnecessary_wraps,
|
||||||
clippy::upper_case_acronyms
|
clippy::upper_case_acronyms,
|
||||||
|
clippy::approx_constant
|
||||||
)]
|
)]
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
#![warn(rustdoc::missing_doc_code_examples)]
|
#![warn(rustdoc::missing_doc_code_examples)]
|
||||||
|
|||||||
+12
-29
@@ -548,7 +548,7 @@ pub trait ArrayView2<T: Debug + Display + Copy + Sized>: Array<T, (usize, usize)
|
|||||||
let (nrows, ncols) = self.shape();
|
let (nrows, ncols) = self.shape();
|
||||||
for r in 0..nrows {
|
for r in 0..nrows {
|
||||||
let row: Vec<T> = (0..ncols).map(|c| *self.get((r, c))).collect();
|
let row: Vec<T> = (0..ncols).map(|c| *self.get((r, c))).collect();
|
||||||
writeln!(f, "{:?}", row)?
|
writeln!(f, "{row:?}")?
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -918,8 +918,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
|
|||||||
let len = self.shape();
|
let len = self.shape();
|
||||||
assert!(
|
assert!(
|
||||||
index.iter().all(|&i| i < len),
|
index.iter().all(|&i| i < len),
|
||||||
"All indices in `take` should be < {}",
|
"All indices in `take` should be < {len}"
|
||||||
len
|
|
||||||
);
|
);
|
||||||
Self::from_iterator(index.iter().map(move |&i| *self.get(i)), index.len())
|
Self::from_iterator(index.iter().map(move |&i| *self.get(i)), index.len())
|
||||||
}
|
}
|
||||||
@@ -990,10 +989,7 @@ pub trait Array1<T: Debug + Display + Copy + Sized>: MutArrayView1<T> + Sized +
|
|||||||
};
|
};
|
||||||
assert!(
|
assert!(
|
||||||
d1 == len,
|
d1 == len,
|
||||||
"Can not multiply {}x{} matrix by {} vector",
|
"Can not multiply {nrows}x{ncols} matrix by {len} vector"
|
||||||
nrows,
|
|
||||||
ncols,
|
|
||||||
len
|
|
||||||
);
|
);
|
||||||
let mut result = Self::zeros(d2);
|
let mut result = Self::zeros(d2);
|
||||||
for i in 0..d2 {
|
for i in 0..d2 {
|
||||||
@@ -1111,11 +1107,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
|||||||
|
|
||||||
assert!(
|
assert!(
|
||||||
nrows * ncols == onrows * oncols,
|
nrows * ncols == onrows * oncols,
|
||||||
"Can't reshape {}x{} array into a {}x{} array",
|
"Can't reshape {onrows}x{oncols} array into a {nrows}x{ncols} array"
|
||||||
onrows,
|
|
||||||
oncols,
|
|
||||||
nrows,
|
|
||||||
ncols
|
|
||||||
);
|
);
|
||||||
|
|
||||||
Self::from_iterator(self.iterator(0).cloned(), nrows, ncols, axis)
|
Self::from_iterator(self.iterator(0).cloned(), nrows, ncols, axis)
|
||||||
@@ -1129,11 +1121,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
|||||||
let (o_nrows, o_ncols) = other.shape();
|
let (o_nrows, o_ncols) = other.shape();
|
||||||
assert!(
|
assert!(
|
||||||
ncols == o_nrows,
|
ncols == o_nrows,
|
||||||
"Can't multiply {}x{} and {}x{} matrices",
|
"Can't multiply {nrows}x{ncols} and {o_nrows}x{o_ncols} matrices"
|
||||||
nrows,
|
|
||||||
ncols,
|
|
||||||
o_nrows,
|
|
||||||
o_ncols
|
|
||||||
);
|
);
|
||||||
let inner_d = ncols;
|
let inner_d = ncols;
|
||||||
let mut result = Self::zeros(nrows, o_ncols);
|
let mut result = Self::zeros(nrows, o_ncols);
|
||||||
@@ -1166,7 +1154,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
|||||||
_ => (nrows, ncols, o_nrows, o_ncols),
|
_ => (nrows, ncols, o_nrows, o_ncols),
|
||||||
};
|
};
|
||||||
if d1 != d4 {
|
if d1 != d4 {
|
||||||
panic!("Can not multiply {}x{} by {}x{} matrices", d2, d1, d4, d3);
|
panic!("Can not multiply {d2}x{d1} by {d4}x{d3} matrices");
|
||||||
}
|
}
|
||||||
let mut result = Self::zeros(d2, d3);
|
let mut result = Self::zeros(d2, d3);
|
||||||
for r in 0..d2 {
|
for r in 0..d2 {
|
||||||
@@ -1198,10 +1186,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
|||||||
};
|
};
|
||||||
assert!(
|
assert!(
|
||||||
d2 == len,
|
d2 == len,
|
||||||
"Can not multiply {}x{} matrix by {} vector",
|
"Can not multiply {nrows}x{ncols} matrix by {len} vector"
|
||||||
nrows,
|
|
||||||
ncols,
|
|
||||||
len
|
|
||||||
);
|
);
|
||||||
let mut result = Self::zeros(d1, 1);
|
let mut result = Self::zeros(d1, 1);
|
||||||
for i in 0..d1 {
|
for i in 0..d1 {
|
||||||
@@ -1432,8 +1417,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
|||||||
0 => {
|
0 => {
|
||||||
assert!(
|
assert!(
|
||||||
index.iter().all(|&i| i < nrows),
|
index.iter().all(|&i| i < nrows),
|
||||||
"All indices in `take` should be < {}",
|
"All indices in `take` should be < {nrows}"
|
||||||
nrows
|
|
||||||
);
|
);
|
||||||
Self::from_iterator(
|
Self::from_iterator(
|
||||||
index
|
index
|
||||||
@@ -1448,8 +1432,7 @@ pub trait Array2<T: Debug + Display + Copy + Sized>: MutArrayView2<T> + Sized +
|
|||||||
_ => {
|
_ => {
|
||||||
assert!(
|
assert!(
|
||||||
index.iter().all(|&i| i < ncols),
|
index.iter().all(|&i| i < ncols),
|
||||||
"All indices in `take` should be < {}",
|
"All indices in `take` should be < {ncols}"
|
||||||
ncols
|
|
||||||
);
|
);
|
||||||
Self::from_iterator(
|
Self::from_iterator(
|
||||||
(0..nrows)
|
(0..nrows)
|
||||||
@@ -1736,7 +1719,7 @@ mod tests {
|
|||||||
let r = Vec::<f32>::rand(4);
|
let r = Vec::<f32>::rand(4);
|
||||||
assert!(r.iterator(0).all(|&e| e <= 1f32));
|
assert!(r.iterator(0).all(|&e| e <= 1f32));
|
||||||
assert!(r.iterator(0).all(|&e| e >= 0f32));
|
assert!(r.iterator(0).all(|&e| e >= 0f32));
|
||||||
assert!(r.iterator(0).map(|v| *v).sum::<f32>() > 0f32);
|
assert!(r.iterator(0).copied().sum::<f32>() > 0f32);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -1954,7 +1937,7 @@ mod tests {
|
|||||||
DenseMatrix::from_2d_array(&[&[1, 3], &[2, 4]])
|
DenseMatrix::from_2d_array(&[&[1, 3], &[2, 4]])
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
DenseMatrix::concatenate_2d(&[&a.clone(), &b.clone()], 0),
|
DenseMatrix::concatenate_2d(&[&a, &b], 0),
|
||||||
DenseMatrix::from_2d_array(&[&[1, 2], &[3, 4], &[5, 6], &[7, 8]])
|
DenseMatrix::from_2d_array(&[&[1, 2], &[3, 4], &[5, 6], &[7, 8]])
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -2025,7 +2008,7 @@ mod tests {
|
|||||||
let r = DenseMatrix::<f32>::rand(2, 2);
|
let r = DenseMatrix::<f32>::rand(2, 2);
|
||||||
assert!(r.iterator(0).all(|&e| e <= 1f32));
|
assert!(r.iterator(0).all(|&e| e <= 1f32));
|
||||||
assert!(r.iterator(0).all(|&e| e >= 0f32));
|
assert!(r.iterator(0).all(|&e| e >= 0f32));
|
||||||
assert!(r.iterator(0).map(|v| *v).sum::<f32>() > 0f32);
|
assert!(r.iterator(0).copied().sum::<f32>() > 0f32);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -581,9 +581,9 @@ mod tests {
|
|||||||
vec![4, 5, 6],
|
vec![4, 5, 6],
|
||||||
DenseMatrix::from_slice(&(*x.slice(1..2, 0..3))).values
|
DenseMatrix::from_slice(&(*x.slice(1..2, 0..3))).values
|
||||||
);
|
);
|
||||||
let second_row: Vec<i32> = x.slice(1..2, 0..3).iterator(0).map(|x| *x).collect();
|
let second_row: Vec<i32> = x.slice(1..2, 0..3).iterator(0).copied().collect();
|
||||||
assert_eq!(vec![4, 5, 6], second_row);
|
assert_eq!(vec![4, 5, 6], second_row);
|
||||||
let second_col: Vec<i32> = x.slice(0..3, 1..2).iterator(0).map(|x| *x).collect();
|
let second_col: Vec<i32> = x.slice(0..3, 1..2).iterator(0).copied().collect();
|
||||||
assert_eq!(vec![2, 5, 8], second_col);
|
assert_eq!(vec![2, 5, 8], second_col);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -640,12 +640,12 @@ mod tests {
|
|||||||
let x = DenseMatrix::<&str>::from_2d_array(&[&["1", "2", "3"], &["4", "5", "6"]]);
|
let x = DenseMatrix::<&str>::from_2d_array(&[&["1", "2", "3"], &["4", "5", "6"]]);
|
||||||
|
|
||||||
assert_eq!(vec!["1", "4", "2", "5", "3", "6"], x.values);
|
assert_eq!(vec!["1", "4", "2", "5", "3", "6"], x.values);
|
||||||
assert!(x.column_major == true);
|
assert!(x.column_major);
|
||||||
|
|
||||||
// transpose
|
// transpose
|
||||||
let x = x.transpose();
|
let x = x.transpose();
|
||||||
assert_eq!(vec!["1", "4", "2", "5", "3", "6"], x.values);
|
assert_eq!(vec!["1", "4", "2", "5", "3", "6"], x.values);
|
||||||
assert!(x.column_major == false); // should change column_major
|
assert!(!x.column_major); // should change column_major
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -659,7 +659,7 @@ mod tests {
|
|||||||
vec![1, 2, 3, 4, 5, 6],
|
vec![1, 2, 3, 4, 5, 6],
|
||||||
m.values.iter().map(|e| **e).collect::<Vec<i32>>()
|
m.values.iter().map(|e| **e).collect::<Vec<i32>>()
|
||||||
);
|
);
|
||||||
assert!(m.column_major == false);
|
assert!(!m.column_major);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -667,10 +667,10 @@ mod tests {
|
|||||||
let a = DenseMatrix::from_2d_array(&[&[1, 2, 3], &[4, 5, 6]]);
|
let a = DenseMatrix::from_2d_array(&[&[1, 2, 3], &[4, 5, 6]]);
|
||||||
let b = DenseMatrix::from_2d_array(&[&[1, 2], &[3, 4], &[5, 6]]);
|
let b = DenseMatrix::from_2d_array(&[&[1, 2], &[3, 4], &[5, 6]]);
|
||||||
|
|
||||||
println!("{}", a);
|
println!("{a}");
|
||||||
// take column 0 and 2
|
// take column 0 and 2
|
||||||
assert_eq!(vec![1, 3, 4, 6], a.take(&[0, 2], 1).values);
|
assert_eq!(vec![1, 3, 4, 6], a.take(&[0, 2], 1).values);
|
||||||
println!("{}", b);
|
println!("{b}");
|
||||||
// take rows 0 and 2
|
// take rows 0 and 2
|
||||||
assert_eq!(vec![1, 2, 5, 6], b.take(&[0, 2], 0).values);
|
assert_eq!(vec![1, 2, 5, 6], b.take(&[0, 2], 0).values);
|
||||||
}
|
}
|
||||||
@@ -692,11 +692,11 @@ mod tests {
|
|||||||
|
|
||||||
let a = a.reshape(2, 6, 0);
|
let a = a.reshape(2, 6, 0);
|
||||||
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], a.values);
|
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], a.values);
|
||||||
assert!(a.ncols == 6 && a.nrows == 2 && a.column_major == false);
|
assert!(a.ncols == 6 && a.nrows == 2 && !a.column_major);
|
||||||
|
|
||||||
let a = a.reshape(3, 4, 1);
|
let a = a.reshape(3, 4, 1);
|
||||||
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], a.values);
|
assert_eq!(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], a.values);
|
||||||
assert!(a.ncols == 4 && a.nrows == 3 && a.column_major == true);
|
assert!(a.ncols == 4 && a.nrows == 3 && a.column_major);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -160,8 +160,8 @@ mod tests {
|
|||||||
fn dot_product<T: Number, V: Array1<T>>(v: &V) -> T {
|
fn dot_product<T: Number, V: Array1<T>>(v: &V) -> T {
|
||||||
let vv = V::zeros(10);
|
let vv = V::zeros(10);
|
||||||
let v_s = vv.slice(0..3);
|
let v_s = vv.slice(0..3);
|
||||||
let dot = v_s.dot(v);
|
|
||||||
dot
|
v_s.dot(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vector_ops<T: Number + PartialOrd, V: Array1<T>>(_: &V) -> T {
|
fn vector_ops<T: Number + PartialOrd, V: Array1<T>>(_: &V) -> T {
|
||||||
@@ -216,7 +216,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_mut_iterator() {
|
fn test_mut_iterator() {
|
||||||
let mut x = vec![1, 2, 3];
|
let mut x = vec![1, 2, 3];
|
||||||
x.iterator_mut(0).for_each(|v| *v = *v * 2);
|
x.iterator_mut(0).for_each(|v| *v *= 2);
|
||||||
assert_eq!(vec![2, 4, 6], x);
|
assert_eq!(vec![2, 4, 6], x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -217,7 +217,7 @@ mod tests {
|
|||||||
fn test_iterator() {
|
fn test_iterator() {
|
||||||
let a = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
let a = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||||
|
|
||||||
let v: Vec<i32> = a.iterator(0).map(|&v| v).collect();
|
let v: Vec<i32> = a.iterator(0).copied().collect();
|
||||||
assert_eq!(v, vec!(1, 2, 3, 4, 5, 6));
|
assert_eq!(v, vec!(1, 2, 3, 4, 5, 6));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,7 +236,7 @@ mod tests {
|
|||||||
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||||
let x_slice = Array2::slice(&x, 0..2, 1..2);
|
let x_slice = Array2::slice(&x, 0..2, 1..2);
|
||||||
assert_eq!((2, 1), x_slice.shape());
|
assert_eq!((2, 1), x_slice.shape());
|
||||||
let v: Vec<i32> = x_slice.iterator(0).map(|&v| v).collect();
|
let v: Vec<i32> = x_slice.iterator(0).copied().collect();
|
||||||
assert_eq!(v, [2, 5]);
|
assert_eq!(v, [2, 5]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,11 +245,11 @@ mod tests {
|
|||||||
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
let x = arr2(&[[1, 2, 3], [4, 5, 6]]);
|
||||||
let x_slice = Array2::slice(&x, 0..2, 0..3);
|
let x_slice = Array2::slice(&x, 0..2, 0..3);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
x_slice.iterator(0).map(|&v| v).collect::<Vec<i32>>(),
|
x_slice.iterator(0).copied().collect::<Vec<i32>>(),
|
||||||
vec![1, 2, 3, 4, 5, 6]
|
vec![1, 2, 3, 4, 5, 6]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
x_slice.iterator(1).map(|&v| v).collect::<Vec<i32>>(),
|
x_slice.iterator(1).copied().collect::<Vec<i32>>(),
|
||||||
vec![1, 4, 2, 5, 3, 6]
|
vec![1, 4, 2, 5, 3, 6]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -279,8 +279,8 @@ mod tests {
|
|||||||
fn test_c_from_iterator() {
|
fn test_c_from_iterator() {
|
||||||
let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
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);
|
let a: NDArray2<i32> = Array2::from_iterator(data.clone().into_iter(), 4, 3, 0);
|
||||||
println!("{}", a);
|
println!("{a}");
|
||||||
let a: NDArray2<i32> = Array2::from_iterator(data.into_iter(), 4, 3, 1);
|
let a: NDArray2<i32> = Array2::from_iterator(data.into_iter(), 4, 3, 1);
|
||||||
println!("{}", a);
|
println!("{a}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ mod tests {
|
|||||||
fn test_iterator() {
|
fn test_iterator() {
|
||||||
let a = arr1(&[1, 2, 3]);
|
let a = arr1(&[1, 2, 3]);
|
||||||
|
|
||||||
let v: Vec<i32> = a.iterator(0).map(|&v| v).collect();
|
let v: Vec<i32> = a.iterator(0).copied().collect();
|
||||||
assert_eq!(v, vec!(1, 2, 3));
|
assert_eq!(v, vec!(1, 2, 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ pub trait EVDDecomposable<T: Number + RealNumber>: Array2<T> {
|
|||||||
fn evd_mut(mut self, symmetric: bool) -> Result<EVD<T, Self>, Failed> {
|
fn evd_mut(mut self, symmetric: bool) -> Result<EVD<T, Self>, Failed> {
|
||||||
let (nrows, ncols) = self.shape();
|
let (nrows, ncols) = self.shape();
|
||||||
if ncols != nrows {
|
if ncols != nrows {
|
||||||
panic!("Matrix is not square: {} x {}", nrows, ncols);
|
panic!("Matrix is not square: {nrows} x {ncols}");
|
||||||
}
|
}
|
||||||
|
|
||||||
let n = nrows;
|
let n = nrows;
|
||||||
@@ -837,10 +837,8 @@ mod tests {
|
|||||||
evd.V.abs(),
|
evd.V.abs(),
|
||||||
epsilon = 1e-4
|
epsilon = 1e-4
|
||||||
));
|
));
|
||||||
for i in 0..eigen_values.len() {
|
for (i, eigen_values_i) in eigen_values.iter().enumerate() {
|
||||||
assert!((eigen_values[i] - evd.d[i]).abs() < 1e-4);
|
assert!((eigen_values_i - evd.d[i]).abs() < 1e-4);
|
||||||
}
|
|
||||||
for i in 0..eigen_values.len() {
|
|
||||||
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
|
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -871,10 +869,8 @@ mod tests {
|
|||||||
evd.V.abs(),
|
evd.V.abs(),
|
||||||
epsilon = 1e-4
|
epsilon = 1e-4
|
||||||
));
|
));
|
||||||
for i in 0..eigen_values.len() {
|
for (i, eigen_values_i) in eigen_values.iter().enumerate() {
|
||||||
assert!((eigen_values[i] - evd.d[i]).abs() < 1e-4);
|
assert!((eigen_values_i - evd.d[i]).abs() < 1e-4);
|
||||||
}
|
|
||||||
for i in 0..eigen_values.len() {
|
|
||||||
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
|
assert!((0f64 - evd.e[i]).abs() < std::f64::EPSILON);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -908,11 +904,11 @@ mod tests {
|
|||||||
evd.V.abs(),
|
evd.V.abs(),
|
||||||
epsilon = 1e-4
|
epsilon = 1e-4
|
||||||
));
|
));
|
||||||
for i in 0..eigen_values_d.len() {
|
for (i, eigen_values_d_i) in eigen_values_d.iter().enumerate() {
|
||||||
assert!((eigen_values_d[i] - evd.d[i]).abs() < 1e-4);
|
assert!((eigen_values_d_i - evd.d[i]).abs() < 1e-4);
|
||||||
}
|
}
|
||||||
for i in 0..eigen_values_e.len() {
|
for (i, eigen_values_e_i) in eigen_values_e.iter().enumerate() {
|
||||||
assert!((eigen_values_e[i] - evd.e[i]).abs() < 1e-4);
|
assert!((eigen_values_e_i - evd.e[i]).abs() < 1e-4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ impl<T: Number + RealNumber, M: Array2<T>> LU<T, M> {
|
|||||||
let (m, n) = self.LU.shape();
|
let (m, n) = self.LU.shape();
|
||||||
|
|
||||||
if m != n {
|
if m != n {
|
||||||
panic!("Matrix is not square: {}x{}", m, n);
|
panic!("Matrix is not square: {m}x{n}");
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut inv = M::zeros(n, n);
|
let mut inv = M::zeros(n, n);
|
||||||
@@ -143,10 +143,7 @@ impl<T: Number + RealNumber, M: Array2<T>> LU<T, M> {
|
|||||||
let (b_m, b_n) = b.shape();
|
let (b_m, b_n) = b.shape();
|
||||||
|
|
||||||
if b_m != m {
|
if b_m != m {
|
||||||
panic!(
|
panic!("Row dimensions do not agree: A is {m} x {n}, but B is {b_m} x {b_n}");
|
||||||
"Row dimensions do not agree: A is {} x {}, but B is {} x {}",
|
|
||||||
m, n, b_m, b_n
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.singular {
|
if self.singular {
|
||||||
|
|||||||
@@ -102,10 +102,7 @@ impl<T: Number + RealNumber, M: Array2<T>> QR<T, M> {
|
|||||||
let (b_nrows, b_ncols) = b.shape();
|
let (b_nrows, b_ncols) = b.shape();
|
||||||
|
|
||||||
if b_nrows != m {
|
if b_nrows != m {
|
||||||
panic!(
|
panic!("Row dimensions do not agree: A is {m} x {n}, but B is {b_nrows} x {b_ncols}");
|
||||||
"Row dimensions do not agree: A is {} x {}, but B is {} x {}",
|
|
||||||
m, n, b_nrows, b_ncols
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.singular {
|
if self.singular {
|
||||||
|
|||||||
@@ -286,7 +286,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut m = m.clone();
|
let mut m = m;
|
||||||
m.standard_scale_mut(&m.mean(1), &m.std(1), 1);
|
m.standard_scale_mut(&m.mean(1), &m.std(1), 1);
|
||||||
assert_eq!(&m, &expected_1);
|
assert_eq!(&m, &expected_1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -509,8 +509,8 @@ mod tests {
|
|||||||
|
|
||||||
assert!(relative_eq!(V.abs(), svd.V.abs(), epsilon = 1e-4));
|
assert!(relative_eq!(V.abs(), svd.V.abs(), epsilon = 1e-4));
|
||||||
assert!(relative_eq!(U.abs(), svd.U.abs(), epsilon = 1e-4));
|
assert!(relative_eq!(U.abs(), svd.U.abs(), epsilon = 1e-4));
|
||||||
for i in 0..s.len() {
|
for (i, s_i) in s.iter().enumerate() {
|
||||||
assert!((s[i] - svd.s[i]).abs() < 1e-4);
|
assert!((s_i - svd.s[i]).abs() < 1e-4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
@@ -713,8 +713,8 @@ mod tests {
|
|||||||
|
|
||||||
assert!(relative_eq!(V.abs(), svd.V.abs(), epsilon = 1e-4));
|
assert!(relative_eq!(V.abs(), svd.V.abs(), epsilon = 1e-4));
|
||||||
assert!(relative_eq!(U.abs(), svd.U.abs(), epsilon = 1e-4));
|
assert!(relative_eq!(U.abs(), svd.U.abs(), epsilon = 1e-4));
|
||||||
for i in 0..s.len() {
|
for (i, s_i) in s.iter().enumerate() {
|
||||||
assert!((s[i] - svd.s[i]).abs() < 1e-4);
|
assert!((s_i - svd.s[i]).abs() < 1e-4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
|
|||||||
@@ -425,10 +425,7 @@ impl<TX: FloatNumber + RealNumber, TY: Number, X: Array2<TX>, Y: Array1<TY>>
|
|||||||
|
|
||||||
for (i, col_std_i) in col_std.iter().enumerate() {
|
for (i, col_std_i) in col_std.iter().enumerate() {
|
||||||
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
|
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!("Cannot rescale constant column {i}")));
|
||||||
"Cannot rescale constant column {}",
|
|
||||||
i
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-4
@@ -356,10 +356,7 @@ impl<TX: FloatNumber + RealNumber, TY: Number, X: Array2<TX>, Y: Array1<TY>> Las
|
|||||||
|
|
||||||
for (i, col_std_i) in col_std.iter().enumerate() {
|
for (i, col_std_i) in col_std.iter().enumerate() {
|
||||||
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
|
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!("Cannot rescale constant column {i}")));
|
||||||
"Cannot rescale constant column {}",
|
|
||||||
i
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -449,8 +449,7 @@ impl<TX: Number + FloatNumber + RealNumber, TY: Number + Ord, X: Array2<TX>, Y:
|
|||||||
|
|
||||||
match k.cmp(&2) {
|
match k.cmp(&2) {
|
||||||
Ordering::Less => Err(Failed::fit(&format!(
|
Ordering::Less => Err(Failed::fit(&format!(
|
||||||
"incorrect number of classes: {}. Should be >= 2.",
|
"incorrect number of classes: {k}. Should be >= 2."
|
||||||
k
|
|
||||||
))),
|
))),
|
||||||
Ordering::Equal => {
|
Ordering::Equal => {
|
||||||
let x0 = Vec::zeros(num_attributes + 1);
|
let x0 = Vec::zeros(num_attributes + 1);
|
||||||
@@ -636,19 +635,19 @@ mod tests {
|
|||||||
|
|
||||||
assert!((g[0] + 33.000068218163484).abs() < std::f64::EPSILON);
|
assert!((g[0] + 33.000068218163484).abs() < std::f64::EPSILON);
|
||||||
|
|
||||||
let f = objective.f(&vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
let f = objective.f(&[1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
||||||
|
|
||||||
assert!((f - 408.0052230582765).abs() < std::f64::EPSILON);
|
assert!((f - 408.0052230582765).abs() < std::f64::EPSILON);
|
||||||
|
|
||||||
let objective_reg = MultiClassObjectiveFunction {
|
let objective_reg = MultiClassObjectiveFunction {
|
||||||
x: &x,
|
x: &x,
|
||||||
y: y.clone(),
|
y,
|
||||||
k: 3,
|
k: 3,
|
||||||
alpha: 1.0,
|
alpha: 1.0,
|
||||||
_phantom_t: PhantomData,
|
_phantom_t: PhantomData,
|
||||||
};
|
};
|
||||||
|
|
||||||
let f = objective_reg.f(&vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
let f = objective_reg.f(&[1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
||||||
assert!((f - 487.5052).abs() < 1e-4);
|
assert!((f - 487.5052).abs() < 1e-4);
|
||||||
|
|
||||||
objective_reg.df(&mut g, &vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
objective_reg.df(&mut g, &vec![1., 2., 3., 4., 5., 6., 7., 8., 9.]);
|
||||||
@@ -697,18 +696,18 @@ mod tests {
|
|||||||
assert!((g[1] - 10.239000702928523).abs() < std::f64::EPSILON);
|
assert!((g[1] - 10.239000702928523).abs() < std::f64::EPSILON);
|
||||||
assert!((g[2] - 3.869294270156324).abs() < std::f64::EPSILON);
|
assert!((g[2] - 3.869294270156324).abs() < std::f64::EPSILON);
|
||||||
|
|
||||||
let f = objective.f(&vec![1., 2., 3.]);
|
let f = objective.f(&[1., 2., 3.]);
|
||||||
|
|
||||||
assert!((f - 59.76994756647412).abs() < std::f64::EPSILON);
|
assert!((f - 59.76994756647412).abs() < std::f64::EPSILON);
|
||||||
|
|
||||||
let objective_reg = BinaryObjectiveFunction {
|
let objective_reg = BinaryObjectiveFunction {
|
||||||
x: &x,
|
x: &x,
|
||||||
y: y.clone(),
|
y,
|
||||||
alpha: 1.0,
|
alpha: 1.0,
|
||||||
_phantom_t: PhantomData,
|
_phantom_t: PhantomData,
|
||||||
};
|
};
|
||||||
|
|
||||||
let f = objective_reg.f(&vec![1., 2., 3.]);
|
let f = objective_reg.f(&[1., 2., 3.]);
|
||||||
assert!((f - 62.2699).abs() < 1e-4);
|
assert!((f - 62.2699).abs() < 1e-4);
|
||||||
|
|
||||||
objective_reg.df(&mut g, &vec![1., 2., 3.]);
|
objective_reg.df(&mut g, &vec![1., 2., 3.]);
|
||||||
|
|||||||
@@ -384,10 +384,7 @@ impl<
|
|||||||
|
|
||||||
for (i, col_std_i) in col_std.iter().enumerate() {
|
for (i, col_std_i) in col_std.iter().enumerate() {
|
||||||
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
|
if (*col_std_i - TX::zero()).abs() < TX::epsilon() {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!("Cannot rescale constant column {i}")));
|
||||||
"Cannot rescale constant column {}",
|
|
||||||
i
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -98,8 +98,8 @@ mod tests {
|
|||||||
let mut scores = HCVScore::new();
|
let mut scores = HCVScore::new();
|
||||||
scores.compute(&v1, &v2);
|
scores.compute(&v1, &v2);
|
||||||
|
|
||||||
assert!((0.2548 - scores.homogeneity.unwrap() as f64).abs() < 1e-4);
|
assert!((0.2548 - scores.homogeneity.unwrap()).abs() < 1e-4);
|
||||||
assert!((0.5440 - scores.completeness.unwrap() as f64).abs() < 1e-4);
|
assert!((0.5440 - scores.completeness.unwrap()).abs() < 1e-4);
|
||||||
assert!((0.3471 - scores.v_measure.unwrap() as f64).abs() < 1e-4);
|
assert!((0.3471 - scores.v_measure.unwrap()).abs() < 1e-4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ mod tests {
|
|||||||
fn entropy_test() {
|
fn entropy_test() {
|
||||||
let v1 = vec![0, 0, 1, 1, 2, 0, 4];
|
let v1 = vec![0, 0, 1, 1, 2, 0, 4];
|
||||||
|
|
||||||
assert!((1.2770 - entropy(&v1).unwrap() as f64).abs() < 1e-4);
|
assert!((1.2770 - entropy(&v1).unwrap()).abs() < 1e-4);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
|
|||||||
+2
-2
@@ -95,8 +95,8 @@ mod tests {
|
|||||||
let score1: f64 = F1::new_with(beta).get_score(&y_true, &y_pred);
|
let score1: f64 = F1::new_with(beta).get_score(&y_true, &y_pred);
|
||||||
let score2: f64 = F1::new_with(beta).get_score(&y_true, &y_true);
|
let score2: f64 = F1::new_with(beta).get_score(&y_true, &y_true);
|
||||||
|
|
||||||
println!("{:?}", score1);
|
println!("{score1:?}");
|
||||||
println!("{:?}", score2);
|
println!("{score2:?}");
|
||||||
|
|
||||||
assert!((score1 - 0.57142857).abs() < 1e-8);
|
assert!((score1 - 0.57142857).abs() < 1e-8);
|
||||||
assert!((score2 - 1.0).abs() < 1e-8);
|
assert!((score2 - 1.0).abs() < 1e-8);
|
||||||
|
|||||||
@@ -213,17 +213,17 @@ mod tests {
|
|||||||
|
|
||||||
for t in &test_masks[0][0..11] {
|
for t in &test_masks[0][0..11] {
|
||||||
// TODO: this can be prob done better
|
// TODO: this can be prob done better
|
||||||
assert_eq!(*t, true)
|
assert!(*t)
|
||||||
}
|
}
|
||||||
for t in &test_masks[0][11..22] {
|
for t in &test_masks[0][11..22] {
|
||||||
assert_eq!(*t, false)
|
assert!(!*t)
|
||||||
}
|
}
|
||||||
|
|
||||||
for t in &test_masks[1][0..11] {
|
for t in &test_masks[1][0..11] {
|
||||||
assert_eq!(*t, false)
|
assert!(!*t)
|
||||||
}
|
}
|
||||||
for t in &test_masks[1][11..22] {
|
for t in &test_masks[1][11..22] {
|
||||||
assert_eq!(*t, true)
|
assert!(*t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ pub fn train_test_split<
|
|||||||
let n_test = ((n as f32) * test_size) as usize;
|
let n_test = ((n as f32) * test_size) as usize;
|
||||||
|
|
||||||
if n_test < 1 {
|
if n_test < 1 {
|
||||||
panic!("number of sample is too small {}", n);
|
panic!("number of sample is too small {n}");
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut indices: Vec<usize> = (0..n).collect();
|
let mut indices: Vec<usize> = (0..n).collect();
|
||||||
@@ -553,6 +553,6 @@ mod tests {
|
|||||||
&accuracy,
|
&accuracy,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
println!("{:?}", results);
|
println!("{results:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -271,21 +271,18 @@ impl<TY: Number + Ord + Unsigned> BernoulliNBDistribution<TY> {
|
|||||||
let y_samples = y.shape();
|
let y_samples = y.shape();
|
||||||
if y_samples != n_samples {
|
if y_samples != n_samples {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
|
||||||
n_samples, y_samples
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if n_samples == 0 {
|
if n_samples == 0 {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x and y should greater than 0; |x|=[{}]",
|
"Size of x and y should greater than 0; |x|=[{n_samples}]"
|
||||||
n_samples
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
if alpha < 0f64 {
|
if alpha < 0f64 {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Alpha should be greater than 0; |alpha|=[{}]",
|
"Alpha should be greater than 0; |alpha|=[{alpha}]"
|
||||||
alpha
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,8 +315,7 @@ impl<TY: Number + Ord + Unsigned> BernoulliNBDistribution<TY> {
|
|||||||
feature_in_class_counter[class_index][idx] +=
|
feature_in_class_counter[class_index][idx] +=
|
||||||
row_i.to_usize().ok_or_else(|| {
|
row_i.to_usize().ok_or_else(|| {
|
||||||
Failed::fit(&format!(
|
Failed::fit(&format!(
|
||||||
"Elements of the matrix should be 1.0 or 0.0 |found|=[{}]",
|
"Elements of the matrix should be 1.0 or 0.0 |found|=[{row_i}]"
|
||||||
row_i
|
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -158,8 +158,7 @@ impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
|
|||||||
pub fn fit<X: Array2<T>, Y: Array1<T>>(x: &X, y: &Y, alpha: f64) -> Result<Self, Failed> {
|
pub fn fit<X: Array2<T>, Y: Array1<T>>(x: &X, y: &Y, alpha: f64) -> Result<Self, Failed> {
|
||||||
if alpha < 0f64 {
|
if alpha < 0f64 {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"alpha should be >= 0, alpha=[{}]",
|
"alpha should be >= 0, alpha=[{alpha}]"
|
||||||
alpha
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,15 +166,13 @@ impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
|
|||||||
let y_samples = y.shape();
|
let y_samples = y.shape();
|
||||||
if y_samples != n_samples {
|
if y_samples != n_samples {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
|
||||||
n_samples, y_samples
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if n_samples == 0 {
|
if n_samples == 0 {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x and y should greater than 0; |x|=[{}]",
|
"Size of x and y should greater than 0; |x|=[{n_samples}]"
|
||||||
n_samples
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
let y: Vec<usize> = y.iterator(0).map(|y_i| y_i.to_usize().unwrap()).collect();
|
let y: Vec<usize> = y.iterator(0).map(|y_i| y_i.to_usize().unwrap()).collect();
|
||||||
@@ -202,8 +199,7 @@ impl<T: Number + Unsigned> CategoricalNBDistribution<T> {
|
|||||||
.max()
|
.max()
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
Failed::fit(&format!(
|
Failed::fit(&format!(
|
||||||
"Failed to get the categories for feature = {}",
|
"Failed to get the categories for feature = {feature}"
|
||||||
feature
|
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
n_categories.push(feature_max + 1);
|
n_categories.push(feature_max + 1);
|
||||||
@@ -429,7 +425,6 @@ mod tests {
|
|||||||
fn search_parameters() {
|
fn search_parameters() {
|
||||||
let parameters = CategoricalNBSearchParameters {
|
let parameters = CategoricalNBSearchParameters {
|
||||||
alpha: vec![1., 2.],
|
alpha: vec![1., 2.],
|
||||||
..Default::default()
|
|
||||||
};
|
};
|
||||||
let mut iter = parameters.into_iter();
|
let mut iter = parameters.into_iter();
|
||||||
let next = iter.next().unwrap();
|
let next = iter.next().unwrap();
|
||||||
|
|||||||
@@ -185,15 +185,13 @@ impl<TY: Number + Ord + Unsigned> GaussianNBDistribution<TY> {
|
|||||||
let y_samples = y.shape();
|
let y_samples = y.shape();
|
||||||
if y_samples != n_samples {
|
if y_samples != n_samples {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
|
||||||
n_samples, y_samples
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if n_samples == 0 {
|
if n_samples == 0 {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x and y should greater than 0; |x|=[{}]",
|
"Size of x and y should greater than 0; |x|=[{n_samples}]"
|
||||||
n_samples
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
let (class_labels, indices) = y.unique_with_indices();
|
let (class_labels, indices) = y.unique_with_indices();
|
||||||
@@ -375,7 +373,6 @@ mod tests {
|
|||||||
fn search_parameters() {
|
fn search_parameters() {
|
||||||
let parameters = GaussianNBSearchParameters {
|
let parameters = GaussianNBSearchParameters {
|
||||||
priors: vec![Some(vec![1.]), Some(vec![2.])],
|
priors: vec![Some(vec![1.]), Some(vec![2.])],
|
||||||
..Default::default()
|
|
||||||
};
|
};
|
||||||
let mut iter = parameters.into_iter();
|
let mut iter = parameters.into_iter();
|
||||||
let next = iter.next().unwrap();
|
let next = iter.next().unwrap();
|
||||||
|
|||||||
@@ -220,21 +220,18 @@ impl<TY: Number + Ord + Unsigned> MultinomialNBDistribution<TY> {
|
|||||||
let y_samples = y.shape();
|
let y_samples = y.shape();
|
||||||
if y_samples != n_samples {
|
if y_samples != n_samples {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
"Size of x should equal size of y; |x|=[{n_samples}], |y|=[{y_samples}]"
|
||||||
n_samples, y_samples
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if n_samples == 0 {
|
if n_samples == 0 {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x and y should greater than 0; |x|=[{}]",
|
"Size of x and y should greater than 0; |x|=[{n_samples}]"
|
||||||
n_samples
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
if alpha < 0f64 {
|
if alpha < 0f64 {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Alpha should be greater than 0; |alpha|=[{}]",
|
"Alpha should be greater than 0; |alpha|=[{alpha}]"
|
||||||
alpha
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,8 +263,7 @@ impl<TY: Number + Ord + Unsigned> MultinomialNBDistribution<TY> {
|
|||||||
feature_in_class_counter[class_index][idx] +=
|
feature_in_class_counter[class_index][idx] +=
|
||||||
row_i.to_usize().ok_or_else(|| {
|
row_i.to_usize().ok_or_else(|| {
|
||||||
Failed::fit(&format!(
|
Failed::fit(&format!(
|
||||||
"Elements of the matrix should be convertible to usize |found|=[{}]",
|
"Elements of the matrix should be convertible to usize |found|=[{row_i}]"
|
||||||
row_i
|
|
||||||
))
|
))
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -236,8 +236,7 @@ impl<TX: Number, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec
|
|||||||
|
|
||||||
if x_n != y_n {
|
if x_n != y_n {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
"Size of x should equal size of y; |x|=[{x_n}], |y|=[{y_n}]"
|
||||||
x_n, y_n
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -224,8 +224,7 @@ impl<TX: Number, TY: Number, X: Array2<TX>, Y: Array1<TY>, D: Distance<Vec<TX>>>
|
|||||||
|
|
||||||
if x_n != y_n {
|
if x_n != y_n {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Size of x should equal size of y; |x|=[{}], |y|=[{}]",
|
"Size of x should equal size of y; |x|=[{x_n}], |y|=[{y_n}]"
|
||||||
x_n, y_n
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -113,12 +113,13 @@ mod tests {
|
|||||||
g[1] = 200. * (x[1] - x[0].powf(2.));
|
g[1] = 200. * (x[1] - x[0].powf(2.));
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut ls: Backtracking<f64> = Default::default();
|
let ls: Backtracking<f64> = Backtracking::<f64> {
|
||||||
ls.order = FunctionOrder::THIRD;
|
order: FunctionOrder::THIRD,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
let optimizer: GradientDescent = Default::default();
|
let optimizer: GradientDescent = Default::default();
|
||||||
|
|
||||||
let result = optimizer.optimize(&f, &df, &x0, &ls);
|
let result = optimizer.optimize(&f, &df, &x0, &ls);
|
||||||
println!("{:?}", result);
|
|
||||||
|
|
||||||
assert!((result.f_x - 0.0).abs() < 1e-5);
|
assert!((result.f_x - 0.0).abs() < 1e-5);
|
||||||
assert!((result.x[0] - 1.0).abs() < 1e-2);
|
assert!((result.x[0] - 1.0).abs() < 1e-2);
|
||||||
|
|||||||
@@ -196,9 +196,9 @@ impl LBFGS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
fn update_hessian<'a, T: FloatNumber, X: Array1<T>>(
|
fn update_hessian<T: FloatNumber, X: Array1<T>>(
|
||||||
&self,
|
&self,
|
||||||
_: &'a DF<'_, X>,
|
_: &DF<'_, X>,
|
||||||
state: &mut LBFGSState<T, X>,
|
state: &mut LBFGSState<T, X>,
|
||||||
) {
|
) {
|
||||||
state.dg = state.x_df.sub(&state.x_df_prev);
|
state.dg = state.x_df.sub(&state.x_df_prev);
|
||||||
@@ -291,8 +291,10 @@ mod tests {
|
|||||||
g[0] = -2. * (1. - x[0]) - 400. * (x[1] - x[0].powf(2.)) * x[0];
|
g[0] = -2. * (1. - x[0]) - 400. * (x[1] - x[0].powf(2.)) * x[0];
|
||||||
g[1] = 200. * (x[1] - x[0].powf(2.));
|
g[1] = 200. * (x[1] - x[0].powf(2.));
|
||||||
};
|
};
|
||||||
let mut ls: Backtracking<f64> = Default::default();
|
let ls: Backtracking<f64> = Backtracking::<f64> {
|
||||||
ls.order = FunctionOrder::THIRD;
|
order: FunctionOrder::THIRD,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
let optimizer: LBFGS = Default::default();
|
let optimizer: LBFGS = Default::default();
|
||||||
|
|
||||||
let result = optimizer.optimize(&f, &df, &x0, &ls);
|
let result = optimizer.optimize(&f, &df, &x0, &ls);
|
||||||
|
|||||||
@@ -132,8 +132,7 @@ impl OneHotEncoder {
|
|||||||
data.copy_col_as_vec(idx, &mut col_buf);
|
data.copy_col_as_vec(idx, &mut col_buf);
|
||||||
if !validate_col_is_categorical(&col_buf) {
|
if !validate_col_is_categorical(&col_buf) {
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"Column {} of data matrix containts non categorizable (integer) values",
|
"Column {idx} of data matrix containts non categorizable (integer) values"
|
||||||
idx
|
|
||||||
);
|
);
|
||||||
return Err(Failed::fit(&msg[..]));
|
return Err(Failed::fit(&msg[..]));
|
||||||
}
|
}
|
||||||
@@ -182,7 +181,7 @@ impl OneHotEncoder {
|
|||||||
match oh_vec {
|
match oh_vec {
|
||||||
None => {
|
None => {
|
||||||
// Since we support T types, bad value in a series causes in to be invalid
|
// Since we support T types, bad value in a series causes in to be invalid
|
||||||
let msg = format!("At least one value in column {} doesn't conform to category definition", old_cidx);
|
let msg = format!("At least one value in column {old_cidx} doesn't conform to category definition");
|
||||||
return Err(Failed::transform(&msg[..]));
|
return Err(Failed::transform(&msg[..]));
|
||||||
}
|
}
|
||||||
Some(v) => {
|
Some(v) => {
|
||||||
@@ -338,11 +337,7 @@ mod tests {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
let params = OneHotEncoderParams::from_cat_idx(&[1]);
|
let params = OneHotEncoderParams::from_cat_idx(&[1]);
|
||||||
match OneHotEncoder::fit(&m, params) {
|
let result = OneHotEncoder::fit(&m, params);
|
||||||
Err(_) => {
|
assert!(result.is_err());
|
||||||
assert!(true);
|
|
||||||
}
|
|
||||||
_ => assert!(false),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -294,7 +294,7 @@ mod tests {
|
|||||||
&[0.5708488802, 0.1846414616, 0.9590802982, 0.5591871046],
|
&[0.5708488802, 0.1846414616, 0.9590802982, 0.5591871046],
|
||||||
&[0.8387612750, 0.5754861361, 0.5537109852, 0.1077646442],
|
&[0.8387612750, 0.5754861361, 0.5537109852, 0.1077646442],
|
||||||
]));
|
]));
|
||||||
println!("{}", transformed_values);
|
println!("{transformed_values}");
|
||||||
assert!(transformed_values.approximate_eq(
|
assert!(transformed_values.approximate_eq(
|
||||||
&DenseMatrix::from_2d_array(&[
|
&DenseMatrix::from_2d_array(&[
|
||||||
&[-1.1154020653, -0.4031985330, 0.9284605204, -0.4271473866],
|
&[-1.1154020653, -0.4031985330, 0.9284605204, -0.4271473866],
|
||||||
|
|||||||
@@ -206,7 +206,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn from_categories() {
|
fn from_categories() {
|
||||||
let fake_categories: Vec<usize> = vec![1, 2, 3, 4, 5, 3, 5, 3, 1, 2, 4];
|
let fake_categories: Vec<usize> = vec![1, 2, 3, 4, 5, 3, 5, 3, 1, 2, 4];
|
||||||
let it = fake_categories.iter().map(|&a| a);
|
let it = fake_categories.iter().copied();
|
||||||
let enc = CategoryMapper::<usize>::fit_to_iter(it);
|
let enc = CategoryMapper::<usize>::fit_to_iter(it);
|
||||||
let oh_vec: Vec<f64> = match enc.get_one_hot(&1) {
|
let oh_vec: Vec<f64> = match enc.get_one_hot(&1) {
|
||||||
None => panic!("Wrong categories"),
|
None => panic!("Wrong categories"),
|
||||||
@@ -218,8 +218,8 @@ mod tests {
|
|||||||
|
|
||||||
fn build_fake_str_enc<'a>() -> CategoryMapper<&'a str> {
|
fn build_fake_str_enc<'a>() -> CategoryMapper<&'a str> {
|
||||||
let fake_category_pos = vec!["background", "dog", "cat"];
|
let fake_category_pos = vec!["background", "dog", "cat"];
|
||||||
let enc = CategoryMapper::<&str>::from_positional_category_vec(fake_category_pos);
|
|
||||||
enc
|
CategoryMapper::<&str>::from_positional_category_vec(fake_category_pos)
|
||||||
}
|
}
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
all(target_arch = "wasm32", not(target_os = "wasi")),
|
all(target_arch = "wasm32", not(target_os = "wasi")),
|
||||||
@@ -275,7 +275,7 @@ mod tests {
|
|||||||
let lab = enc.invert_one_hot(res).unwrap();
|
let lab = enc.invert_one_hot(res).unwrap();
|
||||||
assert_eq!(lab, "dog");
|
assert_eq!(lab, "dog");
|
||||||
if let Err(e) = enc.invert_one_hot(vec![0.0, 0.0, 0.0]) {
|
if let Err(e) = enc.invert_one_hot(vec![0.0, 0.0, 0.0]) {
|
||||||
let pos_entries = format!("Expected a single positive entry, 0 entires found");
|
let pos_entries = "Expected a single positive entry, 0 entires found".to_string();
|
||||||
assert_eq!(e, Failed::transform(&pos_entries[..]));
|
assert_eq!(e, Failed::transform(&pos_entries[..]));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -167,7 +167,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Ensure that a string containing a csv row conforms to a specified row format.
|
/// Ensure that a string containing a csv row conforms to a specified row format.
|
||||||
fn validate_csv_row<'a>(row: &'a str, row_format: &CSVRowFormat<'_>) -> Result<(), ReadingError> {
|
fn validate_csv_row(row: &str, row_format: &CSVRowFormat<'_>) -> Result<(), ReadingError> {
|
||||||
let actual_number_of_fields = row.split(row_format.field_seperator).count();
|
let actual_number_of_fields = row.split(row_format.field_seperator).count();
|
||||||
if row_format.n_fields == actual_number_of_fields {
|
if row_format.n_fields == actual_number_of_fields {
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -208,7 +208,7 @@ where
|
|||||||
match value_string.parse::<T>().ok() {
|
match value_string.parse::<T>().ok() {
|
||||||
Some(value) => Ok(value),
|
Some(value) => Ok(value),
|
||||||
None => Err(ReadingError::InvalidField {
|
None => Err(ReadingError::InvalidField {
|
||||||
msg: format!("Value '{}' could not be read.", value_string,),
|
msg: format!("Value '{value_string}' could not be read.",),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-10
@@ -983,11 +983,7 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
let acc = accuracy(&y, &(y_hat.iter().map(|e| e.to_i32().unwrap()).collect()));
|
let acc = accuracy(&y, &(y_hat.iter().map(|e| e.to_i32().unwrap()).collect()));
|
||||||
|
|
||||||
assert!(
|
assert!(acc >= 0.9, "accuracy ({acc}) is not larger or equal to 0.9");
|
||||||
acc >= 0.9,
|
|
||||||
"accuracy ({}) is not larger or equal to 0.9",
|
|
||||||
acc
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
@@ -1076,11 +1072,7 @@ mod tests {
|
|||||||
|
|
||||||
let acc = accuracy(&y, &(y_hat.iter().map(|e| e.to_i32().unwrap()).collect()));
|
let acc = accuracy(&y, &(y_hat.iter().map(|e| e.to_i32().unwrap()).collect()));
|
||||||
|
|
||||||
assert!(
|
assert!(acc >= 0.9, "accuracy ({acc}) is not larger or equal to 0.9");
|
||||||
acc >= 0.9,
|
|
||||||
"accuracy ({}) is not larger or equal to 0.9",
|
|
||||||
acc
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
|
|||||||
+1
-1
@@ -662,7 +662,7 @@ mod tests {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let t = mean_squared_error(&y_hat, &y);
|
let t = mean_squared_error(&y_hat, &y);
|
||||||
println!("{:?}", t);
|
println!("{t:?}");
|
||||||
assert!(t < 2.5);
|
assert!(t < 2.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -560,8 +560,7 @@ impl<TX: Number + PartialOrd, TY: Number + Ord, X: Array2<TX>, Y: Array1<TY>>
|
|||||||
let k = classes.len();
|
let k = classes.len();
|
||||||
if k < 2 {
|
if k < 2 {
|
||||||
return Err(Failed::fit(&format!(
|
return Err(Failed::fit(&format!(
|
||||||
"Incorrect number of classes: {}. Should be >= 2.",
|
"Incorrect number of classes: {k}. Should be >= 2."
|
||||||
k
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -901,15 +900,13 @@ mod tests {
|
|||||||
)]
|
)]
|
||||||
#[test]
|
#[test]
|
||||||
fn gini_impurity() {
|
fn gini_impurity() {
|
||||||
|
assert!((impurity(&SplitCriterion::Gini, &[7, 3], 10) - 0.42).abs() < std::f64::EPSILON);
|
||||||
assert!(
|
assert!(
|
||||||
(impurity(&SplitCriterion::Gini, &vec![7, 3], 10) - 0.42).abs() < std::f64::EPSILON
|
(impurity(&SplitCriterion::Entropy, &[7, 3], 10) - 0.8812908992306927).abs()
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
(impurity(&SplitCriterion::Entropy, &vec![7, 3], 10) - 0.8812908992306927).abs()
|
|
||||||
< std::f64::EPSILON
|
< std::f64::EPSILON
|
||||||
);
|
);
|
||||||
assert!(
|
assert!(
|
||||||
(impurity(&SplitCriterion::ClassificationError, &vec![7, 3], 10) - 0.3).abs()
|
(impurity(&SplitCriterion::ClassificationError, &[7, 3], 10) - 0.3).abs()
|
||||||
< std::f64::EPSILON
|
< std::f64::EPSILON
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user