1. Linear algebra
NumPy the operation of multidimensional arrays by default does not use matrix operations, and the matrix operations can be done by matrix objects or matrices functions;
The Matrix object is created by the Matrix class, and its arithmetic are implemented by default in matrix operations, similar to MATLAB 10 >:
A = Np.matrix ([[1,2,3],[4,5,6],[7,8,9]])
Matrix ([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
A * a
Matrix ([[30, 36, 42],
[66, 81, 96],
[102, 126, 150]])
A + A
Matrix ([[2, 4, 6],
[8, 10, 12],
[14, 16, 18]])
A * *-1
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]])
Since there are already ndarray in NumPy, it is easy to confuse with matrix;
Matrix Product Operation:
For Ndarray objects, NumPy provides multiple matrix multiplication operations: Dot (), inner (), outer ()
Dot (): For two one-dimensional arrays, computes the sum of the two arrays corresponding to the subscript element, namely: inner product; for a two-dimensional array, the matrix product of two arrays is computed; for multidimensional arrays, each element in the knot > Fruit array is: all elements on the last dimension of array A and array B are the penultimate dimension The sum of all the elements on >:
Dot (A, b) [i,j,k,m] = SUM (A[I,J,:] * b[k,:,m])
The result array C can be regarded as the product of multiple sub-matrices of arrays A and b;
Inner (): For a one-dimensional array, the inner product of the two arrays is computed; for multidimensional arrays, each element in the computed result array is: The inner product of the last dimension of the array A and B, so the last > Dimension of A and B must be the same length:
Inner (A, b) [i,j,k,m] = SUM (a[i,j,:]*b[k,m,:])
Outer (): If the passed-in parameter array is a multidimensional array, the array is flattened into an array and then computed to get the outer product of two one-dimensional arrays:
?
Outer ([1,2,3],[4,5,6,7])
Array ([[4, 5, 6, 7],
[8, 10, 12, 14],
[12, 15, 18, 21]])
Solving systems of linear equations
More advanced operations on matrices can be found in the Linalg module of NumPy:
Np.linalg.solve () passes through two parameter arrays, A is a two-dimensional array of n*n, and B is a one-dimensional array of length n, satisfying: A * x = B, the solution of the X-matrix is the solution of the N-element one-time equation;
NP.LINALG.LSTSQ () incoming parameter array does not require a array to be a square, the number of equations can be > less than the number of unknowns, LSTSQ () The result of the calculation is to make | B-A * x | The minimum of a > Group solution, which is called the least squares solution, makes the sum of squares and minima of the errors of all equations.
2. Mask Array
The Numpy.ma module provides the processing of the mask array, which almost completely replicates all the functions in the NumPy and provides the function of the mask array;
A mask array consists of a normal array and a Boolean array, and the > element with a value of true in the Boolean array indicates that the corresponding underlying value in the normal array is invalid, and false indicates valid;
To create a mask array:
To create a mask array:
Import numpy.ma as Ma
x = Np.array ([1,2,3,5,7,4,3,2,8,0])
Mask = x < 5
mx = Ma.array (x,mask=mask)
Mask
Array ([True, True, True, False, False, True, True, True, false, True], Dtype=bool)
Mx
Masked_array (data = [------5 7------8--],
Mask = [True True True false false true] True
Fill_value = 999999)
The mask array has three properties: data, Mask, Fill_value;data represents the original numeric array >,mask represents the Boolean array used to obtain the mask, and fill_value represents the padding value in place of the > array of invalid values, which is passed filled () Method view;
The mask array can be accessed using a variety of subscript objects, and in the part of the masked value is Masked>, you can set a position value of ma.masked to invalidate it;
3. File access
NumPy provides a variety of access to the array contents of the file Operation function, the saved array data can be binary format or text format, binary format can be unformatted binary and numpy special format binary type;
The ToFile () method writes the array data to an unformatted binary file, ToFile () outputs data that does not > holds information such as the shape and element type of the array, and the FromFile () function can read the unformatted binary > file, at which point It is necessary to set the element type of the array correctly dtype and follow the correct shape > conversion operation, if the SEP parameter is specified, ToFile (), FromFile () will be input and output in text format, and Sep specifies the delimiter of the text;
Load (), save () saves the array data in a numpy-specific binary file, automatically handles information such as element type and shape, and if you save multiple arrays at once, you can use Savez (), and the first argument of the Savez () function is the file name. The arguments that follow are the arrays that need to be saved, or you can use the keyword argument to name the array, and the non-keyword parameter arrays are automatically named Arr_0, arr_1 、... And so on, Savez () outputs a compressed file with an extension of NPZ, each of which is > a npy file saved with Save (), with the same file name and array name. Load () automatically recognizes the Npz> file and returns a dictionary-like object, which can be extracted by an array named key;
The Savetxt (), Loadtxt () function can read and write a text file that holds a one-dimensional and two-dimensional array, outputs the text separated by the spacer, specifies the spacer by the delimiter parameter, and the default output is in the form of '%.18e ', separated by a space by default.
4. Memory-mapped arrays
Creates a memory-mapped array from a file that reads the specified offset data,> without reading the entire file into memory by Memmap ():
FileName: Array file
Dtype:[uint8], Element type
Mode:[r+], read mode
Offset:[0], offset
Shape: The shapes read
ORDER:[C], the element arrangement format, the default is the C language format, F is the FORTRAN format;
Python Scientific Computing _numpy_ linear algebra/mask array/Memory map array