Created
September 2, 2023 03:18
-
-
Save miguelmota/12d9ace828f7302011caf995740c4017 to your computer and use it in GitHub Desktop.
JavaScript remove outliers by z score
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeOutliersByZScore(data: number[], threshold = 2) { | |
const mean = data.reduce((acc, val) => acc + val, 0) / data.length | |
const stdDev = Math.sqrt(data.reduce((acc, val) => acc + Math.pow(val - mean, 2), 0) / data.length) | |
return data.filter((val) => Math.abs((val - mean) / stdDev) < threshold) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment