IndexError: index is out of bounds for axis 0 with size
<p>I have array <code>x_train</code> and <code>targets_train</code>. I want to shuffle the training data and split it into smaller batches and use the batches as training data. My original data has 1000 rows and each time I try to use 250 rows of them :</p>

<pre><code>    x_train = np.memmap('/home/usr/train', dtype='float32', mode='r', shape=(1000, 1, 784))
# print(x_train)
targets_train = np.memmap('/home/usr/train_label', dtype='int32', mode='r', shape=(1000, 1))
train_idxs = [i for i in range(x_train.shape[0])]
np.random.shuffle(train_idxs)


num_batches_train = 4
def next_batch(start, train, labels, batch_size=250):
    newstart = start + batch_size
    if newstart &gt; train.shape[0]:
        newstart = 0
    idxs = train_idxs[start:start + batch_size]
    # print(idxs)
    return train[idxs, :], labels[idxs, :], newstart


# x_train_lab = x_train[:200]
# # x_train = np.array(targets_train)
# targets_train_lab = targets_train[:200]
for i in range(num_batches_train):
    x_train, targets_train, newstart = next_batch(i*batch_size, x_train, targets_train, batch_size=250)
</code></pre>

<p>The problem is, when I shuffle the training data and try to access to batches I get error saying: </p>

<pre><code>    return train[idxs, :], labels[idxs, :], newstart
    IndexError: index 250 is out of bounds for axis 0 with size 250
</code></pre>

<p>Is there anybody who knows what am I doing wrong?</p>

<p>The problem is in this line in the function definition:</p>

<pre><code>idxs = train_idxs[start:start + batch_size]
</code></pre>

<p>Change it to:</p>

<pre><code>idxs = train_idxs[start: newstart]
</code></pre>

<p>Then it should work as expected!</p>

<p>Also, please change the variable names in the <code>for</code> loop to something like:</p>

<pre><code>batch_size = 250
for i in range(num_batches_train):
    x_train_split, targets_train_split, newstart = next_batch(i*batch_size, 
                                                              x_train,
                                                              targets_train,
                                                              batch_size=250)
    print(x_train_split.shape, targets_train_split.shape, newstart)
</code></pre>

<p>Sample output:</p>

<pre><code>(250, 1, 784) (250, 1) 250
(250, 1, 784) (250, 1) 500
(250, 1, 784) (250, 1) 750
(250, 1, 784) (250, 1) 1000
</code></pre>

python arrays list numpy machine-learning