1. Numpy(Numberical Python)
NumPy is integrated in Anaconda and can be used directly. If you want to install it yourself, you can use the popular Python
The package installer pip installs NumPy, and I'm currently using the Anaconde environment to learn and use the library.
1.1 Installation of the library
CMD :pip install numpy
Or use the Tsinghua source image library: pip install numpy -i /simple
(direct copy available)
If you want to check the version of this library:
import numpy as np np.__version__
In fact, many other libraries can also be used to view the version of the library using this method
basic operation
Numpy operates on an ndarray, so you need to transform the data when using this library for calculations
2.1 Creation of arrays: ()
You can convert lists, tuples, or other sequences into ndarrays by default creating a new
ndarray
We will find that the original data inside the int, float type data have been converted to the same type, unified into float type data, because Numpy is required to array inside the data type is consistent
2.2 Creation of N-dimensional arrays
Numpy can generate not only one-dimensional arrays, but also multi-dimensional arrays.
For example, the following two-dimensional array can be generated:
2.3 Common Arrays
2.4 Generation of linear arrays
2.5 Array operations
Simple quadratic operations with elements in corresponding positions in arrays.
Note here that the number of elements in array x and array y are the same (both are one-dimensional arrays with 3 elements)
When both have the same number of elements, arithmetic operations can be performed on each element. If the number of elements is different, an error will be reported, so keep the
It is very important to hold a consistent number of elements.
In addition, Numpy arrays can not only perform arithmetic operations on the corresponding elements, but can also be combined with a single value (scalar) into the
line arithmetic (this feature is also known as broadcasting and will be described in more detail later), for example:
will convert the data to the default float data type of the array.
2.6 Shape transformations of arrays
We start by defining an array
If the magnitude of the array is large, for calculations where only one of the other dimension numbers is known, then you can use the -1 below, as a placeholder, which will automatically fill in the other dimension information for you
In addition to this, there is a more common method of flattening arrays, () and (), which are similar to the
3. Indexing of arrays
This two-dimensional array can be viewed as a matrix, three rows and four columns, through the number of subscript index for the extraction of the number, where the slicing operation is still left-closed right-open
4. Broadcast function
The term broadcast refers to NumPy's ability to handle arrays of different shapes during arithmetic operations.
A broadcast can be simply understood as a set of rules used for calculations (addition, subtraction, multiplication, division, etc.) on arrays of different sizes.
If the arrays have the same shape, the corresponding elements are computed one by one, but if the dimensions of the arrays are not the same, you need to use the broadcast mechanism
Numpy's broadcasts follow a strict set of rules:
Rule 1: If two arrays do not have the same number of dimensions, the shape of the array with the smaller dimension will be 1'd to the left Rule 2: If the shapes of the two arrays do not match in any dimension, the shape of the arrays will be expanded along the dimension with a dimension of 1 to match the shape of the other array Rule 3: If the shapes of the two arrays do not match in any dimension, and no dimension is 1, then an exception is thrown that cannot be broadcast. then an exception is thrown that cannot be broadcast
This article on Python data cleaning tools Numpy's basic operation of the article is introduced to this, more related Python data cleaning content, please search for my previous posts or continue to browse the following related articles I hope you will support me more in the future!