如何腌制或保存scipy kde供以后使用?
import scipy.stats as scs
from sklearn.externals import joblib
kde = scs.gaussian_kde(data, bw_method=.15)
joblib.dump(kde, 'test.pkl')
我在上面尝试过并收到此错误:
PicklingError: Can't pickle <function gaussian_kde.set_bandwidth.<locals>.<lambda> at 0x1a5b6fb7b8>: it's not found as scipy.stats.kde.gaussian_kde.set_bandwidth.<locals>.<lambda>
最佳答案
看来joblib在set_bandwith
方法上遇到了麻烦,我的猜测是因为方法中的lambda
函数-pickling lambdas has been discussed here。
with open('test.pkl', 'wb') as fo:
joblib.dump(lambda x,y: x+y, fo)
PicklingError: Can't pickle <function <lambda> at 0x7ff89495d598>: it's not found as __main__.<lambda>
据我所知,cloudpickle和dill都可以工作:
import cloudpickle
import dill
with open('test.cp.pkl', 'wb') as f:
cloudpickle.dump(kde, f)
with open('test.dill.pkl', 'wb') as f:
dill.dump(kde, f)
with open('test.cp.pkl', 'rb') as f:
kde_cp = cloudpickle.load(f)
with open('test.dill.pkl', 'rb') as f:
kde_dill = dill.load(f)
检查一些数据:
import numpy as np
print(np.array_equal(kde.dataset, kde_cp.dataset))
True
print(np.array_equal(kde.dataset, kde_dill.dataset))
True
print(np.array_equal(kde_cp.dataset, kde_dill.dataset))
True
kde.pdf(10) == kde_cp.pdf(10) == kde_dill.pdf(10)
array([ True])
关于python - 保存(点刺)Scipy KDE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53638328/