Persistence of equadratures objects¶
equadratures objects might be desirable in many workflows, for example when saving a fitted
Poly to make future predictions, or for record keeping. Starting in v9.1.0,
equadratures objects can now be serialised with the
pickle python standard library.
To serialise, use
pickle.dumps(). For example, for a
# Fit a simple 1D polynomial import equadratures as eq f = lambda x: (1.0 - 3*x)**2 + 100.0 myparams = eq.Parameter(distribution='uniform', order=2, lower=-1.0, upper=1.0) mybasis = eq.Basis('tensor-grid') mypoly = eq.Poly(parameters=myparams, basis=mybasis, \ method='numerical-integration') mypoly.set_model(f) # Serialise import pickle serial = pickle.dumps(mypoly)
The serialised object can be unserialised with
mypoly = pickle.loads(serial) mypoly.plot_polyfit_1D(uncertainty=False)
To save and load to and from files,
loads can be replaced with
load. For example:
# Save to pickle file with open('data/mypoly.pickle', 'wb') as f: pickle.dump(mypoly, f)
# Load from file with open('data/mypoly.pickle','rb') as f: mypoly = pickle.load(f) mypoly
<equadratures.poly.Poly at 0x1486f5518>
For (potentially) better performance,
pickle.load can be replaced with the joblib versions. However,
loads versions are not avaiable in joblib.
Never unpickle files from untrusted sources. This could lead to malicious code being excuted when the file is loaded.
Interactive online version: