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 Thanks for your support Paul _______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion |
Hi,
On Mon, Jul 10, 2017 at 3:20 PM, <[hidden email]> wrote:
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
_______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion |
Thanks Nevertheless it does not work for me and I suspect the python/numpy releases :-( The server on which I'm working on is under Contos 7 that uses python 2.7 et numpy 1.7 from memory ; I tried to upgrade both of them (plus spyder) but it fails. I didn't want to impact the other solvers installed on, so I stopped Paul a = np.arange(40).reshape(5, 8); print(a) [[ 0 1 2 3 4 5 6 7] [[ 2 12884901888 3 17179869184] Le 2017-07-10 15:16, eat a écrit :
_______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion |
On Mon, Jul 10, 2017 at 8:46 AM Yarko Tymciurak <[hidden email]> wrote:
Question: Did you try to control the python & numpy versions by creating a virtualenv, or a conda env?
_______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion |
"Question: Did you try to control the python & numpy versions by creating a virtualenv, or a conda env?" I've just downloaded (ana)conda, but I've to take care first that it does not substitute to current python release working for for other solvers thanks for the information's and the support Paul _______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion |
In reply to this post by eat-3
On Mon, 2017-07-10 at 16:16 +0300, eat wrote:
> Hi, > > On Mon, Jul 10, 2017 at 3:20 PM, <[hidden email]> 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]]]) > 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 > > [hidden email] > > https://mail.python.org/mailman/listinfo/numpy-discussion > > > > _______________________________________________ > NumPy-Discussion mailing list > [hidden email] > https://mail.python.org/mailman/listinfo/numpy-discussion NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion signature.asc (817 bytes) Download Attachment |
Free forum by Nabble | Edit this page |