Python numpy package usage tutorial array and related operation details, pythonnumpy
Preface
As you may know, Numpy and Numerical Python are a third-party Python package for scientific computing.
NumPy provides many advanced numerical programming tools, such as matrix data types, vector processing, and precision computing libraries. This product is designed for strict digital processing. Next, this article will introduce in detail the array of the numpy package usage tutorial in python and related operations. I will not talk about it below. Let's take a look at the detailed introduction:
I. Introduction to Arrays
In Numpy, the most important data structure is: multi-dimensional array type (numpy.ndarray)
Ndarray consists of two parts:
- Actual data;
- Metadata describing the data (metadata)
The dimension of an array (matrix) is called axes, and the dimension is called rank.
Important attributes of ndarray include:
ndarray.ndim: The dimension of the array, also known as rank
ndarray.shape: Size of each dimension of the array. For a matrix of n rows and m columns, the shape is (n, m)
ndarray.size: Total number of elements.
ndarray.dtype: The type of each element. It can be numpy. int32, numpy. int16, and numpy. float64.
ndarray.itemsize: The number of bytes occupied by each element.
ndarray.data: Point to the data memory.
Ii. Use of Arrays
Before using numpy, import the module using the following statement:
Improt numpy as np # where np is the alias of numpy, it is a common usage
1. Use the array method to generate an array
Array, or array, is the most basic data structure in numpy. The most critical attribute is the dimension and element type. In numpy, you can easily create various types of multidimensional arrays, and perform some basic operations. There are several ways to generate an array:
Use the list or tuple variable as an array:
>>> print np.array([1,2,3,4]) [1 2 3 4] >>> print np.array((1.2,2,3,4)) [ 1.2 2. 3. 4. ]
Use the list or tuple variable as the element to generate a two-dimensional array or multi-dimensional array:
>>> x = np.array(((1,2,3),(4,5,6))) >>> x array([[1, 2, 3], [4, 5, 6]]) >>> y = np.array([[1,2,3],[4,5,6]]) >>> y array([[1, 2, 3], [4, 5, 6]])
2. Use the numpy. arange method to generate an array
>>> print np.arange(15) [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] >>> print type(np.arange(15)) <type 'numpy.ndarray'>
3. Use built-in functions to generate special matrices (arrays)
Zero Matrix
>>> print np.zeros((3,4)) [[ 0. 0. 0. 0.] [ 0. 0. 0. 0.] [ 0. 0. 0. 0.]]
1 Matrix
>>> print np.ones((3,4)) [[ 1. 1. 1. 1.] [1. 1. 1. 1.] [ 1. 1. 1. 1.]]
Unit Matrix
>>> print np.eye(3) [[ 1. 0. 0.] [0. 1. 0.] [ 0. 0. 1.]]
4. indexing and Slicing
>>> X = np. array (, 3), (, 6) >>> x [] # get the number of the third column in the second row 6
>>> Y = x [:, 1] # obtain the second column >>> y array ([2, 5])
It is consistent with the python syntax and is not used as an example.
5. Get array attributes
>>> A = np. zeros (2, 2) >>> print. ndim # dimension of the array 3 >>> print. shape # size of each dimension of the array (2, 2, 2) >>> print. size # number of elements in the array 8 >>> print. dtype # float64> print. itemsize #8 bytes for each element
6. array transformation
Convert multiple dimensions to one dimension:
>>> x array([[1, 2, 3], [4, 5, 6]]) >>> x.flatten() array([1, 2, 3, 4, 5, 6])
One-dimensional conversion to multi-dimensional:
>>> Print np. arange (15 ). reshape () # change the shape, and change one dimension to three rows and five columns [[0 1 2 3 4] [5 6 7 8 9] [10 11 12 13 14]
Transpose:
>>> x array([[1, 2, 3], [4, 5, 6]]) >>> x.transpose() array([[1, 4], [2, 5], [3, 6]])
7. array combination
Horizontal combination:
>>> y=x >>> numpy.hstack((x,y)) array([[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]]
Vertical combination
>>> numpy.vstack((x,y)) array([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]])
You can use the concatenate function to implement these two methods at the same time. By specifying the axis parameter, the default value is 0 and the vertical combination is used.
>>> numpy.concatenate((x,y)) array([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]]) >>> numpy.concatenate((x,y),axis=1) array([[1, 2, 3, 1, 2, 3], [4, 5, 6, 4, 5, 6]])
8. array Segmentation
Vertical segmentation
>>> Z array ([[1, 2, 3], [4, 5, 6], [1, 2, 3], [4, 5, 6]) >>> numpy. vsplit (z, 2) # note that the number of splits set here must be divisible by the number of rows [array ([1, 2, 3], [4, 5, 6]), array ([[1, 2, 3], [4, 5, 6])]
Horizontal segmentation
>>> numpy.hsplit(z,3) [array([[1], [4], [1], [4]]), array([[2], [5], [2], [5]]), array([[3], [6], [3], [6]])]
You can use the split function to achieve these two effects at the same time. You can set the axis parameter difference, which is similar to the combination. This is not demonstrated here.
Iii. Matrix
The preceding operations on arrays show that numpy can simulate matrices through arrays, but numpy also has a data structure dedicated to processing matrices-matrix.
1. Generate a Matrix
>>> numpy.mat('1 2 3;4 5 6;7 8 9') matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
2. array matrix conversion
Matrix to array
>>> m=numpy.mat('1 2 3;4 5 6;7 8 9') >>> numpy.array(m) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Array conversion matrix
>>> n=numpy.array(m) >>> numpy.mat(n) matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
3. Matrix Method
Inverse:
>>> m.I matrix([[ -4.50359963e+15, 9.00719925e+15, -4.50359963e+15], [ 9.00719925e+15, -1.80143985e+16, 9.00719925e+15], [ -4.50359963e+15, 9.00719925e+15, -4.50359963e+15]])
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message to us. Thank you for your support.