Skip to content

Instantly share code, notes, and snippets.

@jwilber
Last active October 19, 2020 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwilber/a431f6536faa42f9b5b2d12e068515a9 to your computer and use it in GitHub Desktop.
Save jwilber/a431f6536faa42f9b5b2d12e068515a9 to your computer and use it in GitHub Desktop.
// calculate MSE for two equal-dimension arrays
const meanSquaredError = (actual, pred) => {
// helper funcs
const sqr = a => a * a;
const add = (a, b) => a + b;
const difference = (a, b) => a.map((x, i) => x - b[i]);
// calculate MSE
const squaredError = difference(actual, pred).map(sqr);
const sumSquaredError = squaredError.reduce(add, 0);
const mse = sumSquaredError / actual.length;
return mse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment