Skip to main content

r_squared

Function r_squared 

Source
pub fn r_squared(y_pred: &Vector<f32>, y_true: &Vector<f32>) -> f32
Expand description

Computes the coefficient of determination (R²).

R² = 1 - (SS_res / SS_tot)

where SS_res is the residual sum of squares and SS_tot is the total sum of squares.

§Examples

use aprender::metrics::r_squared;
use aprender::primitives::Vector;

let y_true = Vector::from_slice(&[3.0, -0.5, 2.0, 7.0]);
let y_pred = Vector::from_slice(&[2.5, 0.0, 2.0, 8.0]);
let r2 = r_squared(&y_pred, &y_true);
assert!(r2 > 0.9);

§Panics

Panics if vectors have different lengths.