Search code examples
pythonnumpytensorflowkerasrecurrent-neural-network

IndexError: tuple index out of range to split features and label


I got an error when trying to split features and label from DATASET. Here is the code:

print("Persiapan data")
#Split features and label from DATASET 
X = [] #features
Y = [] #labels
x_train,y_train=[],[]
for features,label in vektor_input:
    X.append(features)
    Y.append(label)
    
#Split X,Y to train and test
x_train,x_test,y_train,y_test = train_test_split(X, Y, train_size=0.7)

x_train = np.array(x_train)
x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))


print("Ukuran x_train :",len(x_train))
print("Ukuran x_test  :",len(x_test))

the error shows. i'm new in python. please help, i've tried to fix it, but got some errors and stuck on this.

IndexError Traceback (most recent call
 last) <ipython-input-12-cd5441347f88> in <module>
      12 
      13 x_train = np.array(x_train)
 ---> 14 x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
      15 x_test = np.array(x_test)
      16 x_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))
  IndexError: tuple index out of range

Solution

  • Lets say np_array is a 1D array.

    when using np_array.shape of a you will get output as -> ( number of rows , )
    so np_array.shape[1] gives you error "IndexError: tuple index out of range"

    To solve this use an integer value instead of np_array.shape[1] as needed. ( Note: Total number of elements in input and output arrays should be same)

    eg:

    num = 2 # change this according to required output 
    x_train = np.reshape(x_train, (int(x_train.shape[0]/num), 1, num))