numpy列出第二个轴 [英] Numpy to list over 2nd axis

查看:98
本文介绍了numpy列出第二个轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于内轴拆分n-d numpy数组.

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

我有一个形状为(6,150,29,29,29,1)

我想要一个数组列表--[150 arrays of shape (6,29,29,29,1)]

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

我已经使用过list(a),但是这给了我0轴的列表.

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

推荐答案

arr.transpose(1,0,2,3,4,5)np.swapaxes(arr,0,1)将150维放在第一位.然后,您可以使用list.

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)]

转置效果更好

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)

这篇关于numpy列出第二个轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆