What is the numpy.asanyarray() function in Python?
Overview
The asanyarray() function in Python converts the input data to an array.
Syntax
numpy.asanyarray(a, dtype=None)
Syntax for the asanyarray() function in Python
Parameter value
The asanyarray() function takes the following parameter values:
a: This represents the input data converted to an array. It could be a list, lists of tuples, tuples of lists, and arrays.dtype: This represents the data type of the desired array. By default, the data type of the array is determined from the input data. This is optional.
Return value
The asanyarray() function returns an array representation of the input data.
Example
import numpy as np# creating an input dataa = [1.5, 2.0, 3.1, 4.4, 5]# converting the input data to an arraymyarray = np.asanyarray(a, dtype = np.int)print(myarray)
Explanation
- Line 1: We import the
numpymodule. - Line 4: We create an input data variable,
xWith five elements. - Line 7: We convert the input data
xto an int-type array using theasanyarray()function. The output is assigned to a variable,myarray. - Line 9: We print the
myarrayarray.