Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy: asarray() function

asarray() function

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

Syntax:

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

Version: 1.15.0

Parameter:

Name Description Required /
Optional
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
True

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
True
>>> np.asarray(x, dtype=np.float64) is x
False

Example-3: numpy.asarray() function

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

Python - NumPy Code Editor:

Previous: array()
Next: asanyarray()