# np.ascontiguousarray()详解

1、ascontiguousarray函数将一个内存不连续存储的数组转换为内存连续存储的数组，使得运行速度更快。

import numpy as np
arr = np.arange(12).reshape(3,4)
flags = arr.flags
print("",arr)
print(flags)

output:

 [[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False

import numpy as np
arr = np.arange(12).reshape(3,4)
arr1 = arr.transpose(1,0)
flags = arr1.flags
print("",arr1)
print(flags)

output:

 [[ 0  4  8]
[ 1  5  9]
[ 2  6 10]
[ 3  7 11]]
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False

import numpy as np
arr = np.arange(12).reshape(3,4)
arr1 = arr[:,0:2]
flags = arr1.flags
print("",arr1)
print(flags)

output:

 [[0 1]
[4 5]
[8 9]]
C_CONTIGUOUS : False
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False

import numpy as np
arr = np.arange(12).reshape(3,4)
arr1 = arr[:,0:2]
arr2 = np.ascontiguousarray(arr1)
flags = arr2.flags
print("",arr2)
print(flags)

output:

[[0 1]
[4 5]
[8 9]]
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
C_CONTIGUOUS : True
C_CONTIGUOUS：真
posted @ 2020-08-07 21:32  九叶草  阅读(21472)  评论(0编辑  收藏  举报