Skip to content

Instantly share code, notes, and snippets.

@DiogoRibeiro7
Created September 28, 2023 10:16
Show Gist options
  • Save DiogoRibeiro7/ae0f86418f24f178c6b2961b0f2ea544 to your computer and use it in GitHub Desktop.
Save DiogoRibeiro7/ae0f86418f24f178c6b2961b0f2ea544 to your computer and use it in GitHub Desktop.
from scipy import stats
import numpy as np
def remove_outliers(data: List[float], threshold: float = 1.5) -> List[float]:
"""
Remove outliers from the data using the Z-score method.
Parameters:
- data (List[float]): The observed data
- threshold (float): The Z-score threshold for outlier detection
Returns:
- List[float]: Data with outliers removed
"""
z_scores = np.abs(stats.zscore(data))
return [x for x, z in zip(data, z_scores) if z < threshold]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment