[Numpy-discussion] reshape 2D array into 3D

eat e.antero.tammi at gmail.com
Mon Jul 10 09:16:45 EDT 2017


Hi,

On Mon, Jul 10, 2017 at 3:20 PM, <paul.carrico at free.fr> wrote:

> Dear All
>
> I'm looking in a way to reshape a 2D matrix into a 3D one ; in my example
> I want to *move the columns from the 4th to the 8th in the 2nd plane*  (3rd
> dimension i guess)
>
> a =  np.random.rand(5,8); print(a)
>
> I tried
>
> a = p.reshape(d, (2,5,4), ) but it is not what I'm expecting
>
>
> Nota : it looks like the following task (while I want to split it in 2
> levels and not in 4), but I've not understood at all
>
> https://stackoverflow.com/questions/31686989/numpy-
> reshape-and-partition-2d-array-to-3d
>
Is this what you are looking for:
import numpy as np

a= np.arange(40).reshape(5, 8)

a
Out[]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39]])

np.lib.stride_tricks.as_strided(a, (2, 5, 4), (16, 32, 4))
Out[]:
array([[[ 0,  1,  2,  3],
        [ 8,  9, 10, 11],
        [16, 17, 18, 19],
        [24, 25, 26, 27],
        [32, 33, 34, 35]],

       [[ 4,  5,  6,  7],
        [12, 13, 14, 15],
        [20, 21, 22, 23],
        [28, 29, 30, 31],
        [36, 37, 38, 39]]])

Regards,
-eat

>
> Thanks for your support
>
>
> Paul
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170710/c9c66848/attachment.html>


More information about the NumPy-Discussion mailing list