Leeren - 1 year ago 27

Python Question

So I have a 3D array with shape

`(28, 28, 60000)`

`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)`

`X`

`x`

`y`

Answer

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)
```