YouTip LogoYouTip

Numpy Array Attributes

# NumPy Array Attributes In this section, we will learn about some basic attributes of NumPy arrays. The dimension of a NumPy array is called its rank. The rank is the number of axes (dimensions). For example, the rank of a one-dimensional array is 1, the rank of a two-dimensional array is 2, and so on. In NumPy, each linear array is called an axis. For example, a two-dimensional array is equivalent to two one-dimensional arrays, where each element in the first one-dimensional array is itself a one-dimensional array. So, one-dimensional arrays are the axes in NumPy. The first axis is like the underlying array, and the second axis is the array within that underlying array. The number of axesβ€”the rankβ€”is the dimensionality of the array. Often, you can specify an axis. `axis=0` means performing an operation along the 0th axis (i.e., on each column), while `axis=1` means performing an operation along the 1st axis (i.e., on each row). The most important attributes of the NumPy ndarray object are: | Attribute | Description | | --- | --- | | `ndarray.ndim` | The rank (dimensionality) of the array, i.e., the number of axes or dimensions. | | `ndarray.shape` | The dimensions of the array, indicating the size along each axis. For a 2D array (matrix), it represents the number of rows and columns. | | `ndarray.size` | The total number of elements in the array, equal to the product of the sizes along each axis in `ndarray.shape`. | | `ndarray.dtype` | The data type of the elements in the array. | | `ndarray.itemsize` | The size of each element in the array in bytes. | | `ndarray.flags` | Contains information about the memory layout, such as whether it is C or Fortran contiguous, whether it is read-only, etc. | | `ndarray.real` | The real part of each element in the array (if the element type is complex). | | `ndarray.imag` | The imaginary part of each element in the array (if the element type is complex). | | `ndarray.data` | The buffer containing the actual elements of the array. Typically, elements are accessed via indexing, and this attribute is not used directly. | ### ndarray.ndim `ndarray.ndim` is used to get the number of dimensions (i.e., the number of axes) of the array. ## Example import numpy as np a = np.arange(24)print(a.ndim)# a now has only one dimension# Now adjust its size b = a.reshape(2,4,3)# b now has three dimensions print(b.ndim) The output is: 13 ### ndarray.shape `ndarray.shape` represents the dimensions of the array and returns a tuple. The length of this tuple is the number of dimensions, i.e., the ndim attribute (rank). For example, for a two-dimensional array, the dimensions represent "number of rows" and "number of 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) The output is: (2, 3) Resize the array. ## Example import numpy as np a = np.array([[1,2,3],[4,5,6]])a.shape = (3,2)print(a) The output is: [ ] NumPy also provides the `reshape` function to resize arrays. ## Example import numpy as np a = np.array([[1,2,3],[4,5,6]])b = a.reshape(3,2)print(b) The output is: [ ] ### ndarray.itemsize `ndarray.itemsize` returns the size of each element in the array in bytes. For example, an array with a dtype of `float64` has an `itemsize` value of 8 (since `float64` occupies 64 bits, and each byte is 8 bits, so 64/8 = 8 bytes). Similarly, an array with a dtype of `complex32` has an `itemsize` of 4 (32/8). ## Example import numpy as np# Array dtype is int8 (one byte) x = np.array([1,2,3,4,5], dtype = np.int8)print(x.itemsize)# Array dtype is now float64 (eight bytes) y = np.array([1,2,3,4,5], dtype = np.float64)print(y.itemsize) The output is: 18 ### ndarray.flags `ndarray.flags` returns memory information about the ndarray object, containing the following attributes: | Attribute | Description | | --- | --- | | C_CONTIGUOUS (C) | Data is in a single, C-style contiguous segment. | | F_CONTIGUOUS (F) | Data is in a single, Fortran-style contiguous segment. | | 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 to False makes the data read-only. | | ALIGNED (A) | Data and all elements are properly aligned for the hardware. | | UPDATEIFCOPY (U) | This array is a copy of another array. When this array is released, 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) The output is: C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False
← Numpy Array From Existing DataPython3 Os Path β†’