Why??
Numpy does not include a built-in moving average function as of yet. Most solutions are tedious and complicated and not one liners. This operates similar to the Wolfram Language’s MovingAverage[] function, but has the advantage that it can specify axis for higher ndim arrays.
Usage
See docs for examples. But essentially, the usage is just:
from mvgavg import mvgavg mvgavg(array, n, axis=0, weights = [list of weights]) mvgavg(array, n, axis=0, binning = bool)
What This Code Does
Consider the array [a,b,c,d, … x, y, z]. Taking the moving average of length n=3 results in a new array wherein the first element is (a+b+c)/3, the second (b+c+d)/3 and so on. This can be done for arrays of arbitrary depth, so if you put in a time series [[t1,x1],[t2,x2],…], a moving average of along axis=0 (the default) will give you back the moving average of all t’s along side all x’s.
The output is a vector that is the same size and shape but has been shortened on the axis axis by a length of n-1, unless binning=True (see below).
The structure of this code is that it runs a cumulative sum moving average if there is no weights or binning, as this is the fastest moving average typically, especially for wide or high ndim arrays. On the other hand, if either weights or binning is used, the appropriate function is selected.
Axis and Weight Options
Axis: The axis lets you operate at an increased depth, so using the axis=1 parameter, you can operate horizontally across columns with your moving average. You can do this as deep as the array itself.
Binning
Binning greatly shortens the array and loses some precision. This is desirable if you have an enormous amount of data and don’t need to preserve every point. The difference between a default moving average and a binned moving average is that, for an input array [a b c d e f…] and an output [A B C D E F…] over distance 3, the default moving average looks like this:
A = [a b c]/3 B = [b c d]/3 C = [c d e]/3 D = [d e f]/3 E = [e f g]/3 ....
but if binning = True:
A = [a b c]/3 B = [d e f]/3 C = [g h i]/3 .....
As you can see the output array is greatly shortened. As arrays get very large, binning can become orders of magnitude faster to compute, and if you don’t need the resolution of a moving average, are a much more efficient way of handling data, because you may end up throwing most of your data away later when you go to plot it up anyways.
Why Is This Function Important?
Moving averages smooth data and illuminate trends that otherwise may not be as apparent. They also help with reverse interpolation when different x’s yield the same y. The reasons for using moving averages are myriad, so a decent arbitrary-depth moving average function with numpy-speed and arbitrary weighting needed to be written.
Acknowledgements
Credit to @fnjn on github for the sliding window function.
Misc
If you have issues or questions, open an issue on Github at https://github.com/NGeorgescu/python-moving-average or if you think there’s some functionality that you would like to see, or if you have a faster algorithm
Thanks and Enjoy!
Metadata
Release files for mvgavg 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mvgavg-0.1.0.tar.gz | 4.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mvgavg-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.1 kB
Release files / mvgavg-0.1.0.tar.gz
| Download URL | mvgavg-0.1.0.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
052aecc619dd9ad6a52687f1e107add535c881bd65b85b8f913a9635bbf0ae58
|
|
BLAKE2b-256 checksum How to use checksums |
00c646e10c392b8014c06b97f7bf4995aa3dcb5e31c3121ffd050df44f26f3be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4
|
Release files / mvgavg-0.1.0-py3-none-any.whl
| Download URL | mvgavg-0.1.0-py3-none-any.whl |
|---|---|
| Size | 17.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3ae8c91a51734d4ac2aedeaebab8993626b76ff682550f2b71f212bb6a8bd2bd
|
|
BLAKE2b-256 checksum How to use checksums |
a91ac45ab481f1cbc25d785910d1d82f46a2def3c00adb34f9c24cdadac272ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4
|
Security for mvgavg 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.