【问题标题】:How to fix "IndexError" on python?如何修复 python 上的“IndexError”?
【发布时间】:2019-09-14 06:43:04
【问题描述】:

我是一名学习机器学习并尝试训练神经网络的初学者。但是当我运行我的代码时出现问题。错误说“ IndexError: index 114 outside the limit for 0 axis with size 93"。怎么会发生这种情况以及如何解决?如果你能帮助我,请提前谢谢你

这是我使用 neupy 库的代码

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)

输入是带有 6 个标签输出的 glcm 的 6 个特征。我使用train_test_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 数据条数:372 Jumlah 数据 uji : 372 Jumlah 数据条 : 93 Jumlah 数据 uji : 93

这是错误信息

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) 不要那样做,或 2) 尝试捕获 IndexError 并处理或跳过它。
  • 我添加了错误信息。提前谢谢你

标签: python neupy


【解决方案1】:

我不能 100% 确定您使用的是哪个库。但是你为什么要把测试集放在训练中呢?测试集用于某种验证或评估功能。尝试删除训练函数中的 x_test 和 y_test。 即 optimizer.train(X_train, y_train,epochs=100)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 2019-12-09
    • 1970-01-01
    • 2019-10-19
    • 2016-01-03
    • 2011-07-16
    • 2019-09-15
    相关资源
    最近更新 更多