The numpy.asarray()
function in Python converts an input to an array.
numpy.asarray(a, dtype=None)
The numpy.asarray()
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.The numpy.asarray()
function returns the array interpretation of the input data passed to it.
import numpy as np# creating an input datax = [1, 2, 3, 4, 5]# converting the input data to an arraymyarray = np.asarray(x, dtype = np.double)print(myarray)
numpy
module.x
, with five elements.x
to a float type array using the numpy.asarray()
function. The output is assigned to a variable, myarray
.myarray
variable.