请问一下在机器学习PAI有没有Prophet训练 (ProphetTrainBatchOp)和Prophet预测 (ProphetPredictStreamOp)的python的代码示例?官网的示例不能正确运行。是docker环境,linux jupyter。
以下是使用 Prophet 进行时间序列预测的 Python 代码示例,可以试一下:
import pandas as pd
from fbprophet import Prophet
# 读取数据
data = pd.read_csv('data.csv')
# 转换日期列格式
data['ds'] = pd.to_datetime(data['ds'])
# 创建 Prophet 模型实例
model = Prophet()
# 训练模型
model.fit(data)
# 创建预测时间段
future = model.make_future_dataframe(periods=30)
# 进行预测
forecast = model.predict(future)
# 打印预测结果
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']])
PAI支持Prophet模型的训练和预测,您可以通过以下示例代码实现:
Prophet训练:
import os
import sys
import argparse
from fcntl import fcntl, F_GETFL, F_SETFL
from os import O_NONBLOCK, read
from ai_flow import init_ai_flow_context, PythonObjectExecutor
from ai_flow.graph.edge import JobControlEdge, JobSchedulingRule
from ai_flow.graph.node import JobNode
from ai_flow.graph.workflow import Workflow
from prophet import ProphetTrainBatchOp
from ai_flow_plugins.job_plugin.job_config import JobConfig
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--start', type=int, default=0)
args = parser.parse_args()
with open('/paddle/ai_flow/job_id', 'r') as f:
job_id = int(f.read())
init_ai_flow_context()
# set up prophet training job using ProphetTrainBatchOp
prophet_op = ProphetTrainBatchOp(name='prophet_train', dt_col='date',
target_col='target', freq='D',
prediction_length=7)
prophet_op.set_inputs({'data': '/pfs/data/bike_sharing_hourly.csv'})
prophet_op.set_outputs(['m', 'samples'])
prophet_node = JobNode(
name='prophet_train',
executor=PythonObjectExecutor(python_object=prophet_op),
job_config=JobConfig(job_id=job_id, job_name='prophet_train', properties={}),
common_job_parameters={'resource': {'cpu': 1, 'memory': '2Gi'}})
# set up workflow for prophet training
workflow = Workflow(workflow_name='prophet_workflow')
ctrl_edge = JobControlEdge(
name='prophet_train_edge',
upstream_node_id=None,
downstream_node_id=prophet_node.node_id,
action_config=JobSchedulingRule()
)
workflow.add_edge(ctrl_edge)
workflow.add_node(prophet_node)
# start the workflow
workflow.submit_workflow()
Prophet预测:
import os
import sys
import argparse
from fcntl import fcntl, F_GETFL, F_SETFL
from os import O_NONBLOCK, read
from ai_flow import init_ai_flow_context, PythonObjectExecutor
from ai_flow.graph.edge import JobControlEdge, JobSchedulingRule
from ai_flow.graph.node import JobNode
from ai_flow.graph.workflow import Workflow
from prophet import ProphetPredictStreamOp
from ai_flow_plugins.job_plugin.job_config import JobConfig
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--start', type=int, default=0)
args = parser.parse_args()
with open('/paddle/ai_flow/job_id', 'r') as f:
job_id = int(f.read())
init_ai_flow_context()
# set up prophet prediction job using ProphetPredictStreamOp
prophet_op = ProphetPredictStreamOp(name='prophet_predict',
freq='D', prediction_length=7)
prophet_op.set_inputs({'m': '/pfs/modelfile/prophet_m.pkl', 'samples': '/pfs/inputdata/samples'})
prophet_op.set_outputs({'output': '/pfs/outputfile/predictions'})
prophet_node = JobNode(
name='prophet_predict',
executor=PythonObjectExecutor(python_object=prophet_op),
job_config=JobConfig(job_id=job_id, job_name='prophet_predict', properties={}),
common_job_parameters={'resource': {'cpu': 1, 'memory': '2Gi'}})
# set up workflow for prophet prediction
workflow = Workflow(workflow_name='prophet_predict_workflow')
ctrl_edge = JobControlEdge(
name='prophet_predict_edge',
upstream_node_id=None,
downstream_node_id=prophet_node.node_id,
action_config=JobSchedulingRule()
)
workflow.add_edge(ctrl_edge)
workflow.add_node(prophet_node)
# start the workflow
workflow.submit_workflow()
需要注意的是,在代码示例中,您需要替换数据路径、模型路径和输出路径等相关参数,使其适配您的环境。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
人工智能平台 PAI(Platform for AI,原机器学习平台PAI)是面向开发者和企业的机器学习/深度学习工程平台,提供包含数据标注、模型构建、模型训练、模型部署、推理优化在内的AI开发全链路服务,内置140+种优化算法,具备丰富的行业场景插件,为用户提供低门槛、高性能的云原生AI工程化能力。