title: Split numpy array into chunks by maxmimum size
body: <p>I have some <em>very</em> large two-dimensional numpy arrays. One data set is 55732 by 257659, which is over 14 billion elements. Because some operations I need to perform throw <code>MemoryError</code>s, I would like to try splitting the array up into chunks of a certain size and running them against the chunks. (I can aggregate the results after the operation runs on each piece.) The fact that my problem is <code>MemoryErrors</code> means that it's important that I can cap the size of the arrays somehow, rather than split them into a constant number of pieces.</p>
  
  <p>For an example, let's generate a 1009 by 1009 random array:</p>
  
  <pre><code>a = numpy.random.choice([1,2,3,4], (1009,1009))
  </code></pre>
  
  <p>My data isn't necessary possible to split evenly, and it's definitely not guaranteed to be splitable by the size I want. So I've chosen 1009 because it's prime.</p>
  
  <p>Let's also say I want them in chunks of no larger than 50 by 50. Since this is just to avoid errors with extremely large arrays, it's okay if the result isn't exact.</p>
  
  <p>How can I split this into the desired chunks?</p>
  
  <p>I'm using Python 3.6 64-bit with numpy 1.14.3 (latest).</p>
  
  <h2>Related</h2>
  
  <p>I have seen <a href="https://stackoverflow.com/a/16858283/1394393">this function that uses <code>reshape</code></a>, but it doesn't work if the number of rows and columns doesn't divide the size exactly.</p>
  
  <p><a href="https://stackoverflow.com/q/14406567/1394393">This question</a> (among other similar ones) has answers explaining how to split into a certain number of chunks, but this does not explain how to split into a certain size.</p>
  
  <p>I also saw <a href="https://stackoverflow.com/q/39877999/1394393">this question</a>, since it's actually my exact problem. The answer and comments suggests switching to 64 bit (which I already have) and using <code>numpy.memmap</code>. Neither helped.</p>