How to fix "IndexError" on python?

161 Views Asked by At

I am a beginner who studies machine learning and tries to train neural networks. but there is a problem when I run my code. error saying " IndexError: index 114 outside the limit for 0 axis with size 93 ". how this can happen dan how to fix it? thank you in advance if you can help me

this is the code that i used neupy library

optimizer = algorithms.LevenbergMarquardt(
    [
       layers.Input(6),
       layers.Sigmoid(12),
       layers.Relu(6),
    ],
    shuffle_data=True,
    verbose=True,

)

print("Training...")
optimizer.train(X_train, X_test, y_train, y_test,epochs=100)

the input is 6 features of glcm with 6 label output. I have split the data into testing and training data using train_test_split. this is data that I have split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
print("Jumlah data latih   : ", len(X_train))
print("Jumlah data uji     : ", len(y_train))
print("Jumlah data latih   : ", len(X_test))
print("Jumlah data uji     : ", len(y_test))

Jumlah data latih : 372 Jumlah data uji : 372 Jumlah data latih : 93 Jumlah data uji : 93

And this is the error message

IndexError                                Traceback (most recent call last)

<ipython-input-8-8e9b3836f4ce> in <module>()
     16 
     17 print("Training...")
---> 18 optimizer.train(X_train, X_test, y_train, y_test,epochs=100)
     19 
     20 p = optimizer.predict(testdata)

5 frames

/usr/local/lib/python3.6/dist-packages/neupy/algorithms/gd/base.py in train(self, X_train, y_train, X_test, y_test, *args, **kwargs)
    299             X_train=X_train, y_train=y_train,
    300             X_test=X_test, y_test=y_test,
--> 301             *args, **kwargs)
    302 
    303     def one_training_update(self, X_train, y_train):

/usr/local/lib/python3.6/dist-packages/neupy/algorithms/base.py in train(self, X_train, y_train, X_test, y_test, epochs, batch_size)
    264                 )
    265 
--> 266                 for X_batch, y_batch in iterator:
    267                     self.events.trigger('update_start')
    268                     update_start_time = time.time()

/usr/local/lib/python3.6/dist-packages/neupy/utils/iters.py in minibatches(inputs, batch_size, shuffle)
     64         for index in range(n_batches):
     65             batch_slice = slice(index * batch_size, (index + 1) * batch_size)
---> 66             yield apply_slices(inputs, indices[batch_slice])
     67 
     68     elif n_batches != 1:

/usr/local/lib/python3.6/dist-packages/neupy/utils/iters.py in apply_slices(inputs, indices)
     30 
     31     if isinstance(inputs, (list, tuple)):
---> 32         return [apply_slices(input_, indices) for input_ in inputs]
     33 
     34     return inputs[indices]

/usr/local/lib/python3.6/dist-packages/neupy/utils/iters.py in <listcomp>(.0)
     30 
     31     if isinstance(inputs, (list, tuple)):
---> 32         return [apply_slices(input_, indices) for input_ in inputs]
     33 
     34     return inputs[indices]

/usr/local/lib/python3.6/dist-packages/neupy/utils/iters.py in apply_slices(inputs, indices)
     32         return [apply_slices(input_, indices) for input_ in inputs]
     33 
---> 34     return inputs[indices]
     35 
     36 

IndexError: index 114 is out of bounds for axis 0 with size 93
1

There are 1 best solutions below

0
On

I'm not 100% sure which library you are using. But why are you putting testing set in the training? Testing set is for some sort of validate or evaluate function. Try to remove x_test and y_test in the training function. i.e. optimizer.train(X_train, y_train,epochs=100)