C-ordered contiguous
来源:互联网 发布:oncity中山网络社区 编辑:程序博客网 时间:2024/06/06 18:48
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.
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 holds the next row value on that row. If we want to move down a column, we just need to jump over three blocks (e.g. to jump from 0 to 4 means we skip over 1,2 and 3).
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:
Performance-wise, accessing memory addresses which are next to each other is very often faster than accessing addresses which are more "spread out" (fetching a value from RAM could entail a number of neighbouring addresses being fetched and cached for the CPU.) This means that operations over contiguous arrays will often be quicker.
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
is slightly faster than:
np.sum(arr, axis=0) # sum the columns
Similarly, operations on columns will be slightly faster for Fortran contiguous arrays.
Finally, why can't we flatten the Fortran contiguous array by assigning a new shape?
>>> arr2 = arr.T>>> arr2.shape = 12AttributeError: incompatible shape for a non-contiguous array
In order for this to be possible NumPy would have to put the rows of arr.T
together like this:
(Setting the shape
attribute directly assumes C order - i.e. NumPy tries to perform the operation row-wise.)
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.
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).
Maybe this example with 12 different array values will help:
In [207]: x=np.arange(12).reshape(3,4).copy()In [208]: x.flagsOut[208]: C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True ...In [209]: x.T.flagsOut[209]: C_CONTIGUOUS : False F_CONTIGUOUS : True OWNDATA : False ...
The C order
values are in the order that they were generated in. The transposed ones are not
In [212]: x.reshape(12,) # same as x.ravel()Out[212]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])In [213]: x.T.reshape(12,)Out[213]: array([ 0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11])
You can get 1d views of both
In [214]: x1=x.TIn [217]: x.shape=(12,)
the shape of x
can also be changed.
In [220]: x1.shape=(12,)---------------------------------------------------------------------------AttributeError Traceback (most recent call last)<ipython-input-220-cf2b1a308253> in <module>()----> 1 x1.shape=(12,)AttributeError: incompatible shape for a non-contiguous array
But the shape of the transpose cannot be changed. The data
is still in the 0,1,2,3,4...
order, which can't be accessed accessed as 0,4,8...
in a 1d array.
But a copy of x1
can be changed:
In [227]: x2=x1.copy()In [228]: x2.flagsOut[228]: C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True ...In [229]: x2.shape=(12,)
from:
https://stackoverflow.com/questions/26998223/what-is-the-difference-between-contiguous-and-non-contiguous-arrays
- C-ordered contiguous
- Contiguous Array
- Contiguous Array
- HDU 4991 / BC 8C Ordered Subsequence
- 【poj 2533】Longest Ordered Subsequence 题意&题解&代码(C++)
- Contiguous Derived Data
- Maximum Contiguous Mum I
- Contiguous Implementation remove函数
- contiguous/flush/informed
- LeetCode 525. Contiguous Array
- Leetcode 525. Contiguous Array
- 525. Contiguous Array
- 525. Contiguous Array
- 525. Contiguous Array
- [LeetCode]525. Contiguous Array
- 525. Contiguous Array Medium
- 525. Contiguous Array
- 525. Contiguous Array
- TS流分析
- c#运行JS代码
- JAVA动态代理
- 重写eqlus与hashCode方法例子
- 解决Qt5.8加载qt_zh_CN.qm之后QMessageBox仍显示英文的问题
- C-ordered contiguous
- IntelliJ IDEA 开发Web应用图文教程
- Vue CheckBox 全选
- 将EXCEL导到oracle数据库
- RabbitMQ (五)主题(Topic)
- 数值的整数次方
- iframe+js实现页面全屏、淡入淡出切换
- Unity与Android的交互,使用Android Studio导出各Unity工程通用的Android插件包
- js弹出框、对话框、提示框、弹窗总结