I have a 2D array and I'd like to transform it into a 3D array where each row of the new array contains multiple rows of the original 2D array.
This code replicates the functionality (each row of the output array contains 3 rows of the input array) but I'm just wondering what the correct way to do this is and I think a more correct way of indexing would be faster for large datasets.
input = np.arange(100) + np.arange(100)[:,None]
output = np.apply_along_axis(lambda x: input[x[0]:x[0]+3], 1, np.arange(100-2)[:,None])
The input array looks like this:
array([[ 0, 1, 2, ..., 97, 98, 99],
[ 1, 2, 3, ..., 98, 99, 100],
[ 2, 3, 4, ..., 99, 100, 101],
...,
[ 97, 98, 99, ..., 194, 195, 196],
[ 98, 99, 100, ..., 195, 196, 197],
[ 99, 100, 101, ..., 196, 197, 198]])
And the output array looks like this:
array([[[ 0, 1, 2, ..., 97, 98, 99],
[ 1, 2, 3, ..., 98, 99, 100],
[ 2, 3, 4, ..., 99, 100, 101]],
[[ 1, 2, 3, ..., 98, 99, 100],
[ 2, 3, 4, ..., 99, 100, 101],
[ 3, 4, 5, ..., 100, 101, 102]],
[[ 2, 3, 4, ..., 99, 100, 101],
[ 3, 4, 5, ..., 100, 101, 102],
[ 4, 5, 6, ..., 101, 102, 103]],
...,
[[ 95, 96, 97, ..., 192, 193, 194],
[ 96, 97, 98, ..., 193, 194, 195],
[ 97, 98, 99, ..., 194, 195, 196]],
[[ 96, 97, 98, ..., 193, 194, 195],
[ 97, 98, 99, ..., 194, 195, 196],
[ 98, 99, 100, ..., 195, 196, 197]],
[[ 97, 98, 99, ..., 194, 195, 196],
[ 98, 99, 100, ..., 195, 196, 197],
[ 99, 100, 101, ..., 196, 197, 198]]])
inputisnp.arange(100) + np.arange(100)[:, None], right? And output shape is(98, 3, 100)?