Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

python - Rearranging a 4d numpy array

I have a 4d numpy array which represents a dataset with 3d instances. Lets say that the shape of the array is (32, 32, 3, 73257).

How can i change the shape of the array to (73257, 32, 32, 3)?

--- Question update It seems that both rollaxis and transpose do the trick.

Thanx for replying!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The np.transpose function does exactly what you want, you can pass an axis argument which controls which axis you want to swap:

a = np.empty((32, 32, 3, 73257))
b = np.transpose(a, (3, 0, 1, 2))

The axis of b are permuted versions of the ones of a: the axis 0 of b is the 3-rd axis of a, the axis 1 of b is the 0-th axis of a, etc...

That way, you can specify which of the axis of size 32 you want in second or in third place:

b = np.transpose(a, (3, 1, 0, 2))

Also gives an array of the desired shape, but is different from the previous one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...