愉快的学习就从翻译开始吧_Multivariate Forecasting

Multivariate Forecasting/多变量预测

Another important type of time series is called multivariate time series.

另一种重要的时间序列类型被称为多变量时间序列

This is where we may have observations of multiple different measures and an interest in forecasting one or more of them.

在这里我们可以有多个不同测量的观测值,并且对预测他们中的一个或多个有兴趣

For example, we may have two sets of time series observations obs1 and obs2 and we wish to forecast one or both of these.

例如,我们可能有两组时间序列观测obs1和obs2,我们希望预测其中的一个或两个。

We can call series_to_supervised() in exactly the same way.

我们可以用完全相同的方式调用series_to_supervised()。

For example:

例如:

from pandas import DataFrame
from pandas import concat


def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
    """
    Frame a time series as a supervised learning dataset.
    Arguments:
        data: Sequence of observations as a list or NumPy array.
        n_in: Number of lag observations as input (X).
        n_out: Number of observations as output (y).
        dropnan: Boolean whether or not to drop rows with NaN values.
    Returns:
        Pandas DataFrame of series framed for supervised learning.
    """
    n_vars = 1 if type(data) is list else data.shape[1]
    df = DataFrame(data)
    cols, names = list(), list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols.append(df.shift(i))
        names += [('var%d(t-%d)' % (j + 1, i)) for j in range(n_vars)]
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols.append(df.shift(-i))
        if i == 0:
            names += [('var%d(t)' % (j + 1)) for j in range(n_vars)]
        else:
            names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
    # put it all together
    agg = concat(cols, axis=1)
    agg.columns = names
    # drop rows with NaN values
    if dropnan:
        agg.dropna(inplace=True)
    return agg


raw = DataFrame()
raw['ob1'] = [x for x in range(10)]
raw['ob2'] = [x for x in range(50, 60)]
values = raw.values
data = series_to_supervised(values)
print(data)

Running the example prints the new framing of the data, showing an input pattern with one time step for both variables and an output pattern of one time step for both variables.

运行该示例将打印数据的新框架,显示两个变量一个时间步的输入对,以及两个变量的一个时间步的输出对。

Again, depending on the specifics of the problem, the division of columns into X and Y components can be chosen arbitrarily, such as if the current observation of var1 was also provided as input and only var2 was to be predicted.

同样,根据问题的具体情况,可以任意选择将列划分为X和Y分量,例如,如果还提供var1的当前观察作为输入,仅预测var2。

You can see how this may be easily used for sequence forecasting with multivariate time series by specifying the length of the input and output sequences as above.

通过像上面那样指定输入和输出序列的长度,您可以看到这将多么容易用于多变量时间序列的序列预测。

For example, below is an example of a reframing with 1 time step as input and 2 time steps as forecast sequence.

例如,下面是以1个时间步骤作为输入并且2个时间步骤作为预测序列的重新构造的示例。

from pandas import DataFrame
from pandas import concat


def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
    """
    Frame a time series as a supervised learning dataset.
    Arguments:
        data: Sequence of observations as a list or NumPy array.
        n_in: Number of lag observations as input (X).
        n_out: Number of observations as output (y).
        dropnan: Boolean whether or not to drop rows with NaN values.
    Returns:
        Pandas DataFrame of series framed for supervised learning.
    """
    n_vars = 1 if type(data) is list else data.shape[1]
    df = DataFrame(data)
    cols, names = list(), list()
    # input sequence (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols.append(df.shift(i))
        names += [('var%d(t-%d)' % (j + 1, i)) for j in range(n_vars)]
    # forecast sequence (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols.append(df.shift(-i))
        if i == 0:
            names += [('var%d(t)' % (j + 1)) for j in range(n_vars)]
        else:
            names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
    # put it all together
    agg = concat(cols, axis=1)
    agg.columns = names
    # drop rows with NaN values
    if dropnan:
        agg.dropna(inplace=True)
    return agg


raw = DataFrame()
raw['ob1'] = [x for x in range(10)]
raw['ob2'] = [x for x in range(50, 60)]
values = raw.values
data = series_to_supervised(values, 1, 2)
print(data)

Running the example shows the large reframed DataFrame.

Experiment with your own dataset and try multiple different framings to see what works best.

试验您自己的数据集并尝试多种不同的框架,看看哪种方法效果最好。

Summary/总结

In this tutorial, you discovered how to reframe time series datasets as supervised learning problems with Python.

在本教程中,您了解了如何将时间序列数据集重新定义为Python的监督学习问题。

Specifically, you learned:

具体来说,你了解到:

  • About the Pandas shift() function and how it can be used to automatically define supervised learning datasets from time series data.
    关于Pandas shift()函数以及它如何用于从时间序列数据中自动定义监督学习数据集。
  • How to reframe a univariate time series into one-step and multi-step supervised learning problems.
    如何将单变量时间序列重构为一步和多步监督学习问题。
  • How to reframe multivariate time series into one-step and multi-step supervised learning problems.
    如何将多变量时间序列重构为一步和多步监督学习问题。

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

你有任何问题吗?
在下面的评论中提出您的问题,我会尽我所能来回答。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值