转置数组并实际对内存重新排序 [英] Transpose array and actually reorder memory

查看:89
本文介绍了转置数组并实际对内存重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3-D NumPy数组,例如

I have a 3-D NumPy array, e.g.

a = np.random.random((2,3,5))

我想转置最后两个轴,即

I would like to transpose the last two axes, i.e.

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

但是,我不希望大步向前走!我想实际复制数组,然后在内存中对其重新排序.实现此目标的最佳方法是什么?

However, I do not want a view with twiddled strides! I want to actually copy the array and reorder it in memory. What is the best way to achieve this?

推荐答案

默认情况下,copy()方法将重新排序为C连续顺序:

The copy() method will reorder to C-contiguous order by default:

b = a.transpose(0,2,1).copy()

这篇关于转置数组并实际对内存重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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