Skip to main content

Robust statistics for NumPy: replaces mean with median for outlier-resistant analysis

Project description

robustats

Robust statistics for NumPy — replaces mean with median for outlier-resistant data analysis.

PyPI Python License: MIT

Why?

NumPy's np.mean is sensitive to outliers. One extreme value can distort your entire analysis. robustats swaps mean with median by default — a far more robust estimator — while keeping the same familiar API.

data = [1, 2, 3, 4, 1000]

np.mean(data)      # 202.0  ← distorted by outlier
rs.mean(data)      #   3.0  ← robust, ignores outlier

Installation

pip install robustats

Quick Start

import numpy as np
import robustats as rs

data = [1, 2, 3, 4, 1000]

# Drop-in replacements
rs.mean(data)               # 3.0    (median, robust)
rs.nanmean(data)            # 3.0    (nanmedian, robust)
rs.std(data)                # 1.4826 (MAD-based, robust)
rs.var(data)                # 2.198  (MAD², robust)
rs.mad(data)                # 1.4826 (Median Absolute Deviation)

# Use arithmetic mean explicitly
rs.mean(data, use_median=False)    # 202.0

# Weighted median
rs.average(data, weights=[1,1,1,1,10])   # 5.0

Context Manager — patch np.mean temporarily

with rs.robust_mode():
    np.mean(data)     # 3.0 inside the block
np.mean(data)         # 202.0 restored after the block

Global Patch — replace np.mean everywhere

rs.patch_numpy()
np.mean(data)     # 3.0  (global replacement)

rs.unpatch_numpy()
np.mean(data)     # 202.0 (restored)

API Reference

Function Description
rs.mean(a, axis, use_median=True) Median (or mean) of array
rs.nanmean(a, axis, use_median=True) Nanmedian (or nanmean)
rs.std(a, axis, use_mad=True) MAD-based std (or numpy std)
rs.var(a, axis, use_mad=True) MAD²-based var (or numpy var)
rs.mad(a, axis, scale=1.4826) Median Absolute Deviation
rs.average(a, weights, use_median=True) Weighted median (or average)
rs.robust_mode() Context manager patching np.mean
rs.patch_numpy() Globally replace np.mean / np.nanmean
rs.unpatch_numpy() Restore originals

When to use robustats

  • Your data may have outliers or measurement errors
  • You're doing exploratory data analysis on real-world datasets
  • You need a quick drop-in without changing all your np.mean calls (robust_mode context)
  • You want robust standard deviation via MAD instead of classical std

License

MIT

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

robustats_py-0.2.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

robustats_py-0.2.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file robustats_py-0.2.0.tar.gz.

File metadata

  • Download URL: robustats_py-0.2.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for robustats_py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 28826f3242db875d95ae71c980e5887a4b36dac9b33ada81ea61411f470da535
MD5 c6d7f80d8f7d62210ad6c041ab33b519
BLAKE2b-256 257baa393e2c5be1e9572c85022bc1603a57e37bd93be2d05221b5cd863ce7dc

See more details on using hashes here.

File details

Details for the file robustats_py-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: robustats_py-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for robustats_py-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c14ae24e0dd08e30ed33c4751d46b734b2d82b393e49ddeaafc26b0faa1778f
MD5 dc415b111bb58178ccfa1d60bd19b9f9
BLAKE2b-256 dad4eb642c92384f151af7bd5a2dd02dcc0ed3eff3f41022fd16c78c26255e2f

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