无法将我的输入序列和窗口大小转换为RNN模型的一组输入/输出对

我目前正在构建一个递归神经网络模型,但在将我的输入数据转换为一组输入/输出对用于RNN模型时遇到了困难。

我尝试使用了window_transform_series函数,该函数接受序列、窗口大小和步长作为输入,但我一直收到KEYERROR错误。

将我们的时间序列切割成序列

下面的函数将输入序列和窗口大小转换为一组输入/输出对,用于我们的RNN模型。

def window_transform_series(series,window_size,step_size):
    inputs = []
    outputs = []
    ctr = 0
    for i in range(window_size, len(series), step_size):
        inputs.append(series[ctr:i])
        outputs.append(series[i])
        ctr = ctr + step_size
    return inputs,outputs

window_size = 7
step_size = 5

inputs, outputs = window_transform_series(carbon_persil,window_size,step_size)
KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 7
During handling of the above exception, another exception occurred:
KeyError                                  Traceback (most recent call last)
<ipython-input-45-9810d786d8b5> in <module>
      2 window_size = 7
      3 step_size = 5
----> 4 inputs, outputs = window_transform_series(carbon_persil,window_size,step_size)
<ipython-input-41-82e8b484e9e9> in window_transform_series(series, window_size, step_size)
      9     for i in range(window_size, len(series), step_size):
     10         inputs.append(series[ctr:i])
---> 11         outputs.append(series[i])
     12         ctr = ctr + step_size
     13     return inputs,outputs
~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2657                 return self._engine.get_loc(key)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2661         if indexer.ndim > 1 or indexer.size > 1:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 7

回答:

你的series长度不够。请看下面的示例代码片段。

import numpy as np
import pandas as pd
data = np.array(['a','b','c','d'])
s = pd.Series(data)  # 创建示例系列

现在,print (s[2])将输出'c'

但如果你尝试打印超出范围的值,它会返回KeyError错误。

因此,print (s[5])在这里会返回KeyError: 5。在你的情况下,你的for循环从window_size=7开始,由于你的series的长度小于7,在outputs.append(series[i])这一行就会返回KeyError: 7

有趣的是,当你尝试使用超出范围的索引来切片系列时,这个错误不会发生。

例如,如果你在上面的示例中尝试print (s[1:5]),它只会打印以下内容而不是KeyError错误。

1    b
2    c
3    d

因此,在你的inputs.append(series[ctr:i])这一行中,KeyError被绕过了。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注