Merge potential next release v0.4 (#187) Breaking Changes
* First draft of the new n-dimensional arrays + NB use case * Improves default implementation of multiple Array methods * Refactors tree methods * Adds matrix decomposition routines * Adds matrix decomposition methods to ndarray and nalgebra bindings * Refactoring + linear regression now uses array2 * Ridge & Linear regression * LBFGS optimizer & logistic regression * LBFGS optimizer & logistic regression * Changes linear methods, metrics and model selection methods to new n-dimensional arrays * Switches KNN and clustering algorithms to new n-d array layer * Refactors distance metrics * Optimizes knn and clustering methods * Refactors metrics module * Switches decomposition methods to n-dimensional arrays * Linalg refactoring - cleanup rng merge (#172) * Remove legacy DenseMatrix and BaseMatrix implementation. Port the new Number, FloatNumber and Array implementation into module structure. * Exclude AUC metrics. Needs reimplementation * Improve developers walkthrough New traits system in place at `src/numbers` and `src/linalg` Co-authored-by: Lorenzo <tunedconsulting@gmail.com> * Provide SupervisedEstimator with a constructor to avoid explicit dynamical box allocation in 'cross_validate' and 'cross_validate_predict' as required by the use of 'dyn' as per Rust 2021 * Implement getters to use as_ref() in src/neighbors * Implement getters to use as_ref() in src/naive_bayes * Implement getters to use as_ref() in src/linear * Add Clone to src/naive_bayes * Change signature for cross_validate and other model_selection functions to abide to use of dyn in Rust 2021 * Implement ndarray-bindings. Remove FloatNumber from implementations * Drop nalgebra-bindings support (as decided in conf-call to go for ndarray) * Remove benches. Benches will have their own repo at smartcore-benches * Implement SVC * Implement SVC serialization. Move search parameters in dedicated module * Implement SVR. Definitely too slow * Fix compilation issues for wasm (#202) Co-authored-by: Luis Moreno <morenol@users.noreply.github.com> * Fix tests (#203) * Port linalg/traits/stats.rs * Improve methods naming * Improve Display for DenseMatrix Co-authored-by: Montana Low <montanalow@users.noreply.github.com> Co-authored-by: VolodymyrOrlov <volodymyr.orlov@gmail.com>
This commit is contained in:
+47
-24
@@ -7,11 +7,12 @@
|
||||
//! Example:
|
||||
//! ```
|
||||
//! use smartcore::metrics::auc::AUC;
|
||||
//! use smartcore::metrics::Metrics;
|
||||
//!
|
||||
//! let y_true: Vec<f64> = vec![0., 0., 1., 1.];
|
||||
//! let y_pred: Vec<f64> = vec![0.1, 0.4, 0.35, 0.8];
|
||||
//!
|
||||
//! let score1: f64 = AUC {}.get_score(&y_true, &y_pred);
|
||||
//! let score1: f64 = AUC::new().get_score(&y_true, &y_pred);
|
||||
//! ```
|
||||
//!
|
||||
//! ## References:
|
||||
@@ -20,32 +21,52 @@
|
||||
//! * ["The ROC-AUC and the Mann-Whitney U-test", Haupt, J.](https://johaupt.github.io/roc-auc/model%20evaluation/Area_under_ROC_curve.html)
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::algorithm::sort::quick_sort::QuickArgSort;
|
||||
use crate::linalg::BaseVector;
|
||||
use crate::math::num::RealNumber;
|
||||
use crate::linalg::basic::arrays::{Array1, ArrayView1, MutArrayView1};
|
||||
use crate::numbers::basenum::Number;
|
||||
|
||||
use crate::metrics::Metrics;
|
||||
|
||||
/// Area Under the Receiver Operating Characteristic Curve (ROC AUC)
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[derive(Debug)]
|
||||
pub struct AUC {}
|
||||
pub struct AUC<T> {
|
||||
_phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl AUC {
|
||||
impl<T: Number + Ord> Metrics<T> for AUC<T> {
|
||||
/// create a typed object to call AUC functions
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
fn new_with(_parameter: T) -> Self {
|
||||
Self {
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
/// AUC score.
|
||||
/// * `y_true` - cround truth (correct) labels.
|
||||
/// * `y_pred_probabilities` - probability estimates, as returned by a classifier.
|
||||
pub fn get_score<T: RealNumber, V: BaseVector<T>>(&self, y_true: &V, y_pred_prob: &V) -> T {
|
||||
/// * `y_true` - ground truth (correct) labels.
|
||||
/// * `y_pred_prob` - probability estimates, as returned by a classifier.
|
||||
fn get_score(
|
||||
&self,
|
||||
y_true: &dyn ArrayView1<T>,
|
||||
y_pred_prob: &dyn ArrayView1<T>,
|
||||
) -> f64 {
|
||||
let mut pos = T::zero();
|
||||
let mut neg = T::zero();
|
||||
|
||||
let n = y_true.len();
|
||||
let n = y_true.shape();
|
||||
|
||||
for i in 0..n {
|
||||
if y_true.get(i) == T::zero() {
|
||||
if y_true.get(i) == &T::zero() {
|
||||
neg += T::one();
|
||||
} else if y_true.get(i) == T::one() {
|
||||
} else if y_true.get(i) == &T::one() {
|
||||
pos += T::one();
|
||||
} else {
|
||||
panic!(
|
||||
@@ -55,21 +76,21 @@ impl AUC {
|
||||
}
|
||||
}
|
||||
|
||||
let mut y_pred = y_pred_prob.to_vec();
|
||||
let y_pred = y_pred_prob.clone();
|
||||
|
||||
let label_idx = y_pred.quick_argsort_mut();
|
||||
let label_idx = y_pred.argsort();
|
||||
|
||||
let mut rank = vec![T::zero(); n];
|
||||
let mut rank = vec![0f64; n];
|
||||
let mut i = 0;
|
||||
while i < n {
|
||||
if i == n - 1 || y_pred[i] != y_pred[i + 1] {
|
||||
rank[i] = T::from_usize(i + 1).unwrap();
|
||||
if i == n - 1 || y_pred.get(i) != y_pred.get(i + 1) {
|
||||
rank[i] = (i + 1) as f64;
|
||||
} else {
|
||||
let mut j = i + 1;
|
||||
while j < n && y_pred[j] == y_pred[i] {
|
||||
while j < n && y_pred.get(j) == y_pred.get(i) {
|
||||
j += 1;
|
||||
}
|
||||
let r = T::from_usize(i + 1 + j).unwrap() / T::two();
|
||||
let r = (i + 1 + j) as f64 / 2f64;
|
||||
for rank_k in rank.iter_mut().take(j).skip(i) {
|
||||
*rank_k = r;
|
||||
}
|
||||
@@ -78,14 +99,16 @@ impl AUC {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
let mut auc = T::zero();
|
||||
let mut auc = 0f64;
|
||||
for i in 0..n {
|
||||
if y_true.get(label_idx[i]) == T::one() {
|
||||
if y_true.get(label_idx[i]) == &T::one() {
|
||||
auc += rank[i];
|
||||
}
|
||||
}
|
||||
let pos = pos.to_f64().unwrap();
|
||||
let neg = neg.to_f64().unwrap();
|
||||
|
||||
(auc - (pos * (pos + T::one()) / T::two())) / (pos * neg)
|
||||
T::from(auc - (pos * (pos + 1f64) / 2.0)).unwrap() / T::from(pos * neg).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,8 +122,8 @@ mod tests {
|
||||
let y_true: Vec<f64> = vec![0., 0., 1., 1.];
|
||||
let y_pred: Vec<f64> = vec![0.1, 0.4, 0.35, 0.8];
|
||||
|
||||
let score1: f64 = AUC {}.get_score(&y_true, &y_pred);
|
||||
let score2: f64 = AUC {}.get_score(&y_true, &y_true);
|
||||
let score1: f64 = AUC::new().get_score(&y_true, &y_pred);
|
||||
let score2: f64 = AUC::new().get_score(&y_true, &y_true);
|
||||
|
||||
assert!((score1 - 0.75).abs() < 1e-8);
|
||||
assert!((score2 - 1.0).abs() < 1e-8);
|
||||
|
||||
Reference in New Issue
Block a user