Skip to content Skip to sidebar Skip to footer

How To Write A Huge 2d Numpy Array Into A Buffer

I have a huge 2D numpy array (dtype=bool) and a buffer and I would like to write this 2D array into the buffer. Currently, I do the following, # Python version 3.7.7, NumPy version

Solution 1:

You cannot assign a multi dimensioned memoryview slice.

NotImplementedError: memoryviewslice assignments are currently restricted to ndim = 1

So you might need to reshape your array to be one dimensional before copying it into a memoryview.

>>> dummy_array = np.array(np.empty((2, 213), dtype='bool'), dtype='bool').reshape(2*213)
>>> mem = memoryview(dummy_array)
>>> mem[0]
True>>> np.frombuffer(mem, dtype="bool").reshape(dummy_array.shape)
array([ True,  True,  True,  True,  True,  True, False, False,  True,

If you try to use multidimensional, you'll get this error.

>>>dummy_array = np.array(np.empty((2, 213), dtype='bool'), dtype='bool')>>>mem = memoryview(dummy_array)>>>mem[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NotImplementedError: multi-dimensional sub-views are not implemented

I can't tell you if this will be faster than your other method but it may give you some ideas for how to get the memoryview version working.

Post a Comment for "How To Write A Huge 2d Numpy Array Into A Buffer"