The NumPy Array Object
- The core of the NumPy Library is one main object:
ndarray
(which stands for N-dimensional array) - This object is a multi-dimensional homogeneous array with a predetermined number of items
- In addition to the data stored in the array, this data structure also contains important metadata about the array, such as its shape, size, data type, and other attributes.
Basic Attributes of the ndarray Class
Attribute | Description |
---|---|
shape | A tuple that contains the number of elements (i.e., the length) for each dimension (axis) of the array. |
size | The total number elements in the array. |
ndim | Number of dimensions (axes). |
nbytes | Number of bytes used to store the data. |
dtype | The data type of the elements in the array. |
itemsize | Defines teh size in bytes of each item in the array. |
data | A buffer containing the actual elements of the array. |
In order to use the NumPy library, we need to import it in our program. By convention, the numPy module imported under the alias np, like so:
import numpy as np
After this, we can access functions and classes in the numpy module using the np namespace. Throughout this notebook, we assume that the NumPy module is imported in this way.
data = np.array([[10, 2], [5, 8], [1, 1]])
data
Here the ndarray instance data is created from a nested Python list using the
function np.array
. More ways to create ndarray instances from data and from rules of
various kinds are introduced later in this tutorial.
type(data)
data.ndim
data.size
data.dtype
data.nbytes
data.itemsize
data.data
Data types
dtype
attribute of thendarray
describes the data type of each element in the array.- Since NumPy arrays are homogeneous, all elements have the same data type.
Basic Numerical Data Types Available in NumPy
dtype | Variants | Description |
---|---|---|
int | int8, int16, int32, int64 | Integers |
uint | uint8, uint16, uint32, uint64 | Unsigned (non-negative) integers |
bool | Bool | Boolean (True or False) |
float | float16, float32, float64, float128 | Floating-point numbers |
complex | complex64, complex128, complex256 | Complex-valued floating-point numbers |
Once a NumPy array is created, its dtype
cannot be changed, other than by creating a new copy with type-casted array values
data = np.array([5, 9, 87], dtype=np.float32)
data
data = np.array(data, dtype=np.int32) # use np.array function for type-casting
data
data = np.array([5, 9, 87], dtype=np.float32)
data
data = data.astype(np.int32) # Use astype method of the ndarray class for type-casting
data
Data Type Promotion
When working with NumPy arrays, the data type might get promoted from one type to another, if required by the operation. For instance, adding float-value and integer-valued arrays, the resulting array is a float-valued array:
arr1 = np.array([0, 2, 3], dtype=float)
arr1
arr2 = np.array([10, 20, 30], dtype=int)
arr2
res = arr1 + arr2
res
res.dtype
NOTE:
In some cases, depending on the application and its requirements, it is essential to create arrays with data type appropriately set to right data type. The default data type is float
:
np.sqrt(np.array([0, -1, 2]))
np.sqrt(np.array([0, -1, 2], dtype=complex))
Here, using the np.sqrt
function to compute the square root of each element in
an array gives different results depending on the data type of the array. Only when the data type of the array is complex is the square root of –1
resulting in the imaginary unit (denoted as 1j
in Python).
%load_ext watermark
%watermark --iversion -g -m -v -u -d