Leeren Leeren - 2 years ago 68
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
to my matrix
, however, the same chunk from each sample is returned. How do I ensure each sample uses different randomly generated

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download