Reputation: 147
I am creating a Neural Network and currently I am working on the; train, test split
but I am getting the error IndexError: too many indices for array
My code is:
import csv
import math
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
import datetime
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
X1 = Values[1:16801] #16,800 values
Y1 = P1[1:16801]#16,800 values
train_size = int(len(X1) * 0.67)
test_size = len(X1) - train_size
train, test = X1[0:train_size,], X1[train_size:len(X1),]
def Data(X1, look_back=1):
dataX, dataY = [], []
for i in range(len(X1)-look_back-1):
a = X1[i:(i+look_back), 0]
dataX.append(a)
dataY.append(Y1[i + look_back, 0])
return numpy.array(dataX), numpy.array(dataY)
look_back = 1
trainX, testX = Data(train, look_back)
testX, testY = Data(test, look_back)
look_back = 1
trainX, testX = Data(train, look_back)
testX, testY = Data(test, look_back)
I have 16,800 values for X1 which look like:
[0.03454225 0.02062136 0.00186715 ... 0.92857565 0.64930691 0.20325924]
And my Y1 data looks like: [ 2.25226244 1.44078451 0.99174488 ... 12.8397099 9.75722427 7.98525797]
My traceback error message is:
IndexError Traceback (most recent call last)
<ipython-input-11-afedcaa56e0b> in <module>()
86
87 look_back = 1
---> 88 trainX, testX = Data_split(train, look_back)
89
90 testX, testY = Data_split(test, look_back)
<ipython-input-11-afedcaa56e0b> in Data(X1, look_back)
78 dataX, dataY = [], []
79 for i in range(len(X1)-look_back-1):
---> 80 a = X1[i:(i+look_back), 0]
81 dataX.append(a)
82 dataY.append(Y1[i + look_back, 0])
IndexError: too many indices for array
I asked a very similar question previously and got a answer but unfortunately I cannot apply that solution to this error
Upvotes: 1
Views: 976
Reputation: 837
The problem is with the dimension of an array. you are trying to access element with multiple dimensions indexes which don't exist. look at line number 80.
a = X1[i:(i+look_back), 0] in your case metrics is just single dimention.
sample 2d metrics representation (,)
"," is the reference to the two-dimensional array with row and column but unfortunately, you are having X1 as ndarray.
[0.03454225 0.02062136 0.00186715 ... 0.92857565 0.64930691 0.20325924]
Similar problem example:-
>>> np.ndarray(4)
array([2.0e-323, 1.5e-323, 2.0e-323, 1.5e-323])
>>> a[1:2,0]
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
a[1:2,0]
IndexError: too many indices for array
>>> a[1:2]
array([-2.68156159e+154])
>>>
Upvotes: 1