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 asnumpy.polyfit
andnumpy.poly
, are considered legacy and should not be used in new code. Since NumPy version 1.4, thenumpy.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