简体   繁体   中英

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

I am currently building a reccurent neural network model and i am currently stuck when i was about to transform my input data into a set on input/output for the RNN model.

I have tried the windoe_tranform_series function that takes the series, window_size and the stepsize as inputs but i keep getting a KEYERROR.

cutting our time series into sequences

The function below transforms the input series and window-size into a set #of input/output pairs for our RNN model.

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


        

Your series is not long enough. See the following example snippet.

import numpy as np
import pandas as pd

data = np.array(['a','b','c','d'])
s = pd.Series(data)  # create dummy series

Now, print (s[2]) would print 'c' as the output.

But if you try to print something out of range, it gives the KeyError .

So, print (s[5]) here gives KeyError: 5 . In your case, you start the for loop with window_size=7 and since the length of your series is less than 7 , it gives KeyError: 7 on line outputs.append(series[i]) .

Interestingly, this error doesn't happen when you try to slice the series with an out of range index.

Eg if you try to do print (s[1:5]) in the example above, it would just print the following instead of the KeyError .

1    b
2    c
3    d

Therefore, the KeyError is bypassed in your inputs.append(series[ctr:i]) line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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