Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/metrics_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ pub trait SingleTargetRegression<F: Float, T: AsSingleTargets<Elem = F>>:
let mean_error = diff.mean().ok_or(Error::NotEnoughSamples)?;

Ok(F::one()
- (diff.mapv_into(|x| x * x).sum() - mean_error)
- diff
.mapv_into(|x| (x - mean_error) * (x - mean_error))
.sum()
/ (single_target_compare_to
.mapv(|x| (x - mean) * (x - mean))
.sum()
Expand Down Expand Up @@ -474,7 +476,10 @@ mod tests {
let abs_err_from_arr1 = prediction.explained_variance(st_dataset.targets()).unwrap();
let prediction: DatasetBase<_, _> = (records.view(), prediction).into();
let abs_err_from_ds = prediction.explained_variance(&st_dataset).unwrap();
assert_abs_diff_eq!(abs_err_from_arr1, 0.8, epsilon = 1e-5);
// Explained variance is 1 - Var(y_true - y_pred) / Var(y_true), matching
// sklearn.metrics.explained_variance_score([0.0,0.1,0.2,0.3,0.4],
// [0.1,0.3,0.2,0.5,0.7]) == 0.48
assert_abs_diff_eq!(abs_err_from_arr1, 0.48, epsilon = 1e-5);
assert_abs_diff_eq!(abs_err_from_arr1, abs_err_from_ds);
}

Expand Down
Loading