NumPy的transpose()方法如何排列数组的轴? [英] How does NumPy's transpose() method permute the axes of an array?

查看:81
本文介绍了NumPy的transpose()方法如何排列数组的轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In [28]: arr = np.arange(16).reshape((2, 2, 4))

In [29]: arr
Out[29]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]]])


In [32]: arr.transpose((1, 0, 2))
Out[32]: 
array([[[ 0,  1,  2,  3],
        [ 8,  9, 10, 11]],

       [[ 4,  5,  6,  7],
        [12, 13, 14, 15]]])

当我们将整数元组传递给transpose()函数时,会发生什么?

When we pass a tuple of integers to the transpose() function, what happens?

具体来说,这是一个3D数组:当我传递轴(1, 0 ,2)的元组时,NumPy如何变换该数组?您能解释这些整数指的是哪行或哪一列?在NumPy的上下文中,轴号是什么?

To be specific, this is a 3D array: how does NumPy transform the array when I pass the tuple of axes (1, 0 ,2)? Can you explain which row or column these integers refer to? And what are axis numbers in the context of NumPy?

推荐答案

要转置数组,NumPy只是交换每个轴的形状和步幅信息.这是大步向前:

To transpose an array, NumPy just swaps the shape and stride information for each axis. Here are the strides:

>>> arr.strides
(64, 32, 8)

>>> arr.transpose(1, 0, 2).strides
(32, 64, 8)

请注意,转置操作交换了轴0和轴1的步幅.这些轴的长度也被交换了(在此示例中,两个轴的长度均为2).

Notice that the transpose operation swapped the strides for axis 0 and axis 1. The lengths of these axes were also swapped (both lengths are 2 in this example).

无需复制任何数据即可完成此操作; NumPy可以简单地更改其在底层内存中的外观以构造新数组.

No data needs to be copied for this to happen; NumPy can simply change how it looks at the underlying memory to construct the new array.

跨度值表示为了到达数组轴的下一个值而必须在内存中传输的字节数.

The stride value represents the number of bytes that must be travelled in memory in order to reach the next value of an axis of an array.

现在,我们的3D数组arr看起来(带有标记的轴):

Now, our 3D array arr looks this (with labelled axes):

此数组存储在连续的内存块中;本质上是一维的.要将其解释为3D对象,NumPy必须跳过一定的恒定字节数才能沿三个轴之一移动:

This array is stored in a contiguous block of memory; essentially it is one-dimensional. To interpret it as a 3D object, NumPy must jump over a certain constant number of bytes in order to move along one of the three axes:

由于每个整数占用8个字节的内存(我们使用的是int64 dtype),因此每个维度的步幅值是需要跳转的值数的8倍.例如,要沿轴1移动,则要跳过四个值(32字节),而要沿轴0移动,则要跳过八个值(64字节).

Since each integer takes up 8 bytes of memory (we're using the int64 dtype), the stride value for each dimension is 8 times the number of values that we need to jump. For instance, to move along axis 1, four values (32 bytes) are jumped, and to move along axis 0, eight values (64 bytes) need to be jumped.

编写arr.transpose(1, 0, 2)时,我们将交换轴0和1.转置后的数组如下所示:

When we write arr.transpose(1, 0, 2) we are swapping axes 0 and 1. The transposed array looks like this:

NumPy所需要做的就是交换轴0和轴1的步幅信息(轴2不变).现在我们必须跳得更远,才能沿着轴1而不是轴0:

All that NumPy needs to do is to swap the stride information for axis 0 and axis 1 (axis 2 is unchanged). Now we must jump further to move along axis 1 than axis 0:

此基本概念适用于数组轴的任何排列.处理转置的实际代码是用C语言编写的,可以在

This basic concept works for any permutation of an array's axes. The actual code that handles the transpose is written in C and can be found here.

这篇关于NumPy的transpose()方法如何排列数组的轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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