English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Basic Knowledge of NumPy Ndarray Object
One of the most important features of NumPy is its N-dimensional array object ndarray, which is a collection of data of the same type, indexed from 0 as the starting index of the collection.
ndarray objects are used to store multidimensional arrays of the same type.
Each element in ndarray has an area of the same storage size in memory.
The ndarray is internally composed of the following:
A pointer to the data (a block of data in memory or a memory-mapped file).Data type or dtype, describing the fixed size cells of the values in the array.A tuple representing the shape (shape) of the array, representing the tuple of sizes of each dimension.A tuple of spans (stride), where the integers indicate the number of bytes to 'cross' to advance to the next element in the current dimension.
The main object of NumPy is a homogeneous multidimensional array. It is an element table (usually numeric), all types are the same, and is indexed by a non-negative integer tuple. In NumPy dimensions, it is called an axis.
For example,3Coordinates of points in D space [1, 2, 1]. It has one axis. This axis has3elements, so we say its length is3.In the example shown below, the array has2axes. The length of the first axis is2The length of the second axis is3.
]] 1., 0., 0.], [ 0., 1. 2.]]
NumPy's array class is called ndarray. It is important to note that numpy.array is different from the standard Python library class array.array, which only handles one-dimensional arrays and provides fewer features. ndarray objects have more important properties, as follows:
ndarray.ndim - The number of axes (dimensions) of the array. In the Python world, the number of dimensions is called rank.ndarray.shape - The dimensions of the array. This is an integer tuple representing the size of the array in each dimension. The length of the shape tuple is the rank or the number of dimensions ndim.ndarray.size - The total number of array elements. This is equal to the product of the elements of shape.ndarray.dtype - An object that describes the element type of the array. You can create or specify dtype using standard Python types. Additionally, NumPy provides its own types.ndarray.itemsize - The byte size of each element in the array. For example, if the element is float64 The itemsize of the array of type8!=64/8)ndarray.data - This buffer contains the actual elements of the array. Usually, we do not need to use this attribute because we will use index access to elements in the array.
Let's look at a specific example
>>> import numpy as np >>> a = np.arange(15).reshape(3, 5) >>> a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]) >>> a.shape (3, 5) >>> a.ndim 2 >>> a.dtype.name 'int64' >>> a.itemsize 8 >>> a.size 15 >>> type(a)
To create an ndarray, just call the NumPy's array function:
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
object - Array or nested sequencedtype - Data type of array elements, optionalcopy - Whether the object needs to be copied, optionalorder - Style of array creation, C for row direction, F for column direction, A for arbitrary direction (default)subok - Default returns an array consistent with the base class typendmin - Specify the Minimum Dimension of the Generated Array
Next, we will go through some examples to better understand the usage of Ndarray.
>>> import numpy as np >>> a = np.array([1,2,3,4,5) >>> print(a) [1 2 3 4 5[
>>> import numpy as np >>> a = np.array([1,2,3],[4,5,6],[7,8,9]) >>> print(a) ]]1 2 3[ [4 5 6[ [7 8 9]
>>> import numpy as np >>> a = np.array([1,2,3,4,5、Specify Array Dimensions 3) >>> print(a) [[1 2 3 4 5]]
>>> import numpy as np >>> a = np.array([1,2,3,4,5,6], dtype = float32) >>> print(a) [1. 2. 3. 4. 5. 6.]