什么是连续和非连续的阵列之间的区别? [英] What is the difference between contiguous and non-contiguous arrays?

查看:1086
本文介绍了什么是连续和非连续的阵列之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html\"> numpy的有关重塑()函数手动的

In the numpy manual about the reshape() function, it says

>>> a = np.zeros((10, 2))
# A transpose make the array non-contiguous
>>> b = a.T
# Taking a view makes it possible to modify the shape without modifying the
# initial object.
>>> c = b.view()
>>> c.shape = (20)
AttributeError: incompatible shape for a non-contiguous array

我的问题是:


  1. 什么是连续的,不连续的阵列?它是类似于C连续内存块,如什么是一个连续的内存块?

  2. 是否有这两个之间的性能差异?我们什么时候应该使用一个或其他?

  3. 为什么转做阵列非连续的?

  4. 为什么 c.shape =(20)抛出一个错误不相容的形状不连续的阵列

  1. What are continuous and noncontiguous arrays? Is it similar to the contiguous memory block in C like What is a contiguous memory block?
  2. Is there any performance difference between these two? When should we use one or the other?
  3. Why does transpose make the array non-contiguous?
  4. Why does c.shape = (20) throws an error incompatible shape for a non-contiguous array?

谢谢您的回答!

推荐答案

一个连续的数组只是存储在内存中一个完整的块数组:数组中访问下一个值,我们只是移动到下一个内存地址。

A contiguous array is just an array stored in an unbroken block of memory: to access the next value in the array, we just move to the next memory address.

考虑二维数组 ARR = np.arange(12).reshape(3,4)。它看起来是这样的:

Consider the 2D array arr = np.arange(12).reshape(3,4). It looks like this:

在计算机的内存,的改编的值存储这样的:

In the computer's memory, the values of arr are stored like this:

这意味着改编ç连续阵列,因为的的存储为连续的内存块。接下来的内存地址持有的下一行的价值。要向下移动列,内存三个区块都被跳过。

This means arr is a C contiguous array because the rows are stored as contiguous blocks of memory. The next memory address hold the next row value. To move down a column, three blocks of memory have to be skipped.

移调与 arr.T 数组是指将c连续性丢失,因为相邻的行条目相邻的内存地址不再。然而, arr.T 的Fortran连续自的的是在连续的内存块:

Transposing the array with arr.T means that C contiguity is lost because adjacent row entries are no longer in adjacent memory addresses. However, arr.T is Fortran contiguous since the columns are in contiguous blocks of memory:

性能方面,最好有连续的阵列,因为访问它们相邻的内存地址通常快于访问这是更S $ P $垫出跨内存地址。这意味着,在连续的数组操作往往会更快。

Performance-wise, it's better to have contiguous arrays because accessing memory addresses which are next to each other is often faster than accessing addresses which are more "spread out" across memory. This means that operations over contiguous arrays will often be quicker.

形式为C连续的内存布局的结果,行操作通常比列方式的运算速度更快。例如,你通常会发现,

As a consequence of C contiguous memory layout, row-wise operations are usually faster than column-wise operations. For example, you'll typically find that

np.sum(arr, axis=1) # sum the rows

略快于:

np.sum(arr, axis=0) # sum the columns

同样,在列的操作将是连续的Fortran数组稍快。

Similarly, operations on columns will be slightly faster for Fortran contiguous arrays.

最后,我们为什么不能分配一个新的形状扁平化的Fortran连续的阵列?

Finally, why can't we flatten the Fortran contiguous array by assigning a new shape?

>>> arr2 = arr.T
>>> arr2.shape = 12
AttributeError: incompatible shape for a non-contiguous array

在为了使这成为可能numpy的将不得不把 arr.T 的排在一起,就像这样:

In order for this to be possible NumPy would have to put the rows of arr.T together like this:

(设置形状属性直接假定Ç秩序 - 即numpy的尝试逐行执行操作)

(Setting the shape attribute directly assumes C order - i.e. NumPy tries to perform the operation row-wise.)

这是不可能做到的。对于任何轴,numpy的需要具有的恒定的步幅长度(字节动议的数量)来获得到阵列的下一个元素。压扁 arr.T 以这种方式需要跳过向前和向后的内存来检索数组的连续值。

This is impossible to do. For any axis, NumPy needs to have a constant stride length (the number of bytes to move) to get to the next element of the array. Flattening arr.T in this way would require skipping forwards and backwards in memory to retrieve consecutive values of the array.

如果我们写了 arr2.reshape(12)代替,numpy的将ARR2的值复制到一个新的内存块(因为它不能返回视图对于这种形状的原始数据)。

If we wrote arr2.reshape(12) instead, NumPy would copy the values of arr2 into a new block of memory (since it can't return a view on to the original data for this shape).

这篇关于什么是连续和非连续的阵列之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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