【问题标题】:How to save a fbprophet forecast model using pickle such that the model takes user input when loaded如何使用 pickle 保存 fbprophet 预测模型,以便模型在加载时接受用户输入
【发布时间】:2022-08-06 01:16:00
【问题描述】:

我已经构建了一个 fbProphet 模型,其中包含日期和项目作为列。有 50 个这样的项目列。我需要以 pickle 格式保存这个模型。每次我加载模型时,它都应该将 \'item_number\' 作为用户输入,然后预测未来的日期。如何保存模型以便在加载时接受用户输入。下面是模型拟合的代码。

def model_fit(item_number):
  #Calling the dataframe for specific item
  item_data = data(item_number) #Function call - data(item_number)
  train, test = item_data[item_data[\'ds\'] <= \'2016-12-31\'], item_data[item_data[\'ds\'] > \'2016-12-31\']
  model = Prophet(interval_width = 0.80, changepoint_range = 0.9)
  #model = Prophet(changepoint_range=0.9)
  model.fit(train)

  return model

在上面的代码中,当我运行model = model_fit(item_number) 时,它适合该特定项目的训练数据模型。如果我将它保存到泡菜中,它会单独保存适合该 item_number 的模型。如何保存模型,以便在加载模型时将 \'item_number\' 作为输入。

    标签: python machine-learning time-series pickle facebook-prophet


    【解决方案1】:

    可以保存已安装的 Prophet 模型,以便以后加载和使用。

    在 Python 中,模型不应该用 pickle 保存;附加到模型对象的 Stan 后端不会很好地腌制,并且会在某些 Python 版本下产生问题。相反,您应该使用内置的序列化函数将模型序列化为 json:

    Python

    从先知。序列化导入模型_to_json,模型_from_json

    使用 open('serialized_model.json', 'w') 作为 fout: fout.write(model_to_json(m)) # 保存模型

    使用 open('serialized_model.json', 'r') 作为 fin: m = model_from_json(fin.read()) # 加载模型

    来源:https://facebook.github.io/prophet/docs/additional_topics.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-17
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2022-10-14
      • 2020-12-18
      相关资源
      最近更新 更多