NumPy Array Attributes
In this section, we will explore some basic attributes of NumPy arrays.
The number of dimensions of a NumPy array is called its rank, which is the number of axes, or the dimensionality of the array. A one-dimensional array has a rank of 1, a two-dimensional array has a rank of 2, and so on.
In NumPy, each linear array is called an axis, which is also a dimension. For example, a two-dimensional array consists of two one-dimensional arrays, where each element in the first one-dimensional array is itself a one-dimensional array. Thus, a one-dimensional array is an axis in NumPy, with the first axis being the underlying array and the second axis being the array within the underlying array. The number of axes—the rank—is the dimensionality of the array.
Often, you can declare an axis. axis=0 means performing operations along the 0th axis, i.e., operations on each column; axis=1 means performing operations along the 1st axis, i.e., operations on each row.
Some important attributes of the ndarray object in NumPy include:
Attribute | Description |
---|---|
ndarray.ndim | The rank, i.e., the number of axes or dimensions |
ndarray.shape | The dimensions of the array; for a matrix, n rows by m columns |
ndarray.size | The total number of elements in the array, equivalent to the product n*m from .shape |
ndarray.dtype | The element type of the ndarray object |
ndarray.itemsize | The size of each element in the ndarray object, in bytes |
ndarray.flags | Memory information of the ndarray object |
ndarray.real | The real part of the ndarray elements |
ndarray.imag | The imaginary part of the ndarray elements |
ndarray.data | The buffer containing the actual array elements; usually not needed as elements are accessed via indexing |
ndarray.ndim
ndarray.ndim returns the number of dimensions of the array, equal to its rank.
Example
import numpy as np
a = np.arange(24)
print(a.ndim) # a now has only one dimension
# Now resize it
b = a.reshape(2,4,3) # b now has three dimensions
print(b.ndim)
Output:
1
3
ndarray.shape
ndarray.shape represents the dimensions of the array, returning a tuple whose length is the number of dimensions, i.e., the ndim attribute (rank). For example, for a two-dimensional array, the dimensions represent the number of rows and columns.
ndarray.shape can also be used to resize the array.
Example
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(a.shape)
Output:
(2, 3)
Resizing the array:
Example
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
a.shape = (3,2)
print(a)
Output:
[[1 2]
[3 4]
[5 6]]
NumPy also provides the reshape function to resize the array.
Example
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = a.reshape(3,2)
print(b)
Output:
[[1 2]
[3 4]
[5 6]]
ndarray.itemsize
ndarray.itemsize returns the size of each element in the array in bytes.
For example, for an array with elements of type float64, the itemsize attribute is 8 (since float64 occupies 64 bits, and each byte is 8 bits, so 64/8, occupying 8 bytes). Similarly, for an array with elements of type complex32, the itemsize attribute is 4 (32/8).
Example
import numpy as np
# The dtype of the array is int8 (one byte)
x = np.array([1,2,3,4,5], dtype=np.int8)
print(x.itemsize)
# The dtype of the array is now float64 (eight bytes)
y = np.array([1,2,3,4,5], dtype=np.float64)
print(y.itemsize)
Output:
1
8
ndarray.flags
ndarray.flags returns the memory information of the ndarray object, including the following attributes:
Attribute | Description |
---|---|
C_CONTIGUOUS (C) | The data is in a single, contiguous segment in C style |
F_CONTIGUOUS (F) | Data is in a single contiguous segment in Fortran-style |
OWNDATA (O) | The array owns the memory it uses or borrows it from another object |
WRITEABLE (W) | The data area can be written to; setting this value to False makes the data read-only |
ALIGNED (A) | The data and all elements are properly aligned for hardware |
UPDATEIFCOPY (U) | This array is a copy of another array; when this array is deallocated, the contents of the original array will be updated |
Example
import numpy as np
x = np.array([1,2,3,4,5])
print (x.flags)
Output:
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False