Chapter II NumPy foundation 1
The input and output in the sample code in this chapter come from the IPython session.
2.1 NumPy array object
ndarray in NumPy is a multidimensional array object, which consists of two parts:
- Actual data;
- Metadata describing these data.
Most array operations only modify the metadata without changing the underlying actual data.
In Chapter 1, we already know how to create arrays using the array function. In fact, the array created at that time was only a one-dimensional array containing a set of numbers, and ndarray supported higher dimensions.
NumPy arrays are generally homogeneous (except for a special array type, which is heterogeneous), that is, all element types in the array must be consistent. This has one advantage: if we know that the elements in the array are of the same type, the storage space required by the array can be easily determined.
As in Python, the subscript of NumPy array starts from 0. The data types of array elements are represented by special objects.
Once again, we use the array function to create the array and obtain its data type:
In [1]: import numpy as np In [2]: a = np.arange(5) In [3]: a.dtype Out[3]: dtype('int32')
The data type of array a is int32 (on my machine). Of course, if you use 64 bit Python, the result may be int64. In either case, the data type of the array is an integer (64 or 32 bits).
In addition to the data type, the dimension of the array is also an important attribute.
The example in Chapter 1 demonstrates how to create a vector (that is, a one-dimensional NumPy array). Vectors are commonly used in mathematics, but in most cases, we need higher dimensional objects. Let's first determine the dimension of the vector just created:
In [4]: a Out[4]: array([0, 1, 2, 3, 4]) In [5]: a.shape Out[5]: (5,)
As you can see, this is a vector of 5 elements, with values ranging from 0 to 4. The shape attribute of the array returns a tuple, and the elements in the tuple are the size of each dimension of the NumPy array. The array in the above example is one-dimensional, so there is only one element in the tuple.
2.2 hands on practice: creating multidimensional arrays
Now that we know how to create vectors, we can try to create a multidimensional NumPy array and view its dimensions.
(1) Create a multidimensional array.
(2) Displays the dimensions of the array.
In [6]: m = np.array([np.arange(2),np.arange(2)]) In [7]: m Out[7]: array([[0, 1], [0, 1]]) In [8]: m.shape Out[8]: (2, 2)
We use the array created by the array function as a list element and pass the list as a parameter to the array function to create a 2 × 2, and there is no error message.
The array function can generate an array based on a given object. The given object should be an array of classes, such as a list in Python. In the above example, the object we passed to the array function is a list of NumPy arrays. Such a class array object is the only necessary parameter of the array function, and many other parameters are optional parameters with default values.
Question 1 which of the following ways is the dimension attribute of the ndarray object stored?
(1) Comma separated string
(2) Python list
(3) Python tuple
2.2.1 selecting array elements
Sometimes we need to select a specific element in the array. First, create a 2 × Multidimensional array of 2:
In [9]: a = np.array([[1,2],[3,4]]) In [10]: a Out[10]: array([[1, 2], [3, 4]])
When creating this multidimensional array, the object we pass to the array function is a nested list. Now select the elements in the array in turn. Remember, the subscript of the array starts at 0.
In [11]: a[0,0] Out[11]: 1 In [12]: a[0,1] Out[12]: 2 In [13]: a[1,0] Out[13]: 3 In [14]: a[1,1] Out[14]: 4
Yes, it's that simple to select elements from an array. For array a, you only need to select each array element with a[m,n]
In, m and n are element subscripts, and the corresponding positions are shown in the table below.
2.2.2 NumPy data type
Python supports integer, floating-point and complex data types, but these types are not enough to meet the needs of scientific computing, so NumPy adds many other data types. In practical applications, we need data types with different precision, and they occupy different memory space. In NumPy, * * most data type names end with a number, which represents the number of bits it occupies in memory** The following table (compiled from the NumPy user manual) lists the data types supported in NumPy.
Each data type has a corresponding type conversion function:
In [15]: np.float64(42) Out[15]: 42.0 In [17]: np.int8(42.0) Out[17]: 42 In [18]: np.bool(42) C:\Users\administrator\AppData\Local\Programs\Python\Python37\Scripts\ipython:1: Deprecatio nWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence thi s warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdo cs/release/1.20.0-notes.html#deprecations Out[18]: True In [20]: np.float(True) C:\Users\administrator\AppData\Local\Programs\Python\Python37\Scripts\ipython:1: Deprecatio nWarning: `np.float` is a deprecated alias for the builtin `float`. To silence t his warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here . Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdo cs/release/1.20.0-notes.html#deprecations Out[20]: 1.0
Note: in version 1.20, NP bool, np. Floats are aliases of corresponding system functions
In NumPy, the data type can be specified in the parameters of many functions. Generally, this parameter is optional:
In [23]: np.arange(7, dtype=np.uint16) Out[23]: array([0, 1, 2, 3, 4, 5, 6], dtype=uint16)
It should be noted that complex numbers cannot be converted to integers, which will trigger a TypeError error:
In [24]: np.int(42.0+1.j) C:\Users\administrator\AppData\Local\Programs\Python\Python37\Scripts\ipython:1: Deprecatio nWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is saf e. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the releas e note link for additional information. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdo cs/release/1.20.0-notes.html#deprecations --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-24-4b5b3467cc1e> in <module> ----> 1 np.int(42.0+1.j) TypeError: can't convert complex to int
Similarly, complex numbers cannot be converted to floating point numbers. However, floating-point numbers can be converted to complex numbers, such as complex(1.0). Note that the part with j is the imaginary part of the complex number.
2.2.3 data type object
The data type object is NumPy An instance of the dtype class. As mentioned earlier, the NumPy array has data types. More specifically, each element in the NumPy array has the same data type. The data type object can give the number of bytes occupied by a single array element in memory, that is, the itemsize attribute of dtype class:
In [25]: a.dtype.itemsize Out[25]: 4
2.2.4 character coding
NumPy can use character encoding to represent data types, which is compatible with Numeric, the predecessor of NumPy. I don't recommend character coding, but it is sometimes used, so the corresponding table of character coding is listed below. Readers should prefer to use dtype objects to represent data types rather than these character encodings.
The following code creates an array of single precision floating-point numbers:
In [26]: np.arange(7, dtype='f') Out[26]: array([0., 1., 2., 3., 4., 5., 6.], dtype=float32)
Similarly, you can also create a complex array:
In [28]: np.arange(7, dtype='D') Out[28]: array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j, 5.+0.j, 6.+0.j])