reshape 2D array into 3D

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

reshape 2D array into 3D

paul.carrico

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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: reshape 2D array into 3D

eat-3
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-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
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion



_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: reshape 2D array into 3D

paul.carrico

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)
print("b =")
b = np.lib.stride_tricks.as_strided(a, (2, 5, 4), (16, 32, 4)); print(b)

[[ 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]]
b =
[[[ 0 4294967296 1 8589934592]
[ 4 21474836480 5 25769803776]
[ 8 38654705664 9 42949672960]
[ 12 55834574848 13 60129542144]
[ 16 73014444032 17 77309411328]]

[[ 2 12884901888 3 17179869184]
[ 6 30064771072 7 34359738368]
[ 10 47244640256 11 51539607552]
[ 14 64424509440 15 68719476736]
[ 18 81604378624 19 85899345920]]]




Le 2017-07-10 15:16, eat a écrit :

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-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
[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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: reshape 2D array into 3D

Yarko Tymciurak

On Mon, Jul 10, 2017 at 8:46 AM Yarko Tymciurak <[hidden email]> wrote:
On Mon, Jul 10, 2017 at 8:37 AM <[hidden email]> wrote:

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.


Question:

Did you try to control the python & numpy versions by creating a virtualenv, or a conda env?


I didn't want to impact the other solvers installed on, so I stopped

Paul

a = np.arange(40).reshape(5, 8); print(a)
print("b =")
b = np.lib.stride_tricks.as_strided(a, (2, 5, 4), (16, 32, 4)); print(b)

[[ 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]]
b =
[[[ 0 4294967296 1 8589934592]
[ 4 21474836480 5 25769803776]
[ 8 38654705664 9 42949672960]
[ 12 55834574848 13 60129542144]
[ 16 73014444032 17 77309411328]]

[[ 2 12884901888 3 17179869184]
[ 6 30064771072 7 34359738368]
[ 10 47244640256 11 51539607552]
[ 14 64424509440 15 68719476736]
[ 18 81604378624 19 85899345920]]]




Le 2017-07-10 15:16, eat a écrit :

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-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
[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

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: reshape 2D array into 3D

paul.carrico

"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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: reshape 2D array into 3D

Sebastian Berg
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]]])
>
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
> > [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
Loading...