📜  Python中的 numpy.require()

📅  最后修改于: 2022-05-13 01:54:27.601000             🧑  作者: Mango

Python中的 numpy.require()

当返回正确的标志满足传递给编译代码的要求(可能通过 ctypes)时, numpy.require()函数对数组很有用。

代码#1:

Python3
# Python program explaining
# numpy.require() function
  
# importing numpy
import numpy as np
  
# creating 4 x 4 array
data = np.arange(16).reshape(4, 4)
  
data.flags


Python3
import numpy as np
  
# Python program explaining
# numpy.require()
b = np.require(data, dtype=np.float32,
               requirements=['A', 'W', 'O', 'C'])
b.flags


输出:

C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

代码#2:

蟒蛇3

import numpy as np
  
# Python program explaining
# numpy.require()
b = np.require(data, dtype=np.float32,
               requirements=['A', 'W', 'O', 'C'])
b.flags

输出:

C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False