How to break numpy array into smaller chunks/batches, then iterate through them
<p>Suppose i have this numpy array</p>

<pre><code>[[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]]
</code></pre>

<p>And i want to split it in 2 batches and then iterate:</p>

<pre><code>[[1, 2, 3],      Batch 1
[4, 5, 6]]

[[7, 8, 9],      Batch 2
[10, 11, 12]]
</code></pre>

<p>What is the simplest way to do it?</p>

<p>EDIT: <strong>I'm deeply sorry i missed putting such info</strong>: Once i intend to carry on with the iteration, the original array would be destroyed due to splitting and iterating over batches. Once the batch iteration finished, i need to restart again from the first batch hence <strong>I should preserve that the original array wouldn't be destroyed</strong>. The whole idea is to be consistent with Stochastic Gradient Descent algorithms which require iterations over batches. In a typical example, I could have a 100000 iteration For loop for just 1000 batch that should be replayed again and again.</p>

<p>consider array <code>a</code></p>

<pre><code>a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9],
              [10, 11, 12]])
</code></pre>

<p><strong><em>Option 1</em></strong><br>
use <code>reshape</code> and <code>//</code></p>

<pre><code>a.reshape(a.shape[0] // 2, -1, a.shape[1])

array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
</code></pre>

<p><strong><em>Option 2</em></strong><br>
if you wanted groups of two rather than two groups</p>

<pre><code>a.reshape(-1, 2, a.shape[1])

array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
</code></pre>

<p><strong><em>Option 3</em></strong><br>
Use a generator</p>

<pre><code>def get_every_n(a, n=2):
    for i in range(a.shape[0] // 2):
        yield a[2*i:2*(i+1)]

for sa in get_every_n(a):
    print sa

[[1 2 3]
 [4 5 6]]
[[ 7  8  9]
 [10 11 12]]
</code></pre>

python pandas numpy