Reputation: 21
I was following a YouTube tutorial to learn deep learning (crypto prediction) but I was bombarded with errors. I debugged quite a few of them, but since I'm new to this I really can't figure out a way to solve this.
I get the error:
IndexError: tuple index out of range
on line
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))error traceback: `Traceback (most recent call last): File "/Users/usr/PycharmProjects/cryptoPred/main.py", line 35, in x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1)) IndexError: tuple index out of range` `
The full code for context:
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
Upvotes: 2
Views: 539
Reputation: 1878
The np.append
behaves differently in compare to the list.append
. For example see the below code
# 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.]])
'''
Now it is a 2D matrix and you can access it's second idx.
Upvotes: 0