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

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

问题描述

在[28]中:arr = np.arange(16).reshape((2, 2, 4))在 [29]: arr出[29]:数组([[[ 0, 1, 2, 3],[ 4, 5, 6, 7]],[[ 8, 9, 10, 11],[12, 13, 14, 15]]])在 [32]: arr.transpose((1, 0, 2))出[32]:数组([[[ 0, 1, 2, 3],[ 8, 9, 10, 11]],[[ 4, 5, 6, 7],[12, 13, 14, 15]]])

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

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

解决方案

为了转置数组,NumPy 只是交换每个轴的形状和步幅信息.以下是步骤:

<预><代码>>>>arr.strides(64, 32, 8)>>>arr.transpose(1, 0, 2).strides(32, 64, 8)

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

无需复制数据即可实现此目的;NumPy 可以简单地改变它对底层内存的看法来构造新数组.

<小时>

可视化步伐

stride 值表示为了到达数组轴的下一个值必须在内存中移动的字节数.

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

这个数组存储在一个

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

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

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

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

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

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

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?

解决方案

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)

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

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.


Visualising strides

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.

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

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:

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.

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

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:

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天全站免登陆