Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created September 2, 2023 03:18
Show Gist options
  • Save miguelmota/12d9ace828f7302011caf995740c4017 to your computer and use it in GitHub Desktop.
Save miguelmota/12d9ace828f7302011caf995740c4017 to your computer and use it in GitHub Desktop.
JavaScript remove outliers by z score
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