泡菜适合对象

Pickle fit-object

我写了一个class,其中有一些数据是合适的。由于 拟合 需要很长时间才能拟合大量数据,因此我想保存此 class 的拟合对象,因此我不必 repeat fitting 当我以后想使用拟合数据时。使用 pickle,调用对象的保存方法时出现以下错误:

AttributeError: Can't pickle local object 'ConstantModel.__init__.<locals>.constant'

我只有在 pickle 拟合数据时才会遇到这个问题,如果我在拟合之前保存对象,pickle 就可以工作。 有没有办法 pickle 拟合数据或者有一个很好的解决方法?

class pattern:
    def fitting(self):
        mod_total = lmfit.models.ConstantModel()
        pars_total = mod_total.guess(self.y, x=self.x)

        self.fit = mod_total.fit(self.y, pars_total, x=self.x)

    def save(self, path):
        with open(path, 'wb') as filehandler: 
            pickle.dump(self, filehandler)

我找到了这个问题的解决方案:使用 dill 而不是 pickle 可以达到 (如我所愿)。