繁体   English   中英

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

[英]Unable to transform my input series and window-size into a set of input/output pairs for the RNN model

我目前正在构建一个循环神经网络模型,当我即将将输入数据转换为 RNN 模型的输入/输出集时,我目前陷入困境。

我尝试过将系列、window_size 和 stepsize 作为输入的 windoe_tranform_series 函数,但我一直收到 KEYERROR。

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

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

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

窗口大小 = 7 步长 = 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)  # create dummy series

现在, print (s[2])将打印'c'作为输出。

但是,如果您尝试打印超出范围的内容,则会出现KeyError

因此,这里的print (s[5])给出KeyError: 5 在您的情况下,您使用window_size=7开始 for 循环,并且由于您的series的长度小于7 ,因此它给出KeyError: 7 on line outputs.append(series[i])

有趣的是,当您尝试使用超出范围的索引对系列进行切片时,不会发生此错误。

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

1    b
2    c
3    d

因此, KeyError是在绕过inputs.append(series[ctr:i])线。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM