eager2learn
eager2learn

Reputation: 1488

Copy columns of subarray in Numpy

Given an array X of shape (100,8192), I want to copy the subarrays of length 8192 for each of the 100 outer dimensions 10 times, so that the resulting array has shape (100,8192,10).

I'm kind of confused about how the tile function works, I can sort of only copy a 1d array (although probably not really elegantly), e.g. if I'm given a 1d array of shape (8192,), I can create a 2d array by copying the 1d array like this: np.tile(x,(10,1)).transpose(), but once I try to do this on a 2d array, I have no idea what the tile function is actually doing when you provide a tuple of values, the documentation is kind of unclear about that.

Can anybody tell me how to do this please?

EDIT: Example, given the 2d array:

In [229]: x
Out[229]: 
array([[1, 2, 3],
       [4, 5, 6]])

I want to get by copying along the columns 3 times in this case, the following array:

In [233]: y
Out[233]: 
array([[[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]],

       [[4, 4, 4],
        [5, 5, 5],
        [6, 6, 6]]])

Upvotes: 1

Views: 424

Answers (2)

eager2learn
eager2learn

Reputation: 1488

One way to do this is using np.repeat, e.g.:

Let X be the array of shape (100,8192), to replicate the subarray of dimension 8192 10-times across the column dimension, do the following:

X_new = np.repeat(X,10).reshape(100,8192,10)

Upvotes: 2

Thomas Baruchel
Thomas Baruchel

Reputation: 7517

Are you really asking for a shape (100,8192,10)? By reading you, I would have rather thought of something like (100,10,8192)? Could you provide an example? If you're actually asking for (100,10,8192), maybe you want:

np.tile(x,10).reshape((100,10,8192))

Is it what you're asking for?

Upvotes: 1

Related Questions