HomeLanguagesnumpy.require() in Python

numpy.require() in Python

numpy.require() function is useful for the array when correct flags is returned satisfies requirements for passing to compiled code (perhaps through ctypes).

Syntax: numpy.require(a, dtype=None, requirements=None)

Parameters:

a :  array_like

dtype : data-type

requirements : str or list of str 

The requirements list can be any of the following.

  • ‘F’ : ‘F_CONTIGUOUS’  – ensure a Fortran-contiguous array.
  • ‘C’  : ‘C_CONTIGUOUS’ – ensure a C-contiguous array.
  • ‘A’ : ‘ALIGNED’  – ensure a data-type aligned array.
  • ‘W’ : ‘WRITABLE’  – ensure a writable array.
  • ‘O’ : ‘OWNDATA’ – ensure an array that owns its own data.
  • ‘E’ : ‘ENSUREARRAY’ – ensure a base array, instead of a subclass.

Returns :  ndarray

Exception : ValueError – Raises ValueError

Code #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


Output:

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

Code #2:

Python3




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


Output:

C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32705 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7076 POSTS0 COMMENTS
Nicole Veronica
12212 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12303 POSTS0 COMMENTS
Shaida Kate Naidoo
7198 POSTS0 COMMENTS
Ted Musemwa
7457 POSTS0 COMMENTS
Thapelo Manthata
7175 POSTS0 COMMENTS
Umr Jansen
7162 POSTS0 COMMENTS