NumPy: asarray() function

asarray() function

The asarray() function is used to convert an given input to an array.


numpy.asarray(a, dtype=None, order=None)
NumPy array: asarray() function

Version: 1.15.0


Name Description Required /
a Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Required
dtype By default, the data-type is inferred from the input data. optional
order Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to 'C'. optional

Return value:

[ ndarray] Array interpretation of a. No copy is performed if the input is already an ndarray with matching dtype and order. If a is a subclass of ndarray, a base class ndarray is returned.

Example-1: numpy.asarray() function

>>> import numpy as np
>>> a = [2, 3]
>>> np.asarray(a)
array([2, 3])
>>> x = np.array([2, 3])
>>> np.asarray(x) is x

Pictorial Presentation:

NumPy array: asarray() function

Example-2: numpy.asarray() function

>>> import numpy as np
>>> x = np.array([2, 3], dtype=np.float32)
>>> np.asarray(x, dtype=np.float32) is x
>>> np.asarray(x, dtype=np.float64) is x

Example-3: numpy.asarray() function

>>> import numpy as np
>>> issubclass(np.recarray, np.ndarray)
>>> a = np.array([(2.0, 3), (3.0, 5)], dtype='f4,i4').view(np.recarray)
>>> np.asarray(a) is a
>>> np.asanyarray(a) is a

Python - NumPy Code Editor:

Previous: array()
Next: asanyarray()

Follow us on Facebook and Twitter for latest update.