feat: adds accuracy, recall and precision metrics

This commit is contained in:
Volodymyr Orlov
2020-06-05 17:39:29 -07:00
parent e20e9ca6e0
commit c0c2029f2c
10 changed files with 285 additions and 8 deletions
+45
View File
@@ -0,0 +1,45 @@
use serde::{Serialize, Deserialize};
use crate::math::num::FloatExt;
use crate::linalg::BaseVector;
#[derive(Serialize, Deserialize, Debug)]
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());
}
let n = y_true.len();
let mut positive = 0;
for i in 0..n {
if y_true.get(i) == y_prod.get(i) {
positive += 1;
}
}
T::from_i64(positive).unwrap() / T::from_usize(n).unwrap()
}
}
#[cfg(test)]
mod tests {
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);
assert!((score1 - 0.5).abs() < 1e-8);
assert!((score2 - 1.0).abs() < 1e-8);
}
}