Persistence of 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 Poly object:
# 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, \
serial = pickle.dumps(mypoly)
The serialised object can be unserialised with pickle.loads():
mypoly = pickle.loads(serial)
To save and load to and from files, dumps and loads can be replaced with dump and load. For example:
# Save to pickle file
with open('data/mypoly.pickle', 'wb') as f:
# Load from file
with open('data/mypoly.pickle','rb') as f:
mypoly = pickle.load(f)
<equadratures.poly.Poly at 0x1486f5518>
For (potentially) better performance, pickle.dump and pickle.load can be replaced with the joblib versions. However, dumps and 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.