# NumPy require() Method

Last Updated : 01 Feb, 2024

The NumPy’s require() method returns a ndarray that satisfies certain requirements.

The numpy.require() method is useful for ensuring that an array has the correct flags, thereby satisfying the requirements for passing it to compiled code, possibly through ctypes.

## Syntax

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

## Examples

Let’s look at some examples to understand require() method of the NumPy library in Python.

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

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

Article Tags :
Practice Tags :