Given a fitted polynomial using the numpy Polynomial convenience class (>1.14)
p = np.polynomial.Polynomial.fit(...)
what is the best way of saving and loading this fit?
There are three arguments which are needed to reproduce the object, namely p.coef
, p.domain
and p.window
.
See this question for what happens when only the coefficients are loaded.
One could save all three as different files and re-load them, but that’s cumbersome and annoying.
My two initial ideas would be to save the entire object, either as a 0d-array
np.save("polyfit.npy", p, allow_pickle=True)
np.load("polyfit.npy", allow_pickle=True).item()
or as a 1d-array, as I honestly had to look up .item()
for accessing the element.
np.save("polyfit.npy", [p], allow_pickle=True)
np.load("polyfit.npy", allow_pickle=True)[0]
Are there any recommended ways of doing this?
The documentation doesn’t seem to contain any good hint
https://numpy.org/doc/stable/reference/routines.polynomials.html
https://numpy.org/doc/stable/reference/routines.polynomials.classes.html