Skip to main content

Python implementation of the Hampel Filter

Project description

Theory

The Hampel filter is generally used to detect anomalies in data with a timeseries structure. It basically consists of a sliding window of a parameterizable size. For each window, each observation will be compared with the Median Absolute Deviation (MAD). The observation will be considered an outlier in the case in which it exceeds the MAD by n times (the parameter n is also parameterizable). For more details, see the following Medium post as well as the Wikipedia entry on MAD.

https://medium.com/wwblog/clean-up-your-time-series-data-with-a-hampel-filter-58b0bb3ebb04

https://en.wikipedia.org/wiki/Median_absolute_deviation

Install Package

To install the package execute the following command.

pip install hampel

Usage

hampel(ts, window_size=5, n=3)
hampel(ts, window_size=5, n=3, imputation=True)

Arguments

  • ts - A pandas Series object representing the timeseries
  • window_size - Total window size will be computed as 2*window_size + 1
  • n - Threshold, default is 3 (Pearson's rule)
  • imputation - If set to False, then the algorithm will be used for outlier detection. If set to True, then the algorithm will also imput the outliers with the rolling median.

Code Example

import matplotlib.pyplot as plt
import pandas as pd
from hampel import hampel

ts = pd.Series([1, 2, 1 , 1 , 1, 2, 13, 2, 1, 2, 15, 1, 2])

# Just outlier detection
outlier_indices = hampel(ts, window_size=5, n=3)
print("Outlier Indices: ", outlier_indices)

# Outlier Imputation with rolling median
ts_imputation = hampel(ts, window_size=5, n=3, imputation=True)

ts.plot(style="k-")
ts_imputation.plot(style="g-")
plt.show()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hampel-0.0.5.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hampel-0.0.5-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file hampel-0.0.5.tar.gz.

File metadata

  • Download URL: hampel-0.0.5.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.2

File hashes

Hashes for hampel-0.0.5.tar.gz
Algorithm Hash digest
SHA256 c9baa3ea022c76d5cfc1ad8959de8df6c4a232ecdf0dffd599a03609aeae5fdb
MD5 9a91c229fc274d5916c7ab38576006fc
BLAKE2b-256 500ca9c77ff9b7f3f907940f520274a3946ff66587f823e41b57c29ac33ed81d

See more details on using hashes here.

File details

Details for the file hampel-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: hampel-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.2

File hashes

Hashes for hampel-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3852db2a0882beaaafba8545576c0b15eeeaf604efb59245c9bd3ca6d24e902d
MD5 577f5ffd167ed31aff9c25e373575c90
BLAKE2b-256 e294a08d5ca9820277ea395b712975778dd20b720597f1c0109d7d2325d86a02

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page