On Mon, 2017-07-10 at 16:16 +0300, eat wrote: > Hi, > > On Mon, Jul 10, 2017 at 3:20 PM, <paul.carr...@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-part > > ition-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]]]) >
While maybe what he wants, I would avoid stride tricks if you can achieve the same thing with a reshape + transpose. Far more safe if you hardcode the strides, and much shorter if you don't, plus easier to read usually. One thing some people might get confused about with reshape is the order, numpy reshape defaults to C-order, while other packages may use fortran order for reshaping, you can actually change the order you want to use (though it is in general a good idea to prefer C-order in numpy probably). - Sebastian > Regards, > -eat > > Thanks for your support > > > > Paul > > > > _______________________________________________ > > NumPy-Discussion mailing list > > NumPy-Discussion@python.org > > https://mail.python.org/mailman/listinfo/numpy-discussion > > > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion
signature.asc
Description: This is a digitally signed message part
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion