You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python functions to calculate the mean, weighted mean, median, and weighted median.
Installation
The easiest way to install WeightedStats is to use pip:
$ pip install weightedstats
Usage
WeightedStats includes four functions (mean, weighted_mean, median, weighted_median) which accept lists as arguments, and two functions (numpy_weighted_mean, numpy weighted_median) which accept either lists or numpy arrays.
Example:
importweightedstatsaswsmy_data= [1, 2, 3, 4, 5]
my_weights= [10, 1, 1, 1, 9]
# Ordinary (unweighted) mean and medianws.mean(my_data) # equivalent to ws.weighted_mean(my_data)ws.median(my_data) # equivalent to ws.weighted_median(my_data)# Weighted mean and medianws.weighted_mean(my_data, weights=my_weights)
ws.weighted_median(my_data, weights=my_weights)
# Special weighted mean and median functions for use with numpy arraysws.numpy_weighted_mean(my_data, weights=my_weights)
ws.numpy_weighted_median(my_data, weights=my_weights)
Tests
Unit tests are in the test/ directory.
About
Calculate weighted mean, median, and weighted median.