Creating a NumPy ndarray Object
NumPy is a powerful Python library used for working with arrays. In NumPy, the core data structure is called an ndarray (short for N-dimensional array).
You can create a NumPy ndarray object using the `array()` function.
Program:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
The `type()` function is a built-in Python method that returns the type of the object passed to it. In the example above, it confirms that `arr` is a `numpy.ndarray`.
Creating an ndarray from Different Array-Like Structures
To create an ndarray, you can pass in a list, tuple, or any array-like object to the `array()` function. It will be automatically converted into an ndarray.
Program:
Using a tuple to create a NumPy array:
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
This code will create a NumPy array from the given tuple and print the result.