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
457 views
in Technique[技术] by (71.8m points)

python - Numpy to list over 2nd axis

I would like to split a n-d numpy array based on a internal axis.

I have a array of shape (6,150,29,29,29,1)

I would like a list of arrays as - [150 arrays of shape (6,29,29,29,1)]

I have used the list(a), but this has given me a list over axis 0.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

arr.transpose(1,0,2,3,4,5) or np.swapaxes(arr,0,1) put the 150 dimension first. Then you can use list.

Or you could use a list comprehension

[a[:,i] for i in range(150)]

The transpose is somewhat better

In [28]: timeit list(arr.transpose(1,0,2,3,4,5))
47.7 μs ± 47.1 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [29]: timeit [arr[:,i] for i in range(150)]
88.7 μs ± 22.2 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [32]: timeit list(np.swapaxes(arr,0,1))
49.2 μs ± 51.1 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

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

...