【问题标题】:IndexError: tuple index out of range LSTM model: working with numpy and indexesIndexError:元组索引超出范围 LSTM 模型:使用 numpy 和索引
【发布时间】:2021-10-18 19:27:29
【问题描述】:

我正在关注YouTube tutorial to learn deep learning(加密预测),但我被错误轰炸了。我调试了很多,但由于我是新手,我真的想不出解决这个问题的方法。

我得到错误:

IndexError: 元组索引超出范围 on linex_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))

错误回溯:`回溯(最近一次调用最后): 文件“/Users/usr/PycharmProjects/cryptoPred/main.py”,第 35 行,在 x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) IndexError:元组索引超出范围` `

上下文的完整代码:

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    import pandas_datareader as web
    import datetime as dt
    
    from sklearn.preprocessing import MinMaxScaler
    from tensorflow.keras.layers import Dense, Dropout, LSTM
    from tensorflow.keras.layers import Lambda
    from tensorflow.keras.models import Sequential
    
    # loading data from yahoo financial API
    crypto_currency = 'BTC'
    rl_currency = 'USD'
    
    start = dt.datetime(2016, 1, 1)
    end = dt.datetime(2021, 8, 10)
    
    data = web.DataReader(f'{crypto_currency}-{rl_currency}', 'yahoo', start, end)
    
    # preparing data
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1, 1))
    
    prediction_days = 60
    
    x_train, y_train = np.array([]), np.array([])
    print(x_train)
    
    for x in range(prediction_days, len(scaled_data)):
            x_train = np.append(x_train, scaled_data[x-prediction_days:x, 0])  
    
            y_train = np.append(y_train, scaled_data[x, 0])

    x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) // error line

【问题讨论】:

  • 始终共享完整的回溯。大声笑,您要么修改了错误行,要么根本不包括在内。这基本上打破了这个问题。
  • @AbhishekPrajapat 我不知道,我编辑了帖子
  • @AbhishekPrajapat 你能帮忙吗

标签: python numpy deep-learning lstm


【解决方案1】:

list.append 相比,np.append 的行为不同。例如看下面的代码

# First What you are doing is like below
x_train, y_train = np.array([]), np.array([])
temp_list = [i for i in range(5)]
for x in range(1, 5):
    x_train = np.append(x_train, temp_list, 0)

# After doing this the output is 
'''
After doing this the x_train is 
array([0., 1., 2., 3., 4., 0., 1., 2., 3., 4., 0., 1., 2., 3., 4., 0., 1.,
       2., 3., 4.])

As you can see this is a 1D vector but what you want is a 2D matrix. 
You should do it like below
'''
# Pre allocate space for faster editing
x_train = np.zeros((5, 5))

# Now change the value at idx as required
start = 0
for idx, x in enumerate(range(5)):
    x_train[idx] = [i for i in range(start, start+5)]
    start += 1a

# This will give the output of x_train as below
'''
array([[0., 1., 2., 3., 4.],
       [1., 2., 3., 4., 5.],
       [2., 3., 4., 5., 6.],
       [3., 4., 5., 6., 7.],
       [4., 5., 6., 7., 8.]])
'''

现在它是一个二维矩阵,您可以访问它的第二个 idx。

【讨论】:

  • 我对此很陌生,所以我有点困惑。第二个代码的第一个代码中使用的temp_list 是什么?
  • 我需要在x_train 中附加一些数据,所以我创建了一些任意数据。
猜你喜欢
  • 2021-12-21
  • 1970-01-01
  • 2013-07-28
  • 2018-11-16
  • 2017-07-12
  • 2013-12-16
  • 2014-08-01
  • 2021-04-19
相关资源
最近更新 更多