Skip to content

Instantly share code, notes, and snippets.

@stefanschmidt
Created May 15, 2023 17:58
Show Gist options
  • Save stefanschmidt/90bb9ebaa00e21847aca966aa9bcecfa to your computer and use it in GitHub Desktop.
Save stefanschmidt/90bb9ebaa00e21847aca966aa9bcecfa to your computer and use it in GitHub Desktop.
Simple linear regression in NumPy

Simple linear regression in NumPy

The verbosity of performing a simple linear regression in the current stable release has increased by an astonishing amount.1

In the documentation it is recommended to not use numpy.polyfit.

As noted above, the poly1d class and associated functions defined in numpy.lib.polynomial, such as numpy.polyfit and numpy.poly, are considered legacy and should not be used in new code. Since NumPy version 1.4, the numpy.polynomial package is preferred for working with polynomials.

For polynomial regression, the the documentation recommends to replace numpy.polyfit with numpy.polynomial.polynomial.Polynomial.fit.

In the prior API (now considered "legacy") running a simple linear regression was as simple as

m, b = numpy.polyfit(x, y, 1)

In the new and shiny API if following the recommendation – after several iterations of trial and error – this turns into:

b, m = numpy.polynomial.polynomial.Polynomial.fit(x, y, 1).convert().coef

So for the trivial task of a simple linear regression instead of writing mere 22 characters of code one is now expected to write 66 characters, so exactly three times the amount of code.1

Igoring the official recommendation this can be brought down a bit, but still increases the amount of code by a factor of 1.5.

b, m = numpy.polynomial.polynomial.polyfit(x, y, 1)

I am certainly not the first to have noticed this.

<Insert smart and humourous concluding remark here>


1. As of writing this is NumPy 1.24
2. Yes, I am very well aware that for multiple usages this can of course be drastically abbreviated with a from ... import ... as ... statement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment