开发者社区 > 大数据与机器学习 > 人工智能平台PAI > 正文

请问一下在机器学习PAI有没有Prophet训练 和Prophet预测的python的代码示例?

请问一下在机器学习PAI有没有Prophet训练 (ProphetTrainBatchOp)和Prophet预测 (ProphetPredictStreamOp)的python的代码示例?官网的示例不能正确运行。是docker环境,linux jupyter。

展开
收起
三分钟热度的鱼 2023-05-29 11:45:24 153 0
2 条回答
写回答
取消 提交回答
  • 以下是使用 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']])
    
    
    
    2023-05-29 13:49:23
    赞同 展开评论 打赏
  • 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()
    

    需要注意的是,在代码示例中,您需要替换数据路径、模型路径和输出路径等相关参数,使其适配您的环境。

    2023-05-29 13:44:08
    赞同 展开评论 打赏

人工智能平台 PAI(Platform for AI,原机器学习平台PAI)是面向开发者和企业的机器学习/深度学习工程平台,提供包含数据标注、模型构建、模型训练、模型部署、推理优化在内的AI开发全链路服务,内置140+种优化算法,具备丰富的行业场景插件,为用户提供低门槛、高性能的云原生AI工程化能力。

相关产品

  • 人工智能平台 PAI
  • 热门讨论

    热门文章

    相关电子书

    更多
    微博机器学习平台架构和实践 立即下载
    机器学习及人机交互实战 立即下载
    大数据与机器学习支撑的个性化大屏 立即下载