When I print a torch tensor, I get the below output. How could I get that tensor without [] for the inner elements?
I printed the type of the first element and it returns <class 'torch.Tensor'> So this tensor seems to be a tensor of tensors...How could I convert it to a tensor of numbers?
tensor([[-5.6117e-01],
[ 3.5726e-01],
[-2.5853e-01],
[-4.8641e-01],
[-1.0581e-01],
[-1.8322e-01],
[-1.2732e+00],
[-5.9760e-02],
[ 1.2819e-01],
[ 6.3894e-02],
[-9.1817e-01],
[-1.6539e-01],
[-1.1471e+00],
[ 1.9666e-01],
[-6.3297e-01],
[-4.0876e-01],
[-2.4590e-02],
[ 2.7065e-01],
[ 3.5308e-01],
[-4.6348e-01],
[-4.1755e-01],
[-1.1554e-01],
[-4.2062e-01],
[ 1.4067e-01],
[-2.9788e-01],
[-7.4582e-02],
[-5.3751e-01],
[ 1.1344e-01],
[-2.6100e-01],
[ 2.6951e-02],
[-5.0437e-02],
[-1.9163e-01],
[-3.3893e-02],
[-5.9640e-01],
[-1.1574e-01],
[ 1.4613e-01],
[ 1.2263e-01],
[-1.5566e-01],
[ 1.4740e-01],
[-9.9924e-01],
[ 2.0878e-01],
[-2.0074e-01],
[ 7.8383e-02],
[ 7.4679e-02],
[-5.8065e-01],
[ 6.7777e-01],
[ 5.9879e-01],
[ 6.6301e-01],
[-4.7051e-01],
[-2.5468e-01],
[-2.7382e-01],
[ 1.7585e-01],
[ 3.6151e-01],
[-9.2532e-01],
[-1.6999e-01],
[ 8.4971e-02],
[-6.6083e-01],
[-3.1204e-02],
[ 6.3712e-01],
[-5.8580e-02],
[-7.7901e-04],
[-4.6792e-01],
[ 1.0796e-01],
[ 7.8766e-01],
[ 1.6809e-01],
[-7.0058e-01],
[-2.9299e-01],
[-8.2735e-02],
[ 2.0875e-01],
[-2.9426e-01],
[-7.6748e-02],
[-1.5762e-01],
[-5.7432e-01],
[-5.2042e-01],
[-1.5152e-01],
[ 1.4119e+00],
[-1.5752e-01],
[-3.0565e-01],
[-5.1378e-01],
[-5.8924e-01],
[-1.0163e+00],
[-2.2021e-01],
[ 2.9112e-02],
[ 1.8521e-01],
[ 6.2814e-01],
[-6.8793e-01],
[ 2.1395e-02],
[ 5.7168e-01],
[ 9.0977e-01],
[ 3.8899e-01],
[ 3.0209e-01],
[ 2.4655e-01],
[-1.1688e-01],
[-5.9835e-02],
[ 3.6426e-02],
[-5.2782e-01],
[ 1.4604e+00],
[ 2.9685e-01],
[-2.4077e-01],
[ 1.0163e+00],
[ 6.9770e-01],
[-2.6183e-01],
[ 3.6770e-01],
[ 3.6535e-03],
[ 4.2364e-01],
[-5.4703e-01],
[ 8.9173e-02],
[-3.9032e-01],
[-5.9740e-01],
[ 3.7479e-02],
[ 3.0257e-01],
[ 8.2539e-02],
[-6.0559e-01],
[-4.3660e-01],
[-7.0624e-01],
[-5.0503e-01],
[-4.0929e-01],
[-2.3300e-01],
[ 2.0298e-01],
[-6.3697e-01],
[-1.2584e-01],
[ 5.6092e-02],
[ 5.0150e-02],
[-1.5358e-01],
[ 2.9248e-02],
[ 1.1180e-01],
[-1.5535e-01],
[ 1.1964e-01],
[-6.5698e-01],
[ 4.1923e-01],
[ 7.4044e-02],
[ 2.4536e-02],
[ 3.2647e-01],
[-7.7464e-01],
[ 3.9898e-01],
[-2.5777e-01],
[ 8.5569e-02],
[-4.0305e-01],
[ 5.4463e-01],
[-3.4124e-01],
[-4.0789e-01],
[ 4.2093e-01],
[-3.8487e-01],
[-4.0491e-01],
[-2.1539e-01],
[-1.7979e-02],
[ 3.2492e-01],
[-2.0894e-01],
[ 2.5629e-01],
[ 9.6046e-01]], device='cuda:0', grad_fn=<AddmmBackward0>)
That tensor has a singleton dimension (ie. it's of shape [Nx1]). Just squeeze that dimension or pick the 0th element:
In [1]: import torch
In [2]: a = torch.zeros([10,1])
In [3]: a
Out[3]:
tensor([[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.]])
In [4]: a[:,0]
Out[4]: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [5]: a.squeeze(1)
Out[5]: tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
if i understand your question right you can use the flatten method
input:
tvalue=torch.tensor([[-5.6117e-01],
[ 3.5726e-01],
[-2.5853e-01],
[-4.8641e-01],
[-1.0581e-01],
[-1.8322e-01],
[-1.2732e+00],
[-5.9760e-02],
[ 1.2819e-01],
[ 6.3894e-02],
[-9.1817e-01],
[-1.6539e-01],
[-1.1471e+00],
[ 1.9666e-01],
[-6.3297e-01],
[-4.0876e-01],
[-2.4590e-02],
[ 2.7065e-01],
[ 3.5308e-01],
[-4.6348e-01],
[-4.1755e-01],
[-1.1554e-01],
[-4.2062e-01],
[ 1.4067e-01],
[-2.9788e-01],
[-7.4582e-02],
[-5.3751e-01],
[ 1.1344e-01],
[-2.6100e-01],
[ 2.6951e-02],
[-5.0437e-02],
[-1.9163e-01],
[-3.3893e-02],
[-5.9640e-01],
[-1.1574e-01],
[ 1.4613e-01],
[ 1.2263e-01],
[-1.5566e-01],
[ 1.4740e-01],
[-9.9924e-01],
[ 2.0878e-01],
[-2.0074e-01],
[ 7.8383e-02],
[ 7.4679e-02],
[-5.8065e-01],
[ 6.7777e-01],
[ 5.9879e-01],
[ 6.6301e-01],
[-4.7051e-01],
[-2.5468e-01],
[-2.7382e-01],
[ 1.7585e-01],
[ 3.6151e-01],
[-9.2532e-01],
[-1.6999e-01],
[ 8.4971e-02],
[-6.6083e-01],
[-3.1204e-02],
[ 6.3712e-01],
[-5.8580e-02],
[-7.7901e-04],
[-4.6792e-01],
[ 1.0796e-01],
[ 7.8766e-01],
[ 1.6809e-01],
[-7.0058e-01],
[-2.9299e-01],
[-8.2735e-02],
[ 2.0875e-01],
[-2.9426e-01],
[-7.6748e-02],
[-1.5762e-01],
[-5.7432e-01],
[-5.2042e-01],
[-1.5152e-01],
[ 1.4119e+00],
[-1.5752e-01],
[-3.0565e-01],
[-5.1378e-01],
[-5.8924e-01],
[-1.0163e+00],
[-2.2021e-01],
[ 2.9112e-02],
[ 1.8521e-01],
[ 6.2814e-01],
[-6.8793e-01],
[ 2.1395e-02],
[ 5.7168e-01],
[ 9.0977e-01],
[ 3.8899e-01],
[ 3.0209e-01],
[ 2.4655e-01],
[-1.1688e-01],
[-5.9835e-02],
[ 3.6426e-02],
[-5.2782e-01],
[ 1.4604e+00],
[ 2.9685e-01],
[-2.4077e-01],
[ 1.0163e+00],
[ 6.9770e-01],
[-2.6183e-01],
[ 3.6770e-01],
[ 3.6535e-03],
[ 4.2364e-01],
[-5.4703e-01],
[ 8.9173e-02],
[-3.9032e-01],
[-5.9740e-01],
[ 3.7479e-02],
[ 3.0257e-01],
[ 8.2539e-02],
[-6.0559e-01],
[-4.3660e-01],
[-7.0624e-01],
[-5.0503e-01],
[-4.0929e-01],
[-2.3300e-01],
[ 2.0298e-01],
[-6.3697e-01],
[-1.2584e-01],
[ 5.6092e-02],
[ 5.0150e-02],
[-1.5358e-01],
[ 2.9248e-02],
[ 1.1180e-01],
[-1.5535e-01],
[ 1.1964e-01],
[-6.5698e-01],
[ 4.1923e-01],
[ 7.4044e-02],
[ 2.4536e-02],
[ 3.2647e-01],
[-7.7464e-01],
[ 3.9898e-01],
[-2.5777e-01],
[ 8.5569e-02],
[-4.0305e-01],
[ 5.4463e-01],
[-3.4124e-01],
[-4.0789e-01],
[ 4.2093e-01],
[-3.8487e-01],
[-4.0491e-01],
[-2.1539e-01],
[-1.7979e-02],
[ 3.2492e-01],
[-2.0894e-01],
[ 2.5629e-01],
[ 9.6046e-01]])
output
tvalue.flatten()
tensor([-5.6117e-01, 3.5726e-01, -2.5853e-01, -4.8641e-01, -1.0581e-01,
-1.8322e-01, -1.2732e+00, -5.9760e-02, 1.2819e-01, 6.3894e-02,
-9.1817e-01, -1.6539e-01, -1.1471e+00, 1.9666e-01, -6.3297e-01,
-4.0876e-01, -2.4590e-02, 2.7065e-01, 3.5308e-01, -4.6348e-01,
-4.1755e-01, -1.1554e-01, -4.2062e-01, 1.4067e-01, -2.9788e-01,
-7.4582e-02, -5.3751e-01, 1.1344e-01, -2.6100e-01, 2.6951e-02,
-5.0437e-02, -1.9163e-01, -3.3893e-02, -5.9640e-01, -1.1574e-01,
1.4613e-01, 1.2263e-01, -1.5566e-01, 1.4740e-01, -9.9924e-01,
2.0878e-01, -2.0074e-01, 7.8383e-02, 7.4679e-02, -5.8065e-01,
6.7777e-01, 5.9879e-01, 6.6301e-01, -4.7051e-01, -2.5468e-01,
-2.7382e-01, 1.7585e-01, 3.6151e-01, -9.2532e-01, -1.6999e-01,
8.4971e-02, -6.6083e-01, -3.1204e-02, 6.3712e-01, -5.8580e-02,
-7.7901e-04, -4.6792e-01, 1.0796e-01, 7.8766e-01, 1.6809e-01,
-7.0058e-01, -2.9299e-01, -8.2735e-02, 2.0875e-01, -2.9426e-01,
-7.6748e-02, -1.5762e-01, -5.7432e-01, -5.2042e-01, -1.5152e-01,
1.4119e+00, -1.5752e-01, -3.0565e-01, -5.1378e-01, -5.8924e-01,
-1.0163e+00, -2.2021e-01, 2.9112e-02, 1.8521e-01, 6.2814e-01,
-6.8793e-01, 2.1395e-02, 5.7168e-01, 9.0977e-01, 3.8899e-01,
3.0209e-01, 2.4655e-01, -1.1688e-01, -5.9835e-02, 3.6426e-02,
-5.2782e-01, 1.4604e+00, 2.9685e-01, -2.4077e-01, 1.0163e+00,
6.9770e-01, -2.6183e-01, 3.6770e-01, 3.6535e-03, 4.2364e-01,
-5.4703e-01, 8.9173e-02, -3.9032e-01, -5.9740e-01, 3.7479e-02,
3.0257e-01, 8.2539e-02, -6.0559e-01, -4.3660e-01, -7.0624e-01,
-5.0503e-01, -4.0929e-01, -2.3300e-01, 2.0298e-01, -6.3697e-01,
-1.2584e-01, 5.6092e-02, 5.0150e-02, -1.5358e-01, 2.9248e-02,
1.1180e-01, -1.5535e-01, 1.1964e-01, -6.5698e-01, 4.1923e-01,
7.4044e-02, 2.4536e-02, 3.2647e-01, -7.7464e-01, 3.9898e-01,
-2.5777e-01, 8.5569e-02, -4.0305e-01, 5.4463e-01, -3.4124e-01,
-4.0789e-01, 4.2093e-01, -3.8487e-01, -4.0491e-01, -2.1539e-01,
-1.7979e-02, 3.2492e-01, -2.0894e-01, 2.5629e-01, 9.6046e-01])
I have the following xy numpy array which represents the locations of the vertices of some triangles:
array([[[ 0.30539728, 49.82845203],
[ 0.67235022, 49.95042185],
[ 0.268982 , 49.95195348]],
[[ 0.268982 , 49.95195348],
[ 0.67235022, 49.95042185],
[ 0.27000135, 50.16334035]],
...
[[ 1.00647459, 50.25958169],
[ 0.79479121, 50.3010079 ],
[ 0.67235022, 49.95042185]],
[[ 0.79479121, 50.3010079 ],
[ 0.6886783 , 50.25867683],
[ 0.67235022, 49.95042185]]])
Here, it's an array of shape (10, 3, 2) but it could as well be (5, 3, 2) or (18, 3, 2), you name it. In any case it's of shape (N, 3, 2).
I have another numpy array to_replace of shape (4, 2) but it could as well be (6, 2) or (7, 2), but always of shape (M, 2):
array([[ 1.08267406, 49.88690993],
[ 1.1028248 , 50.01440407],
[ 0.74114309, 49.73183549],
[ 1.08267406, 49.88690993]])
It represents the locations of pairs of coordinates that can be found in my first array. Note that each of these pairs is present at least once in xy but could be present more than once.
Finally, I have a third array replace_by of which shape (8,) (or of shape (M*2) based on the indication above) and which values are meant to replace exactly those contained in to_replace in my first xy array. It looks like this:
array([ 0.87751214, 49.91866589, 0.88758751, 49.98241296, 0.70674665, 49.84112867, 0.87751214, 49.91866589])
So basically all pairs [1.08267406, 49.88690993] in xy should be replaced by [0.87751214, 49.91866589] for example.
My current code looks like this but it works only if to_replace and replace_by are strictly of shape (2, 2).
indices = (xy == to_replace[:, None][:, None])[0]
xy[indices] = replace_by
I already looked at a number of answers and actually got inspired by some of them but I still can't get it to work.
You can use numpy.isclose to compare rows and then use .all(axis=2) to find where all last rows are the same. Numpy will broadcast each row to fit xy shape.
import numpy as np
xy = np.array([[[ 0.30539728, 49.82845203],
[ 0.67235022, 49.95042185],
[ 0.268982 , 49.95195348]],
[[ 0.268982 , 49.95195348],
[ 0.67235022, 49.95042185],
[ 0.27000135, 50.16334035]],
[[ 1.00647459, 50.25958169],
[ 0.79479121, 50.3010079 ],
[ 0.67235022, 49.95042185]],
[[ 0.79479121, 50.3010079 ],
[ 0.6886783 , 50.25867683],
[ 0.67235022, 49.95042185]]])
xy_start = xy.copy()
to_replace = np.array([[ 1.08267406, 49.88690993],
[ 1.1028248 , 50.01440407],
# [ 0.74114309, 49.73183549],
[ 0.6886783 , 50.25867683],
[ 1.08267406, 49.88690993]])
replace_by = np.array([ 0.87751214, 49.91866589, 0.88758751, 49.98241296, 0.70674665, 49.84112867, 0.87751214, 49.91866589])
replace_by_reshaped = replace_by.reshape(-1, 2)
for i, row in enumerate(to_replace):
xy[np.isclose(xy, row).all(axis=2)] = replace_by_reshaped[i]
print(xy_start)
# [[[ 0.30539728 49.82845203]
# [ 0.67235022 49.95042185]
# [ 0.268982 49.95195348]]
# [[ 0.268982 49.95195348]
# [ 0.67235022 49.95042185]
# [ 0.27000135 50.16334035]]
# [[ 1.00647459 50.25958169]
# [ 0.79479121 50.3010079 ]
# [ 0.67235022 49.95042185]]
# [[ 0.79479121 50.3010079 ]
# [ 0.6886783 50.25867683]
# [ 0.67235022 49.95042185]]]
print(xy)
# [[[ 0.30539728 49.82845203]
# [ 0.67235022 49.95042185]
# [ 0.268982 49.95195348]]
# [[ 0.268982 49.95195348]
# [ 0.67235022 49.95042185]
# [ 0.27000135 50.16334035]]
# [[ 1.00647459 50.25958169]
# [ 0.79479121 50.3010079 ]
# [ 0.67235022 49.95042185]]
# [[ 0.79479121 50.3010079 ]
# [ 0.70674665 49.84112867]
# [ 0.67235022 49.95042185]]]
EDIT
.all(axis=2) shrink axis=2 to True if all values along axis=2 are True and False else. I think little 2d example made it clear what is happening here.
>>> import numpy as np
>>> a = np.array([[0, 1], [0, 2], [3, 4]])
>>> a
array([[0, 1],
[0, 2],
[3, 4]])
>>> np.isclose(a, [0, 1])
array([[ True, True],
[ True, False],
[False, False]])
>>> np.isclose(a, [0, 1]).all(axis=1)
array([ True, False, False])
>>> a[np.isclose(a, [0, 1]).all(axis=1)]
array([[0, 1]])
>>> a[np.isclose(a, [0, 1]).all(axis=1)] = [12, 14]
>>> a
array([[12, 14],
[ 0, 2],
[ 3, 4]])
The numpy-indexed package (disclaimer: I am its author) contains functionality to solve this problem in a vectorized and elegant manner.
Given the arrays as you have defined them, this one-liner should do the trick:
import numpy_indexed as npi
npi.remap(xy.reshape(-1, 2), to_replace, replace_by.reshape(-1, 2)).reshape(-1, 3, 2)
I have a list of lists like
list_ABC = [[A,B,C], [A,B,C], ...]
with 2D ndarrays A (2x2), B (2x3) and C (2x3).
Now, I'd like to convert the main list to a numpy array:
np.array(list_ABC)
However, I get the following error:
ValueError: could not broadcast input array from shape (2,2) into shape (2)
I need this conversion because I'd like to get
A_matrices = np.array(list_ABC)[:, 0]
B_matrices = np.array(list_ABC)[:, 1]
Such that I can finally obtain a ndarray containing all A-arrays (array(A,A,A,...)).
Unfortunately I can't get a clue from the value error message. Interestingly, if I only transpose matrix C with C.T (making it a 3x2 matrix) no error is thrown.
Now, I could solve the problem by creating a list_A, list_B, list_C beforehand (and not list_ABC), but this doesn't feel as simple (constructing and appending to each list_A/B/C requires a few more lines of code). Similarly I could use other methods (e.g. using a dict with A,B,C keys containing a list of all A/B/C matrices), but nothing feels so simple like this solution.
A working example which throws the error:
import numpy as np
list = [[np.array([[ 476., 667.], [ 474., 502.]]), np.array([[ 343., 351., 449.], [ 352., 332., 292.]]), np.array([[ 328., 328., 294.], [ 367., 355., 447.]])], [np.array([[ 497., 546.], [ 456., 517.]]), np.array([[ 361., 342., 340.], [ 341., 304., 328.]]), np.array([[ 347., 313., 293.], [ 355., 333., 375.]])]]
np.array(list)
Thanks a lot!
When constructing an array from arrays, np.array function can do 3 things:
if all subarrays have the same shape it will make a higher dimensional array
if the subarrays differ in shape it may construct an object dtype array. This is like a list, or nested list, but with the ability to index and reshape like arrays
raise an error. This seems to happen most when rows match but columns don't. It may be detecting the shape mismatch too late to fall back on the object dtype solution.
More on this at:
How to keep numpy from broadcasting when creating an object array of different shaped arrays
To get array(A,A,A,...) I would suggest use list comprehension. Constructing object dtype arrays is too tricky.
The reliable way of creating an object array is to initialize an empty one and fill it:
In [116]: arr = np.empty((2,3), dtype=object)
In [117]: arr[...] = alist
In [118]: arr
Out[118]:
array([[array([[ 476., 667.],
[ 474., 502.]]),
array([[ 343., 351., 449.],
[ 352., 332., 292.]]),
array([[ 328., 328., 294.],
[ 367., 355., 447.]])],
[array([[ 497., 546.],
[ 456., 517.]]),
array([[ 361., 342., 340.],
[ 341., 304., 328.]]),
array([[ 347., 313., 293.],
[ 355., 333., 375.]])]], dtype=object)
Now I can select the 'A' elements:
In [119]: arr[:,0]
Out[119]:
array([array([[ 476., 667.],
[ 474., 502.]]),
array([[ 497., 546.],
[ 456., 517.]])], dtype=object)
but that's an object array, and wrapping again in np.array doesn't change that:
In [120]: np.array(arr[:,0])
Out[120]:
array([array([[ 476., 667.],
[ 474., 502.]]),
array([[ 497., 546.],
[ 456., 517.]])], dtype=object)
but they can be concatenated on several different axes.
In [121]: np.stack(arr[:,0])
Out[121]:
array([[[ 476., 667.],
[ 474., 502.]],
[[ 497., 546.],
[ 456., 517.]]])
But I can get the same thing without the object array step
In [123]: np.stack([a[0] for a in alist])
Out[123]:
array([[[ 476., 667.],
[ 474., 502.]],
[[ 497., 546.],
[ 456., 517.]]])
I need to reshape/resize(pivot?) [sorry I am fairly new to numpy, working with it about 6 weeks] an numpy array based on column.
The source numpy array is this:
[[[-0.98261404]
[-0.98261404]
[-0.95991508]
...,
[-0.92496699]
[-0.92731224]
[-0.926328 ]]
[[-0.91894622]
[-0.91894622]
[-0.92171439]
...,
[-1.02966519]
[-1.03908464]
[-1.03527072]]
[[-0.92201427]
[-0.92201427]
[-0.93004196]
...,
[-1.06750448]
[-1.07838491]
[-1.07398661]]
[[-0.9233676 ]
[-0.9233676 ]
[-0.93250255]
...,
[-1.07617807]
[-1.08736608]
[-1.08284474]]
[[-0.91913077]
[-0.91913077]
[-0.92023803]
...,
[-1.01886934]
[-1.02782743]
[-1.02419806]]]
I like to reshape/resize(pivot?) above as follows:
[[[-0.98261404]
[-0.91894622]
[-0.92201427]
[-0.9233676 ]
[-0.91913077]]
...,
[[-0.926328 ]
[-1.03527072]
[-1.07398661]
[-1.08284474]
[-1.02419806]]]
What is the best way to do this?
Thanks!
I believe what you want is (considering I did understood it properly):
>>> B = np.transpose(A, (0, 2, 1))
being A your data and B the resulting array. That will transpose/pivot the last 2 axes. Alternatively, you can write
>>> B = np.swapaxes(A, 1, 2)
which is equivalent (and probably easier to read). Extended for an N-dimensional arrays:
>>> B = np.swapaxes(A, a, b) # being `a` and `b` the axes
An example:
>>> import numpy as np
>>> A = np.random.rand(1, 2, 3)
>>> A
array([[[ 0.54766263, 0.95017886, 0.32949198],
[ 0.76255173, 0.88943131, 0.78594731]]])
>>> np.swapaxes(A, 1, 2)
array([[[ 0.54766263, 0.76255173],
[ 0.95017886, 0.88943131],
[ 0.32949198, 0.78594731]]])
Alternatively, you can just transpose the array:
>>> A.T # equivalent to np.transpose(A, (2, 1, 0))
array([[[ 0.54766263],
[ 0.76255173]],
[[ 0.95017886],
[ 0.88943131]],
[[ 0.32949198],
[ 0.78594731]]])
Reordering the dimensions in the opposite order (2, 1, 0).
Let's say your array is a.
Then this should do the trick :
x,y,z = a.shape
b = a.T #Transpose - get the indices grouped along the other axis
b = b.reshape(y, x, z) #Interchange the axes.
For example :
In [58]: a = np.random.random(20)
In [59]: a = a.reshape(4,5,1)
In [60]: a
Out[60]:
array([[[ 0.40906066],
[ 0.57160002],
[ 0.22642471],
[ 0.35845352],
[ 0.26999423]],
[[ 0.91962882],
[ 0.62664991],
[ 0.21286972],
[ 0.39995373],
[ 0.1141539 ]],
[[ 0.03040894],
[ 0.79666903],
[ 0.72822631],
[ 0.84388555],
[ 0.23265895]],
[[ 0.63548896],
[ 0.50314843],
[ 0.88547892],
[ 0.49824574],
[ 0.55835843]]])
In [61]: b = b.reshape(y, x, z)
In [62]: x,y,z = a.shape
In [63]: b = a.T
In [64]: b = b.reshape(y,x,z)
In [65]: b
Out[65]:
array([[[ 0.40906066],
[ 0.91962882],
[ 0.03040894],
[ 0.63548896]],
[[ 0.57160002],
[ 0.62664991],
[ 0.79666903],
[ 0.50314843]],
[[ 0.22642471],
[ 0.21286972],
[ 0.72822631],
[ 0.88547892]],
[[ 0.35845352],
[ 0.39995373],
[ 0.84388555],
[ 0.49824574]],
[[ 0.26999423],
[ 0.1141539 ],
[ 0.23265895],
[ 0.55835843]]])