In NumPy, array is used to represent a generic n-dimensional array, and the matrix is specific for linear algebra calculations. Both the array and the matrix can be used to represent matrices, and there are some differences in multiplication operations.
When using array, the operator * is used to calculate the quantity product (point multiplication), and the function dot() is used to calculate the cross product (cross-multiply), as in the example:
Import= Np.array ([[1, 2], [3, 4= Np.array ([[5, 6], [7, 8print'A * b = \ n', A * bprint'dot (A, b) = \ n', Np.dot (A, B) The result of the operation is:*B = 5] [[ [43] [50]]
Visible, when a and B are arrays,A*b calculates the quantity product of A and B (corresponding to MATLABA * b ), dot (a, ba< Span class= "crayon-h" > * b )。
Unlike array, when using the matrix, the operator * is used to calculate cross product , and the function multiply() isused to calculate the quantity product , as in the example:
ImportNumPy as NP a= Np.mat ('1 2; 3 4') b= Np.mat ('5 6; 7 8'); Print 'A * b = \ n'AbPrint 'Multiply (A, b) = \ n', Np.multiply (A, B) runs the result: a* B = [[19 22] [43 50]]multiply (A, b)= [[ 5 12] [21 32]]
It can be seen that when a and B are matrix, a * B calculates the cross product of A and B, with multiply(a, b) The quantity product of a and B is calculated. When using matrix, whether it is to generate matrices or calculations, numpy style and matlab closer, reducing the burden of language switching.
Python (): Array and matrix operations