Leeren - 5 months ago 3x
Python Question

# How do you apply a function incorporating random numbers to rows of a numpy array in python?

So I have a 3D array with shape

`(28, 28, 60000)`
, corresponding to 60000 28x28 images. I want to get random 24x24 chunks of each image by using the following function:

``````def crop(X):
x = random.randint(0,3)
y = random.randint(0,3)
return X[x:24+x, y:24+y,]
``````

If I apply the function
`crop(X)`
to my matrix
`X`
, however, the same chunk from each sample is returned. How do I ensure each sample uses different randomly generated
`x`
and
`y`
values?

Here is my attempt at it.

Basically the idea is you will have to somehow split the matrix away from the last dimension (numpy doesn't let you apply over things which aren't a 1d array). You can do this using `dsplit`, and put it back together using `dstack`.

Then you would apply your crop function over each component. As a simplified example:

``````import random

a = np.array(range(300)).reshape(10,10,3)

def crop(X):
x = random.randint(0,3)
y = random.randint(0,3)
return X[x:3+x, y:3+y]

# we can loop over each component of the matrix by first splitting it
# off the last dimension:
b = [np.squeeze(x) for x in np.dsplit(a, a.shape[-1])]

# this will recreate the original matrix
c = np.dstack(b)

# so putting it together with the crop function
get_rand_matrix = [crop(np.squeeze(x)) for x in np.dsplit(a, a.shape[-1])]
desired_result = np.dstack(get_rand_matrix)
``````