numpy-the difference between np.array, np.matrix, np.mat and the difference between (*), np.multiply, np.dot multiplication

[1] The difference between np.array, np.matrix, np.mat

(1) The difference between np.array and the latter two

Different data types
np.array produces data of numpy.ndarray type, and cannot perform matrix multiplication. np.matrix, np.mat generate numpy.matrix type data, which can be used for matrix multiplication.
See the following example

import numpy as np


a = np.mat('1 2 3;3 4 5;1 2 3')
b = np.mat([[1, 2], [3, 4]])
print(a)
print(b)
'''
两种方法都可以
[[1 2 3]
 [3 4 5]
 [1 2 3]]
[[1 2]
 [3 4]]
 '''
print(type(a))
print(type(b))
'''
<class 'numpy.matrix'>
<class 'numpy.matrix'>
'''

c = np.array([[1, 2], [3, 4]])
print(c)
'''
[[1 2]
 [3 4]]
'''
print(type(c)) #<class 'numpy.ndarray'>

Explanation : Array is to create an array, its shape is (6,), but the array can be converted to a matrix after using reshape. (See the example below)
The difference between np.array and np.arange : np.array must manually input the array; np.arange is similar to using range directly, and can generate a string of numbers. But both produce arrays and not matrices.

a = np.arange(12).reshape(3,4)
print(a)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
 '''
b = np.array([1,2,3,4,5,6])
print(b.shape)    #(6,)
c = b.reshape(2,3)
print(c.shape)    #(2, 3)

The difference between a one-dimensional array and a one-dimensional matrix
(12,) means that it is an array, and there is only one []; (1, 12) is a one-dimensional matrix, with two [] on the left and right, matrix can make a two-dimensional array into a two-dimensional Matrix, you can turn the array into a one-dimensional matrix

a = np.arange(12)
print(a.shape) #(12,)
#[ 0  1  2  3  4  5  6  7  8  9 10 11]
a = np.matrix(a)
print(a.shape) #(1, 12)
#[[ 0  1  2  3  4  5  6  7  8  9 10 11]]
c = np.matrix([[1,2,3,4,5,6],[7,8,9,10,11,12]], dtype=int)
print(c)
'''
[[ 1  2  3  4  5  6]
 [ 7  8  9 10 11 12]]
 '''

(2) The difference between np.mat and np.matrix

There are two ways to change from an array to a matrix: np.mat and np.matrix.
After using np.mat, if the original array is changed, the matrix will also change accordingly, as follows:

#创建ndarray二维数组
x = np.array([[1, 2], [3, 4]])

#生成 <class 'numpy.matrix'> 矩阵
m = np.mat(x)

#打印 m 矩阵
print(m)
'''
[[1 2]
 [3 4]]
'''
x[0,0] = 0
print(m)
'''
[[0 2]
 [3 4]]
'''

But after using np.matrix, if the original array is changed, the matrix will not change accordingly, as follows:

#创建ndarray二维数组
x = np.array([[1, 2], [3, 4]])

#生成 <class 'numpy.matrix'> 矩阵
m = np.matrix(x)

#打印 m 矩阵
print(m)
'''
[[1 2]
 [3 4]]
'''
x[0,0] = 0
print(m)
'''
[[1 2]
 [3 4]]
'''

[3] The difference between matrix multiplication (*), np.multiply, np.dot

Note : Different types of data have different effects when using them.

(1) When the two being multiplied are arrays

Use np.dot to multiply matrices (2×3 and 3×2=2×2 are multiplied and then added), but the result type of the product is <class'numpy.ndarray'>, which means it is not a real matrix phase Multiply, but you can follow the rule of matrix multiplication.

import numpy as np
a = np.array([[1,2,3],[1,1,1]])
b = np.array([[1,1],[1,1],[1,1]])
print(a)
'''
[[1 2 3]
 [1 1 1]]
'''
print(b)
'''
[[1 1]
 [1 1]
 [1 1]]
'''
print(np.dot(a,b))
'''
[[6 6]
 [3 3]]
'''

If the matrix multiplication rules are not met, the two arrays must have exactly the same form before the operation can be performed.
a*b is the product of corresponding elements, multiply is also the product of corresponding elements, and the result type of the product is <class'numpy.ndarray'>

a = np.array([[1,2,3],[1,1,1]])
b = np.array([[1,1,1],[1,1,1]])
print(a)
'''
[[1 2 3]
 [1 1 1]]
'''
print(b)
'''
[[1 1 1]
 [1 1 1]]
'''
print(a*b)
'''
[[1 2 3]
 [1 1 1]]
'''

(2) When the two being multiplied are mat/matrix

The default a*b and dot(a,b) is the product of the matrix (3×3 and 3×1=3×1 ie m×n and n×y=m×y), the result type is <class'numpy.matrix '>

import numpy as np
a = np.mat('1 2 3;3 4 5;1 2 3')
b = np.mat([[1],[1],[1]])

c = np.dot(a,b)
print(c)
'''
[[ 6]
 [12]
 [ 6]]
'''
c = a*b
print(c)
'''
[[ 6]
 [12]
 [ 6]]
'''

Multiply is converted to the product of the corresponding elements. The multiplication condition is that the two matrices are in exactly the same format or the number of rows is the same and the number of columns is 1 (2×3 and 2×1=3×3, that is, m×n and m×1= m×n), the corresponding position is only multiplied without adding

import numpy as np

a = np.matrix('1 2 3;3 4 5') #相同格式
b = np.matrix([[1,1,1],[2,2,2]])
c = np.multiply(a,b)
print(c)
'''
[[ 1  2  3]
 [ 6  8 10]]
'''

a = np.matrix('1 2 3;3 4 5') #行数相同其中一个列数为1
b = np.matrix([[1],[2]])
c = np.multiply(a,b)
print(c)
'''
[[ 1  2  3]
 [ 6  8 10]]
'''

(3) Summary

np.dot whether the data is array or matrix means matrix multiplication
np.multiply whether the data is array or matrix means matrix multiplication

If the data type is array , (*) and np.multiply represent the corresponding position multiplication, np.dot represents the matrix form multiplication

If the data type is matrix , np.multiply represents the corresponding position multiplication, (*) and np.dot represent the matrix form multiplication

Guess you like

Origin blog.csdn.net/qq_46126258/article/details/107705262