如何使用 fbProphet 或其他模型在 Python 中执行包含多个组的时间序列分析?

How to perform time series analysis that contains multiple groups in Python using fbProphet or other models?(如何使用 fbProphet 或其他模型在 Python 中执行包含多个组的时间序列分析?)
本文介绍了如何使用 fbProphet 或其他模型在 Python 中执行包含多个组的时间序列分析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

全部,

我的数据集如下所示.我正在尝试使用 fbProphet 或其他模型来预测未来 6 个月的金额".但我的问题是,我想根据每个组(即 A、B、C、D)预测未来 6 个月的数量.我不确定如何在 python 中使用 fbProphet 或其他模型来做到这一点?我参考了 fbprophet 的官方页面,但我找到的唯一信息是先知"只有两列,一是日期",另一列是金额".

My dataset looks like following. I am trying to predict the 'amount' for next 6 months using either the fbProphet or other model. But my issue is that I would like to predict amount based on each groups i.e A,B,C,D for next 6 months. I am not sure how to do that in python using fbProphet or other model ? I referenced official page of fbprophet, but the only information I found is that "Prophet" takes two columns only One is "Date" and other is "amount" .

我是 python 新手,非常感谢任何关于代码解释的帮助!

I am new to python, so any help with code explanation is greatly appreciated!

import pandas as pd
data = {'Date':['2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01','2017-05-01','2017-06-01','2017-07-01'],'Group':['A','B','C','D','C','A','B'],
       'Amount':['12.1','13','15','10','12','9.0','5.6']}
df = pd.DataFrame(data)
print (df)

输出:

         Date Group Amount
0  2017-01-01     A   12.1
1  2017-02-01     B     13
2  2017-03-01     C     15
3  2017-04-01     D     10
4  2017-05-01     C     12
5  2017-06-01     A    9.0
6  2017-07-01     B    5.6

推荐答案

fbprophet 需要 dsy 两列,所以需要先重命名两列

fbprophet requires two columns ds and y, so you need to first rename the two columns

df = df.rename(columns={'Date': 'ds', 'Amount':'y'})

假设您的组彼此独立并且您希望为每个组获得一个预测,您可以按组"列对数据框进行分组并为每个组运行预测

Assuming that your groups are independent from each other and you want to get one prediction for each group, you can group the dataframe by "Group" column and run forecast for each group

from fbprophet import Prophet
grouped = df.groupby('Group')
for g in grouped.groups:
    group = grouped.get_group(g)
    m = Prophet()
    m.fit(group)
    future = m.make_future_dataframe(periods=365)
    forecast = m.predict(future)
    print(forecast.tail())

请注意,您在问题中提供的输入数据框对于模型来说是不够的,因为 D 组只有一个数据点.fbprophet 的预测至少需要 2 个非 Nan 行.

Take note that the input dataframe that you supply in the question is not sufficient for the model because group D only has a single data point. fbprophet's forecast needs at least 2 non-Nan rows.

如果您想将所有预测合并到一个数据帧中,想法是为每个观察分别命名 yhat,在循环,然后在最后挑选您需要的列:

if you want to merge all predictions into one dataframe, the idea is to name the yhat for each observations differently, do pd.merge() in the loop, and then cherry-pick the columns that you need at the end:

final = pd.DataFrame()
for g in grouped.groups:
    group = grouped.get_group(g)
    m = Prophet()
    m.fit(group)
    future = m.make_future_dataframe(periods=365)
    forecast = m.predict(future)    
    forecast = forecast.rename(columns={'yhat': 'yhat_'+g})
    final = pd.merge(final, forecast.set_index('ds'), how='outer', left_index=True, right_index=True)

final = final[['yhat_' + g for g in grouped.groups.keys()]]

这篇关于如何使用 fbProphet 或其他模型在 Python 中执行包含多个组的时间序列分析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)