numpy.asarray() in Python
The numpy.asarray()function is used to convert the input to an array. Input includes lists, lists of tuples, tuples, tuples of tuples,etc,.
Syntax
1 2 3 |
numpy.asarray(a, dtype=None, order=None) |
Parameter
arr : This parameter includes theinput data, in any form that can be converted to an array. This includes lists, tuples, lists of tuples,tuples of tuples, tuples of listsetc,.
dtype : It is an optional parameter. It depicts the data type of returned array, and by default, it is a float.
order : This parameter states whether to use row-major (C-style) or column-major (Fortran-style) memory representation. By defaults it takes ‘C-style’.
Return
This function returns the array interpretation of arr. If the parameter ‘arr’ is a subclass, a base class is returned.
Example 1
1 2 3 4 5 6 7 8 9 |
# Python Programming giving an example for # numpy.asarray() method importnumpy as numpy list = [2, 4, 6, 7, 9] print ("Input list : ", list) arr = numpy.asarray(list) print ("output array from input list : ", arr) |
Output
Input list :
1 2 3 |
[2, 4, 6, 7, 9] |
output array from input list :
1 2 3 |
[2 4 6 7 9] |
Example 2
1 2 3 4 5 6 7 8 9 |
# Python Programming giving an example for # numpy.asarray() method using tuple importnumpy as numpy tuple = ([1, 2, 3], [4, 5, 6]) print ("Input tuple : ", tuple) arr = numpy.asarray(tuple) print ("output array from input tuple : ", arr) |
Output
Input touple :
1 2 3 |
([1, 2, 3], [4, 5, 6]) |
output array from input touple :
1 2 3 4 |
[[1 2 3] [4 5 6]] |