w3resource

NumPy: numpy.asanyarray() function

numpy.asanyarray() function

The numpy.asanyarray() function is used to convert a given input to an ndarray, but pass ndarray subclasses through.

Syntax:

numpy.asanyarray(a, dtype=None, order=None)

Parameters:

Name Description Required /
Optional
a Input data, in any form that can be converted to an array. This includes scalars, 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 (Fortranstyle) memory representation. Defaults to 'C'. Optional

Return value:

out [ndarray or an ndarray subclass] Array interpretation of a. If a is an ndarray or a subclass of ndarray, it is returned as-is and no copy is performed.

Example-1: numpy.asanyarray()

>>> import numpy as np
>>> x = [4, 6]
>>> np.asanyarray(x)
array([4, 6])

Example-2: numpy.asanyarray()

>>> import numpy as np
>>> x = np.array([(2.0, 3), (4.0, 5)], dtype='f4,i4').view(np.recarray)
>>> np.asanyarray(x) is x
True

Python - NumPy Code Editor:

Previous: asarray()
Next: asmatrix()



Follow us on Facebook and Twitter for latest update.