Skip to content Skip to sidebar Skip to footer

Dynamic Outlier Detection Using Window In Pandas

I want to implement outlier detection which will use a window to check whether the next element is an outlier or not. Let's say we use a window of length 3 on pd.Series like this:

Solution 1:

Say you start with

s = pd.Series([1, 2, 1, 4, 2000, 2])

Then using rolling, the following will show you that the 5th element is 200 away from a length-3 window median:

(s - s.rolling(3).median()).abs() > 2000False1False2False3False4True5False
dtype: bool

It is vectorized, and therefore should be much faster than a for loop.

Post a Comment for "Dynamic Outlier Detection Using Window In Pandas"