Open In App

NumPy require() Method

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Example 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

Example 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


Similar Reads

CAT Preparation 2019: A Complete Preparation Package You Require for Cracking CAT 2019
CAT (Common Admission Test) is arguably the most important exam in India for students wanting to secure the coveted MBA seats in various management institutes of the country. It scores the candidates on the basis of their Quantitative Ability, Verbal Ability & Reading Comprehension(VARC) as well as their Data Interpretation & Logical Reasoning (DIL
7 min read
NumPy ndarray.tolist() Method | Convert NumPy Array to List
The ndarray.tolist() method converts a NumPy array into a nested Python list. It returns the array as an a.ndim-levels deep nested list of Python scalars. Data items are converted to the nearest compatible built-in Python type. Example C/C++ Code import numpy as np gfg = np.array([1, 2, 3, 4, 5]) print(gfg.tolist()) Output[1, 2, 3, 4, 5] SyntaxSynt
1 min read
NumPy ndarray.size() Method | Get Number of Elements in NumPy Array
The ndarray.size() method returns the number of elements in the NumPy array. It works the same as np.prod(a.shape), i.e., the product of the dimensions of the array. Example C/C++ Code import numpy as np arr = np.zeros((3, 4, 2), dtype = np.complex128) gfg = arr.size print (gfg) Output : 24Syntax Syntax: numpy.ndarray.size(arr) Parameters arr : [ar
1 min read
NumPy ndarray.imag() Method | Get Imaginary Part in NumPy Array
The ndarray.imag() method returns the imaginary part of the complex number in the NumPy array. Note: Remember resulting data type for the imaginary value is 'float64'. Example C/C++ Code # import the important module in python import numpy as np # make an array with numpy gfg = np.array([1 + 2j, 2 + 3j]) # applying ndarray.imag() method geeks = np.
1 min read
NumPy ndarray.transpose() Method | Find Transpose of the NumPy Array
The ndarray.transpose() function returns a view of the array with axes transposed. For a 1-D array, this has no effect, as a transposed vector is simply the same vector.For a 2-D array, this is a standard matrix transpose.For an n-D array, if axes are given, their order indicates how the axes are permuted. If axes are not provided and arr.shape = (
2 min read
Python | Numpy numpy.resize()
With the help of Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing. Parameters: new_shape : [tuple of ints, or n ints] Shape of resized array refcheck : [bool, optio
2 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self<value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self>value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self<=value Example #1 : In this example we
1 min read
Practice Tags :