Creating NumPy ndarrays
An ndarray is an extremely high-performant and space-efficient data structure for multidimensional arrays.
First, we need to import the NumPy library, as follows:
import numpy as np
Next, we will start creating a 1D ndarray.
Creating 1D ndarrays
The following line of code creates a 1D ndarray:
arr1D = np.array([1.1, 2.2, 3.3, 4.4, 5.5]); arr1D
This will give the following output:
array([1.1, 2.2, 3.3, 4.4, 5.5])
Let's inspect the type of the array with the following code:
type(arr1D)
This shows that the array is a NumPy ndarray, as can be seen here:
numpy.ndarray
We can easily create ndarrays of two dimensions or more.
Creating 2D ndarrays
To create a 2D ndarray, use the following code:
arr2D = np.array([[1, 2], [3, 4]]); arr2D
The result has two rows and each row has two values, so it is a 2 x 2 ndarray, as illustrated in the following code snippet:
array([[1, 2], ...