Robust ZScore Normalization Use robust statistics for Z-Score normalization: mean(x) = median(x) std(x) = MAD(x) * 1.4826 Reference: https://en.wikipedia.org/wiki/Median_absolute_deviation.
(x: pd.Series, zscore=False)
| 13 | |
| 14 | |
| 15 | def robust_zscore(x: pd.Series, zscore=False): |
| 16 | """Robust ZScore Normalization |
| 17 | |
| 18 | Use robust statistics for Z-Score normalization: |
| 19 | mean(x) = median(x) |
| 20 | std(x) = MAD(x) * 1.4826 |
| 21 | |
| 22 | Reference: |
| 23 | https://en.wikipedia.org/wiki/Median_absolute_deviation. |
| 24 | """ |
| 25 | x = x - x.median() |
| 26 | mad = x.abs().median() |
| 27 | x = np.clip(x / mad / 1.4826, -3, 3) |
| 28 | if zscore: |
| 29 | x -= x.mean() |
| 30 | x /= x.std() |
| 31 | return x |
| 32 | |
| 33 | |
| 34 | def zscore(x: Union[pd.Series, pd.DataFrame]): |