Skip to main content
added 78 characters in body
Source Link
hpaulj
  • 233.3k
  • 14
  • 262
  • 394

Another method using reshape and transpose - U13Forward uses a 2d reshaping, I doing a 3d one:

In [262]: arr = np.arange(1,17); arr
Out[262]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])


In [264]: arr.reshape(-1,2,4)
Out[264]: 
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8]],

       [[ 9, 10, 11, 12],
        [13, 14, 15, 16]]])

In [265]: arr.reshape(-1,2,4).transpose(1,0,2)
Out[265]: 
array([[[ 1,  2,  3,  4],
        [ 9, 10, 11, 12]],

       [[ 5,  6,  7,  8],
        [13, 14, 15, 16]]])

In [266]: arr.reshape(-1,2,4).transpose(1,0,2).reshape(-1,8)
Out[266]: 
array([[ 1,  2,  3,  4,  9, 10, 11, 12],
       [ 5,  6,  7,  8, 13, 14, 15, 16]])

And to get a list of arrays instead of a 2d array, just use list:

In [268]: list(_266)
Out[268]: 
[array([ 1,  2,  3,  4,  9, 10, 11, 12]),
 array([ 5,  6,  7,  8, 13, 14, 15, 16])]

Individually reshape and transpose make views, but combined at least one copy is required.

Quick timing tests show that this performs well for the small example, but it doesn't scale as well. That final list step adds a lot of time for large problems - since it produces 1000's of (8,) arrays (as opposed to just 2 (n*4) arrays).

Another method using reshape and transpose - U13Forward uses a 2d reshaping, I doing a 3d one:

In [262]: arr = np.arange(1,17); arr
Out[262]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])


In [264]: arr.reshape(-1,2,4)
Out[264]: 
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8]],

       [[ 9, 10, 11, 12],
        [13, 14, 15, 16]]])

In [265]: arr.reshape(-1,2,4).transpose(1,0,2)
Out[265]: 
array([[[ 1,  2,  3,  4],
        [ 9, 10, 11, 12]],

       [[ 5,  6,  7,  8],
        [13, 14, 15, 16]]])

In [266]: arr.reshape(-1,2,4).transpose(1,0,2).reshape(-1,8)
Out[266]: 
array([[ 1,  2,  3,  4,  9, 10, 11, 12],
       [ 5,  6,  7,  8, 13, 14, 15, 16]])

And to get a list of arrays instead of a 2d array, just use list:

In [268]: list(_266)
Out[268]: 
[array([ 1,  2,  3,  4,  9, 10, 11, 12]),
 array([ 5,  6,  7,  8, 13, 14, 15, 16])]

Individually reshape and transpose make views, but combined at least one copy is required.

Quick timing tests show that this performs well for the small example, but it doesn't scale as well. That final list step adds a lot of time for large problems.

Another method using reshape and transpose - U13Forward uses a 2d reshaping, I doing a 3d one:

In [262]: arr = np.arange(1,17); arr
Out[262]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])


In [264]: arr.reshape(-1,2,4)
Out[264]: 
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8]],

       [[ 9, 10, 11, 12],
        [13, 14, 15, 16]]])

In [265]: arr.reshape(-1,2,4).transpose(1,0,2)
Out[265]: 
array([[[ 1,  2,  3,  4],
        [ 9, 10, 11, 12]],

       [[ 5,  6,  7,  8],
        [13, 14, 15, 16]]])

In [266]: arr.reshape(-1,2,4).transpose(1,0,2).reshape(-1,8)
Out[266]: 
array([[ 1,  2,  3,  4,  9, 10, 11, 12],
       [ 5,  6,  7,  8, 13, 14, 15, 16]])

And to get a list of arrays instead of a 2d array, just use list:

In [268]: list(_266)
Out[268]: 
[array([ 1,  2,  3,  4,  9, 10, 11, 12]),
 array([ 5,  6,  7,  8, 13, 14, 15, 16])]

Individually reshape and transpose make views, but combined at least one copy is required.

Quick timing tests show that this performs well for the small example, but it doesn't scale as well. That final list step adds a lot of time for large problems - since it produces 1000's of (8,) arrays (as opposed to just 2 (n*4) arrays).

added 169 characters in body
Source Link
hpaulj
  • 233.3k
  • 14
  • 262
  • 394

Another method using reshape and transpose - U13Forward uses a 2d reshaping, I doing a 3d one:

In [262]: arr = np.arange(1,17); arr
Out[262]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])


In [264]: arr.reshape(-1,2,4)
Out[264]: 
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8]],

       [[ 9, 10, 11, 12],
        [13, 14, 15, 16]]])

In [265]: arr.reshape(-1,2,4).transpose(1,0,2)
Out[265]: 
array([[[ 1,  2,  3,  4],
        [ 9, 10, 11, 12]],

       [[ 5,  6,  7,  8],
        [13, 14, 15, 16]]])

In [266]: arr.reshape(-1,2,4).transpose(1,0,2).reshape(-1,8)
Out[266]: 
array([[ 1,  2,  3,  4,  9, 10, 11, 12],
       [ 5,  6,  7,  8, 13, 14, 15, 16]])

And to get a list of arrays instead of a 2d array, just use list:

In [268]: list(_266)
Out[268]: 
[array([ 1,  2,  3,  4,  9, 10, 11, 12]),
 array([ 5,  6,  7,  8, 13, 14, 15, 16])]

Individually reshape and transpose make views, but combined at least one copy is required.

Quick timing tests show that this performs well for the small example, but it doesn't scale as well. That final list step adds a lot of time for large problems.

Another method using reshape and transpose - U13Forward uses a 2d reshaping, I doing a 3d one:

In [262]: arr = np.arange(1,17); arr
Out[262]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])


In [264]: arr.reshape(-1,2,4)
Out[264]: 
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8]],

       [[ 9, 10, 11, 12],
        [13, 14, 15, 16]]])

In [265]: arr.reshape(-1,2,4).transpose(1,0,2)
Out[265]: 
array([[[ 1,  2,  3,  4],
        [ 9, 10, 11, 12]],

       [[ 5,  6,  7,  8],
        [13, 14, 15, 16]]])

In [266]: arr.reshape(-1,2,4).transpose(1,0,2).reshape(-1,8)
Out[266]: 
array([[ 1,  2,  3,  4,  9, 10, 11, 12],
       [ 5,  6,  7,  8, 13, 14, 15, 16]])

And to get a list of arrays instead of a 2d array, just use list:

In [268]: list(_266)
Out[268]: 
[array([ 1,  2,  3,  4,  9, 10, 11, 12]),
 array([ 5,  6,  7,  8, 13, 14, 15, 16])]

Individually reshape and transpose make views, but combined at least one copy is required.

Another method using reshape and transpose - U13Forward uses a 2d reshaping, I doing a 3d one:

In [262]: arr = np.arange(1,17); arr
Out[262]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])


In [264]: arr.reshape(-1,2,4)
Out[264]: 
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8]],

       [[ 9, 10, 11, 12],
        [13, 14, 15, 16]]])

In [265]: arr.reshape(-1,2,4).transpose(1,0,2)
Out[265]: 
array([[[ 1,  2,  3,  4],
        [ 9, 10, 11, 12]],

       [[ 5,  6,  7,  8],
        [13, 14, 15, 16]]])

In [266]: arr.reshape(-1,2,4).transpose(1,0,2).reshape(-1,8)
Out[266]: 
array([[ 1,  2,  3,  4,  9, 10, 11, 12],
       [ 5,  6,  7,  8, 13, 14, 15, 16]])

And to get a list of arrays instead of a 2d array, just use list:

In [268]: list(_266)
Out[268]: 
[array([ 1,  2,  3,  4,  9, 10, 11, 12]),
 array([ 5,  6,  7,  8, 13, 14, 15, 16])]

Individually reshape and transpose make views, but combined at least one copy is required.

Quick timing tests show that this performs well for the small example, but it doesn't scale as well. That final list step adds a lot of time for large problems.

Source Link
hpaulj
  • 233.3k
  • 14
  • 262
  • 394

Another method using reshape and transpose - U13Forward uses a 2d reshaping, I doing a 3d one:

In [262]: arr = np.arange(1,17); arr
Out[262]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])


In [264]: arr.reshape(-1,2,4)
Out[264]: 
array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8]],

       [[ 9, 10, 11, 12],
        [13, 14, 15, 16]]])

In [265]: arr.reshape(-1,2,4).transpose(1,0,2)
Out[265]: 
array([[[ 1,  2,  3,  4],
        [ 9, 10, 11, 12]],

       [[ 5,  6,  7,  8],
        [13, 14, 15, 16]]])

In [266]: arr.reshape(-1,2,4).transpose(1,0,2).reshape(-1,8)
Out[266]: 
array([[ 1,  2,  3,  4,  9, 10, 11, 12],
       [ 5,  6,  7,  8, 13, 14, 15, 16]])

And to get a list of arrays instead of a 2d array, just use list:

In [268]: list(_266)
Out[268]: 
[array([ 1,  2,  3,  4,  9, 10, 11, 12]),
 array([ 5,  6,  7,  8, 13, 14, 15, 16])]

Individually reshape and transpose make views, but combined at least one copy is required.