Back to TILs.

Calmcode TIL

Python Statistics Module logoPython Statistics Module

Typically when you think "numeric stuff in Python" you'd immediately think of numpy. While numpy is certainly great, you don't always want it. Casting base Python lists to arrays and back can take a fair bit of overhead and numpy support on raspberry pi can also fluctuate.

Enter statistics

That's why it could be a neat idea to explore the python statistics module. You don't have to pip-install it because it's part of the core packages. It comes with support for the basic statistical functions that you'd expect, like mean, median, quantile and stdev, mode(), covariance and correlation. But there's also support for more advanced features like geometric_mean, harmonic_mean and qauntiles. Many of these methods support fancy numeric types like the built-in fraction-type or decimal-type.

There's even support for simple linear regression.

When to use it.

In general, it's safe to assume that numpy is faster. But if you're keen to keep docker containers super small, working on a raspberry, or if you're working with fraction or decimal types it can be good to know that some of these features are supported from base-python as well.


Back to main.