Python NumPy asarray() Function

NumPy asarray() Function:

The asarray() function of NumPy module creates an array from data in the form of lists or tuples. This function helps a lot when we need to convert a Python sequence to a numpy array object.

Syntax:

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

Parameters

a: This is required. Data can be in any format as input that can be converted to an array. It may be any sequence like Lists, lists of tuples, tuples, tuples of tuples, tuples of lists, and ndarrays are all examples of this.

dtype: This is optional. The datatype is taken from the input data by default.

order: This is optional. Which memory representation to use: row-major (C-style) or column-major (Fortran-style). ‘C’ is the default value.

Return Value:

Array interpretation of a (ndarray). If the input is already an ndarray with the same dtype and order as the output, no copy is done. A base class ndarray is returned if an is a subclass of ndarray.

If axis is not None and the axis being squeezed is not of length 1, ValueError is raised.

NumPy asarray() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the above list as an argument to the asarray() function to convert it into an array.
  • Store it in a variable.
  • Print the above-obtained result array.
  • Pass the above array as an argument to the type() function to get the type of it and print it.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the list as static input and store it in a variable.
gvn_lst = [2, 5, 7, 9, 3]
# Pass the above list as an argument to the asarray() function to convert it into an array.
# Store it in a variable.
rslt_arry = np.asarray(gvn_lst)
# Print the above obtained result array
print(rslt_arry)
# Pass the above array as an argument to the type() function to get the
# type of it and print it
print(type(rslt_arry))

Output:

[2 5 7 9 3]
<class 'numpy.ndarray'>

Example2: For Tuples

Approach:

  • Import numpy module using the import keyword.
  • Give the tuple as static input and store it in a variable.
  • Pass the above tuple, dtype=float as arguments to the asarray() function to convert it into an array(float values).
  • Store it in a variable.
  • Print the above-obtained result array.
  • Pass the above array as an argument to the type() function to get the type of it and print it.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the tuple as static input and store it in a variable.
gvn_tupl = (2, 5, 7, 9, 3)
# Pass the above tuple, dtype=float as arguments to the asarray() function to 
# convert into an array(float values).
# Store it in a variable.
rslt_arry = np.asarray(gvn_tupl, dtype=float)
# Print the above obtained result array
print(rslt_arry)
# Pass the above array as an argument to the type() function to get the
# type of it and print it
print(type(rslt_arry))

Output:

[2. 5. 7. 9. 3.]
<class 'numpy.ndarray'>

Example3: List of lists

Approach:

  • Import numpy module using the import keyword.
  • Give the list of lists as static input and store it in a variable.
  • Pass the above list as an argument to the asarray() function to convert it into an array.
  • Store it in a variable.
  • Print the above-obtained result array.
  • Pass the above array as an argument to the type() function to get the type of it and print it.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Give the list of lists as static input and store it in a variable.
gvn_lst = [[1,3,5], [2,4,6]]
# Pass the above list as an argument to the asarray() function to convert it into an array.
# Store it in a variable.
rslt_arry = np.asarray(gvn_lst)
# Print the above obtained result array
print(rslt_arry)
# Pass the above array as an argument to the type() function to get the
# type of it and print it
print(type(rslt_arry))

Output:

[[1 3 5]
 [2 4 6]]
<class 'numpy.ndarray'>