numpy.asarray() - ( Creating Arrays with NumPy )
Heading h2
Syntax
numpy.asarray(a, dtype=None, order=None)
Example
import numpy as np
a = [1, 2, 3, 4, 5]
arr = np.asarray(a)
print(arr)
Output
[1 2 3 4 5]
Explanation
The numpy.asarray()
function is used to create an array from a given sequence or iterable. The function accepts a sequence or iterable as input and returns a new NumPy array that is created from the input. The input can be a list, tuple, or any sequence type that can be iterated. The function also accepts an optional dtype
and order
argument, which can be used to specify the data type and memory layout of the output array.
Use
The numpy.asarray()
function is useful in situations where we need to convert a given sequence or iterable to a NumPy array. This function can be used on any sequence or iterable in Python.
Important Points
numpy.asarray()
creates a new NumPy array from a given sequence or iterable- The function can be used on any sequence or iterable in Python
- The optional
dtype
andorder
arguments can be used to specify the data type and memory layout of the output array
Summary
In conclusion, the numpy.asarray()
function is a useful tool for creating a new NumPy array from a given sequence or iterable. This function can be used on any sequence or iterable in Python. The function returns a new NumPy array that is created from the input, with an optional dtype
and order
argument that can be used to specify the data type and memory layout of the output array.