Python Forum

Full Version: Weird IndexError (help a beginner)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.]
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.
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.
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!