English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NumPy Array Attributes

    Description and Examples of NumPy Array Attributes

The dimension of NumPy arrays is called rank, which is the dimension of the array, and the rank of a one-dimensional array is 1, the rank of a two-dimensional array is 2, and so on.
In NumPy, every linear array is called an axis (axis), axis=0 indicating operations along the 0 axis, that is, operations on each column; axis=1, indicating along the1Axis operations, that is, operations on each row.
For example, a two-dimensional array is equivalent to two one-dimensional arrays, where each element of the first one-dimensional array is also a one-dimensional array.
Therefore, a one-dimensional array is an axis (axis) in NumPy, and the first axis is equivalent to the underlying array, and the second axis is an array within the underlying array. The number of axes—the rank—is the dimension of the array.

Some important ndarray object attributes in NumPy arrays are

The data types supported by numpy are more than Python built-in data typesThere are many more, which can basically correspond to the data types of C language, among which some types correspond to Python built-in types. The following table lists common NumPy basic types.

ndarray.ndim - Rank, which is the number of axes or the number of dimensions.ndarray.shape - The dimensions of the array, for a matrix, n rows and m columns.ndarray.size - The total number of array elements, equivalent to .shape[n].*The value of m.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 ndarray objects.ndarray.real - The real part of ndarray elements.ndarray.imag - The imaginary part of ndarray elements.ndarray.data - The buffer containing the actual array elements. Since elements are generally obtained through array indexing, this attribute is usually not needed.

1、ndarray.ndim

ndarray.ndim is used to return the number of dimensions of the array, which is equal to the rank.

>>> import numpy as np
>>> a = np.arange(24)
>>> print(a.ndim) 
1 # a now has only one dimension
>>> b = a.reshape(2,4,3)
>>> print(b.ndim)
3 # b now has three dimensions

2、ndarray.shape

ndarray.shape represents the dimensions of the array, returning a tuple. The length of this tuple is the number of dimensions, which is also the ndim attribute (rank). For example, a two-dimensional array represents 'rows' and 'columns' in its dimensions.
ndarray.shape can also be used to adjust the size of the array.

>>> import numpy as np
>>> a = np.array([1,2,3,4,5,6],[4,5,6,7,8,9])
>>> print(a.shape)
(2, 6)
>>> a.shape = (6,2)
>>> print(a)
[[1 2]
 [3 4]
 [5 6]
 [4 5]
 [6 7]
 [8 9]]

At the same time, NumPy also provides the reshape function to adjust the size of the array, with the following specific examples:

>>> import numpy as np
>>> a = np.array([1,2,3,4,5,6],[4,5,6,7,8,9])
>>> b = a.reshape(6,2)
>>> print(b)
[[1 2]
 [3 4]
 [5 6]
 [4 5]
 [6 7]
 [8 9]]

3、ndarray.size

ndarray.size is the number of elements in the array. It is equal to np.prod(a.shape), that is, the product of the array dimensions.
a.size returns a standard arbitrary precision Python integer. For other methods that obtain the same value, the situation may not be the same (such as the recommended method np.prod(a.shape), which returns an instance np.int_), and if this value is further used in calculations that may overflow fixed-size integer types, it may be relevant.

>>> import numpy as np
>>> x = np.zeros((3, 5, 2), dtype=np.complex128)
>>> x.size
30
>>> np.prod(x.shape)
30

4、ndarray.dtype

>>> import numpy as np
>>> x
array([[0, 1],
       [2, 3])
>>> x.dtype
dtype('int32')
>>> type(x.dtype)

5、ndarray.itemsize

ndarray.itemsize returns the size of each element in the array in bytes.
For example, an element type of float64 The array's itemsiz attribute value is 8(float64 occupies 64 bits, with each byte having a length of 8, so 64/8, occupies 8 bytes), for example, an element type of complex32 The array's item attribute is 4(32/8)。

>>> import numpy as np
>>> x = np.array([1,2,3,4,5], dtype = np.int8)
>>> print (x.itemsize)
1
>>> y = np.array([1,2,3,4,5], dtype = np.float64)
>>> print (y.itemsize)
8

6、ndarray.flags

ndarray.flags returns the memory information of the ndarray object, including the following properties:

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. Set this value to False to make the data read-only.ALIGNED (A) - Data and all elements are properly aligned to the hardware.UPDATEIFCOPY (U) - This array is a copy of another array, and when this array is released, the content of the original array will be updated.

>>> import numpy as np
>>> a = np.array([1,2,3,4,5]
>>> print(a.flags)
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

7、ndarray.real and x.imag

>>> import numpy as np
>>> x = np.sqrt([1+0j, 0+1j])
>>> x.real
array([1. , 0.70710678]
>>> x.real.dtype
dtype('float64')
>>> x.imag
array([0. , 0.70710678]
>>> x.imag.dtype
dtype('float64')