mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
Added Mean Square Error (#417)
* Added Mean Square Error * Update MeanSquareError.js Co-authored-by: vinayak <itssvinayak@gmail.com>
This commit is contained in:
26
Maths/MeanSquareError.js
Normal file
26
Maths/MeanSquareError.js
Normal file
@ -0,0 +1,26 @@
|
||||
// Wikipedia: https://en.wikipedia.org/wiki/Mean_squared_error
|
||||
|
||||
const meanSquaredError = (predicted, expected) => {
|
||||
if (!Array.isArray(predicted) || !Array.isArray(expected)) {
|
||||
throw new TypeError('Argument must be an Array')
|
||||
}
|
||||
|
||||
if (predicted.length !== expected.length) {
|
||||
throw new TypeError('The two lists must be of equal length')
|
||||
}
|
||||
|
||||
let err = 0
|
||||
|
||||
for (let i = 0; i < expected.length; i++) {
|
||||
err += (expected[i] - predicted[i]) ** 2
|
||||
}
|
||||
|
||||
return err / expected.length
|
||||
}
|
||||
|
||||
// testing
|
||||
(() => {
|
||||
console.log(meanSquaredError([1, 2, 3, 4], [1, 2, 3, 4]) === 0)
|
||||
console.log(meanSquaredError([4, 3, 2, 1], [1, 2, 3, 4]) === 5)
|
||||
console.log(meanSquaredError([2, 0, 2, 0], [0, 0, 0, 0]) === 3)
|
||||
})()
|
Reference in New Issue
Block a user