NumPy is a library that uses multidimensional arrays as the basic data structure. The only data structure in NumPy is
ndarray but not Python primitive
list data type, because
list runs relatively slowly.
After you have learned
ndarray that is cornerstone of NumPy, you will understand why NumPy can achieve high-speed computing.
ndarray is the abbreviation of n-dimension array, or in other words - multidimensional arrays.
ndarray is an array object representing a multidimensional, homogeneous array of fixed-size items.
The dimensions and the number of elements are defined by the shape, that is a tuple of N integers that represents the number of elements in each dimension. The element type in the array is defined by
Let’s explain the sentences above in layman language. All the elements stored in the
ndarray object must have the same data type and size.
The characteristics of
ndarray data type are summarized as follows.
- Can only store elements of the same type
- The amount of data in each dimension must be the same, for example 2D
ndarraymust have the same amount of elements in every column, and of course also in each row.
- It is written in C language and it could execute matrix operation optimally
Let’s list the attributes of
||Transpose matrix. When the array is 1 D, the original array is returned.|
||A Python buffer object that points to the starting position of the data in the array.|
||The data type of the element contained in the ndarray.|
||Information about how to store ndarray data in memory (memory layout).|
||An iterator that converts ndarray to a one-dimensional array.|
||The imaginary part of ndarray data|
||Real part of ndarray data|
||The number of elements contained in the ndarray.|
||The size of each element in bytes.|
||The total memory (in bytes) occupied by the ndarray.|
||The number of dimensions contained in the ndarray.|
||The shape of the ndarray (results are tuples).|
||The number of bytes required to move to the next adjacent element in each dimension direction is represented by a tuple.|
||An iterator that is processed in the ctypes module.|
||The object on which ndarray is based (which memory is being referenced).|
When you access the attributes of
ndarray, the data of
ndarray instance is not modified, even if when you use
.T to get the transpose of the object. You get a new
ndarray object but not modified original data.
Let’s take a look at the specific meaning of each attribute through example codes.
>>> import numpy as np >>> a = np.array([1, 2, 3])
We need to import
NumPy library and create a new 1-D array. You could check its data type and the data type of its element.
>>> type(a) numpy.ndarray >>> a.dtype dtype('int32')
Let’s create a new 2-D array and then check its attributes.
>>> b = np.array([[4, 5, 6], [7, 8, 9]]) >>> b array([[4, 5, 6], [7, 8, 9]]) >>> b.T # get the transpose of b array([[4, 7], [5, 8], [6, 9]]) >>> b # b keeps unmodified array([[4, 5, 6], [7, 8, 9]]) >>> a.size # a has 3 elements 3 >>> b.size # b has 6 elements 6 >>> a.itemsize # The size of element in a. The data type here is int64 - 8 bytes 8 >>> b.nbytes # check how many bytes in b. It is 48, where 6x8 = 48 48 >>> b.shape # The shape of b (2, 3) >>> b.dnim # The dimensions of b 2