fix: cargo fmt

This commit is contained in:
Volodymyr Orlov
2020-06-05 17:52:03 -07:00
parent 685be04488
commit a2784d6345
52 changed files with 3342 additions and 2829 deletions
+14 -12
View File
@@ -1,15 +1,19 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use crate::math::num::FloatExt;
use crate::linalg::BaseVector;
use crate::math::num::FloatExt;
#[derive(Serialize, Deserialize, Debug)]
pub struct Accuracy{}
pub struct Accuracy {}
impl Accuracy {
pub fn get_score<T: FloatExt, V: BaseVector<T>>(&self, y_true: &V, y_prod: &V) -> T {
if y_true.len() != y_prod.len() {
panic!("The vector sizes don't match: {} != {}", y_true.len(), y_prod.len());
panic!(
"The vector sizes don't match: {} != {}",
y_true.len(),
y_prod.len()
);
}
let n = y_true.len();
@@ -23,23 +27,21 @@ impl Accuracy {
T::from_i64(positive).unwrap() / T::from_usize(n).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::*;
#[test]
fn accuracy() {
let y_pred: Vec<f64> = vec![0., 2., 1., 3.];
let y_true: Vec<f64> = vec![0., 1., 2., 3.];
let score1: f64 = Accuracy{}.get_score(&y_pred, &y_true);
let score2: f64 = Accuracy{}.get_score(&y_true, &y_true);
let score1: f64 = Accuracy {}.get_score(&y_pred, &y_true);
let score2: f64 = Accuracy {}.get_score(&y_true, &y_true);
assert!((score1 - 0.5).abs() < 1e-8);
assert!((score2 - 1.0).abs() < 1e-8);
}
}
}
}
+11 -11
View File
@@ -1,34 +1,34 @@
pub mod accuracy;
pub mod recall;
pub mod precision;
pub mod recall;
use crate::math::num::FloatExt;
use crate::linalg::BaseVector;
use crate::math::num::FloatExt;
pub struct ClassificationMetrics{}
pub struct ClassificationMetrics {}
impl ClassificationMetrics {
pub fn accuracy() -> accuracy::Accuracy{
pub fn accuracy() -> accuracy::Accuracy {
accuracy::Accuracy {}
}
pub fn recall() -> recall::Recall{
pub fn recall() -> recall::Recall {
recall::Recall {}
}
pub fn precision() -> precision::Precision{
pub fn precision() -> precision::Precision {
precision::Precision {}
}
}
pub fn accuracy<T: FloatExt, V: BaseVector<T>>(y_true: &V, y_prod: &V) -> T{
pub fn accuracy<T: FloatExt, V: BaseVector<T>>(y_true: &V, y_prod: &V) -> T {
ClassificationMetrics::accuracy().get_score(y_true, y_prod)
}
pub fn recall<T: FloatExt, V: BaseVector<T>>(y_true: &V, y_prod: &V) -> T{
pub fn recall<T: FloatExt, V: BaseVector<T>>(y_true: &V, y_prod: &V) -> T {
ClassificationMetrics::recall().get_score(y_true, y_prod)
}
pub fn precision<T: FloatExt, V: BaseVector<T>>(y_true: &V, y_prod: &V) -> T{
pub fn precision<T: FloatExt, V: BaseVector<T>>(y_true: &V, y_prod: &V) -> T {
ClassificationMetrics::precision().get_score(y_true, y_prod)
}
}
+25 -17
View File
@@ -1,15 +1,19 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use crate::math::num::FloatExt;
use crate::linalg::BaseVector;
use crate::math::num::FloatExt;
#[derive(Serialize, Deserialize, Debug)]
pub struct Precision{}
pub struct Precision {}
impl Precision {
pub fn get_score<T: FloatExt, V: BaseVector<T>>(&self, y_true: &V, y_prod: &V) -> T {
if y_true.len() != y_prod.len() {
panic!("The vector sizes don't match: {} != {}", y_true.len(), y_prod.len());
panic!(
"The vector sizes don't match: {} != {}",
y_true.len(),
y_prod.len()
);
}
let mut tp = 0;
@@ -17,11 +21,17 @@ impl Precision {
let n = y_true.len();
for i in 0..n {
if y_true.get(i) != T::zero() && y_true.get(i) != T::one() {
panic!("Precision can only be applied to binary classification: {}", y_true.get(i));
panic!(
"Precision can only be applied to binary classification: {}",
y_true.get(i)
);
}
if y_prod.get(i) != T::zero() && y_prod.get(i) != T::one() {
panic!("Precision can only be applied to binary classification: {}", y_prod.get(i));
panic!(
"Precision can only be applied to binary classification: {}",
y_prod.get(i)
);
}
if y_prod.get(i) == T::one() {
@@ -31,27 +41,25 @@ impl Precision {
tp += 1;
}
}
}
}
T::from_i64(tp).unwrap() / T::from_i64(p).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::*;
#[test]
fn precision() {
let y_true: Vec<f64> = vec![0., 1., 1., 0.];
let y_pred: Vec<f64> = vec![0., 0., 1., 1.];
let score1: f64 = Precision{}.get_score(&y_pred, &y_true);
let score2: f64 = Precision{}.get_score(&y_pred, &y_pred);
let y_pred: Vec<f64> = vec![0., 0., 1., 1.];
assert!((score1 - 0.5).abs() < 1e-8);
let score1: f64 = Precision {}.get_score(&y_pred, &y_true);
let score2: f64 = Precision {}.get_score(&y_pred, &y_pred);
assert!((score1 - 0.5).abs() < 1e-8);
assert!((score2 - 1.0).abs() < 1e-8);
}
}
}
}
+25 -17
View File
@@ -1,15 +1,19 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use crate::math::num::FloatExt;
use crate::linalg::BaseVector;
use crate::math::num::FloatExt;
#[derive(Serialize, Deserialize, Debug)]
pub struct Recall{}
pub struct Recall {}
impl Recall {
pub fn get_score<T: FloatExt, V: BaseVector<T>>(&self, y_true: &V, y_prod: &V) -> T {
if y_true.len() != y_prod.len() {
panic!("The vector sizes don't match: {} != {}", y_true.len(), y_prod.len());
panic!(
"The vector sizes don't match: {} != {}",
y_true.len(),
y_prod.len()
);
}
let mut tp = 0;
@@ -17,11 +21,17 @@ impl Recall {
let n = y_true.len();
for i in 0..n {
if y_true.get(i) != T::zero() && y_true.get(i) != T::one() {
panic!("Recall can only be applied to binary classification: {}", y_true.get(i));
panic!(
"Recall can only be applied to binary classification: {}",
y_true.get(i)
);
}
if y_prod.get(i) != T::zero() && y_prod.get(i) != T::one() {
panic!("Recall can only be applied to binary classification: {}", y_prod.get(i));
panic!(
"Recall can only be applied to binary classification: {}",
y_prod.get(i)
);
}
if y_true.get(i) == T::one() {
@@ -31,27 +41,25 @@ impl Recall {
tp += 1;
}
}
}
}
T::from_i64(tp).unwrap() / T::from_i64(p).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::*;
#[test]
fn recall() {
let y_true: Vec<f64> = vec![0., 1., 1., 0.];
let y_pred: Vec<f64> = vec![0., 0., 1., 1.];
let score1: f64 = Recall{}.get_score(&y_pred, &y_true);
let score2: f64 = Recall{}.get_score(&y_pred, &y_pred);
let y_pred: Vec<f64> = vec![0., 0., 1., 1.];
assert!((score1 - 0.5).abs() < 1e-8);
let score1: f64 = Recall {}.get_score(&y_pred, &y_true);
let score2: f64 = Recall {}.get_score(&y_pred, &y_pred);
assert!((score1 - 0.5).abs() < 1e-8);
assert!((score2 - 1.0).abs() < 1e-8);
}
}
}
}