I am generating a very large matrix of permutations based on the numbers 0,1, and 2. Ideally, I want them 48 wide but even happy with 20. Right now, I run out of memory so wanted to see if there was a non-memory based option available.
I did try it on a large memory machine but I would prefer an option that uses disk (I have a fast SSD) even if it takes a long time. As you see, I have already made it an int8 instead of int32 to save some space but even that only went so far. Grid_size of 18 is 6.5G where 19 is 21G so I understand it grows exponentially and I will be a TB soon.
import numpy as np
class LargeGrid():
def permgrid(self, m, n):
inds = np.indices((m,) * n, dtype=np.int8)
arr = inds.reshape(n, -1).T
arr[arr == 2] = -1
return arr
def save(self):
grid_size = 24
grid = self.permgrid(3,grid_size)
np.save('grid', grid)
lg = LargeGrid()
lg.save()
Expected a very large file on disk but ended up with:
Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] on win32
MemoryError: Unable to allocate array with shape (24, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3) and data type int8