Python - repeating a numpy array without data replication

This question has been asked before, but the solution only works for 1D / 2D arrays, and I need a more general answer.

How do you create a repeating array without data replication? This strikes me as something in common, as it would help to vectorize python operations without losing memory.

In particular, I have an array (y, x) that I want to alternate several times to create an array (z, y, x). I can do this with numpy.tile (array, (nz, 1,1)), but I'm out of memory. My specific case has x = 1500, y = 2000, z = 700.

+6
source share
1 answer

One simple trick is to use np.broadcast_arrays to translate your (x, y) into a z -long vector in the first dimension:

 import numpy as np M = np.arange(1500*2000).reshape(1500, 2000) z = np.zeros(700) # broadcasting over the first dimension _, M_broadcast = np.broadcast_arrays(z[:, None, None], M[None, ...]) print M_broadcast.shape, M_broadcast.flags.owndata # (700, 1500, 2000), False 

To summarize the stride_tricks method given for the 1D array in this answer , you just need to include the shape and step length for each dimension of your output array

 M_strided = np.lib.stride_tricks.as_strided( M, # input array (700, M.shape[0], M.shape[1]), # output dimensions (0, M.strides[0], M.strides[1]) # stride length in bytes ) 
+4
source

Source: https://habr.com/ru/post/969372/


All Articles