Python Forum
Weird IndexError (help a beginner)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weird IndexError (help a beginner)
#1
Exclamation 
Hello,
When I pass as input an array of shape (15,1) I get:
IndexError: index 15 is out of bounds for axis 0 with size 15

However, when I pass the same array after eliminating the second column, i.e. passing an array of shape (15,), then the error
disappears.

How could this be possible? Nothing else is changed between the two attempts.

The relevant piece of code is the following:
class curve:
	def __init__(self, lam, P11, F, extra):
		self.extra = extra
		self.lam = lam
		self.P11 = P11
		self.F = F
		self.P11_pred = np.zeros(P11.shape)
		self.P11_pred[:] = np.NaN # Initialize a spot where later on the prediction can be stored        
		
	def predict(self, model):
		self.P11_pred = model.predict([self.F, self.extra]) # Predict stress using the provided keras model
The error log is the following:
Error:
File D:\CANN-master\CANN-master\CANN\Inputs.py:198 in predict self.P11_pred = model.predict([self.F, self.extra]) # Predict stress using the provided keras model File ~\Anaconda3\envs\new_env\lib\site-packages\tensorflow\python\keras\engine\training_v1.py:983 in predict return func.predict( File ~\Anaconda3\envs\new_env\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py:708 in predict return predict_loop( File ~\Anaconda3\envs\new_env\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py:370 in model_iteration ins_batch = slice_arrays(ins, batch_ids) File ~\Anaconda3\envs\new_env\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py:713 in slice_arrays return [None if x is None else x[start] for x in arrays] File ~\Anaconda3\envs\new_env\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py:713 in <listcomp> return [None if x is None else x[start] for x in arrays] IndexError: index 15 is out of bounds for axis 0 with size 15
P.S I use Spyder version: 5.1.5
* Python version: 3.8.3 64-bit
* Qt version: 5.9.7
* PyQt5 version: 5.9.2
* Operating System: Windows 10
Reply
#2
zeros((15, 1)) makes a 2D array that is an array of 15 arrays that are all length 1.
[[0.]
[0.]
<snip>
[0.]]

zeros((15,) makes a 1D array containing 15 zeros. Completely different.
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Reply
#3
return [None if x is None else x[start] for x in arrays]
IndexError: index 15 is out of bounds for axis 0 with size 15
x[start] for x in arrays] is the significent part . Debug this yourself. A simple print(x, start) should do the trick.
Reply
#4
woooee, the line you mention is in the tensorflow package. That code works fine. The programming error is in this line:
self.P11_pred = model.predict([self.F, self.extra])
Either self.F or self.extra (or both) is not correct for the call.
Reply
#5
I have found the bug.
model.predict is called in two instances in the program. In one of them, it works fine, whereas the other involves a dateset with a higher resolution of points. Thus, since 'extra' only had a fixed number of elements, the second instance fails in applying the method on the data.
I solved it by resizing 'extra' with np.resize to dynamically adjust its size according to the instance.

Thanks for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Python beginner: Weird Syntax Error mnsaathvika 1 2,155 Jul-22-2019, 06:14 AM
Last Post: buran
  Python beginner: Weird Syntax Error mentoly 5 10,370 Oct-13-2017, 08:06 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020