Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat a 2D NumPy array N times [duplicate]

I need to augment(replicate) a 2d array of shape 32X32 to a 3d array of shape 32X32X3 by duplicating the source array. How can i do this in the best possible way?

Below is the sample of the source and expected array. I need to apply this logic over a bigger scope of my application

Source array:

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Expected array:

array([[[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],

       [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],

       [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]])

2 Answers

By my tests, np.repeat is a little faster than np.tile:

X = np.repeat(arr[None,:], 3, axis=0)

Alternatively, use np.concatenate:

X = np.concatenate([[arr]] * 3, axis=0)

arr = np.arange(10000 * 1000).reshape(10000, 1000)

%timeit np.repeat(arr[None,:], 3, axis=0)
%timeit np.tile(arr, (3, 1, 1))
%timeit np.concatenate([[arr]] * 3, axis=0)
# Read-only, array cannot be modified.
%timeit np.broadcast_to(arr, (3, *arr.shape))
# Creating copy of the above.
%timeit np.broadcast_to(arr, (3, *arr.shape)).copy()

170 ms ± 3.82 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
187 ms ± 3.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
243 ms ± 3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
10.9 µs ± 218 ns per loop (mean ± std. dev. of 7 runs, 100000 loops 
189 ms ± 2.45 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)each) 

np.array_equals(np.repeat(arr[None,:], 3, axis=0), 
                np.tile(arr, (3, 1, 1))
True
like image 136
cs95 Avatar answered Aug 30 '26 07:08

cs95


Sounds like a job for np.tile:

In [101]: np.tile(A, (3,1,1))
Out[101]: 
array([[[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],

       [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]],

       [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]])

The second argument specifies the number of copies on each dimension.

like image 44
hilberts_drinking_problem Avatar answered Aug 30 '26 07:08

hilberts_drinking_problem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!