Numpy the difference matrix () and array () of

From: jeexi

 

Difference matrix () and array (), mainly from the following aspects to start:

 

1. Different matrix generation method

import numpy as np
 
a1 = np.array([[1, 2], [3, 4]])
b1 = np.mat([[1, 2], [3, 4]])
 
a2 = np.array(([1, 2], [3, 4]))
b2 = np.mat(([1, 2], [3, 4]))
 
a3 = np.array(((1,2), (3,4)))
b3 = np.mat(((1,2), (3,4)))
 
b4 = np.mat('1 2; 3 4')
 
print("\n",a1,"\n",b1,"\n",a2,"\n",b2,"\n",a3,"\n",b3,"\n",b4)

The results are:

 [[1 2]
 [3 4]]

The above change is "[]" with "()." Except that in quotation marks, to generate a matrix semicolon and a space within b4, this method can only be used in function Matrix (), i.e. b4 = np.mat ( '1 2; 3 4'). Not written a4 = np.array ( '1 2; 3 4').

 

2. different matrix properties

matrix () and array () followed by .T been transposed. However, matrix () may also be added at the end to give .H conjugate matrix, an inverse matrix obtained .I added, Array () can not.

import numpy as np
 
a1 = np.array([[1, 2], [3, 4]])
b1 = np.mat([[1, 2], [3, 4]])
 
print(a1.T)
print(b1.T)
[[1 3]
 [2 4]]
[[1 3]
 [2 4]]
import numpy as np
a1 = np.array([[1, 2], [3, 4]])
print(a1.H)

AttributeError: 'numpy.ndarray' object has no attribute 'H' 

print(a1.I)

AttributeError: 'numpy.ndarray' object has no attribute 'I' 

import numpy as np
b1 = np.mat([[1, 2], [3, 4]])
 
print(b1.H)
print(b1.I)

[[1 3]
 [2 4]]
[[-2.   1. ]
 [ 1.5 -0.5]]

3. In the matrix multiplication of different

Import numpy AS NP 
 
A1 = np.array ([[. 1, 2], [. 3,. 4 ]]) 
C1 = np.array ([[5,6], [7, 8 ]]) 
 
B1 = np.mat ( [[. 1, 2], [. 3,. 4 ]]) 
D1 = np.mat ([[5,6], [7, 8 ]]) 
 
Print ( " A1 c1 is multiplied result: " , A1 * c1)
 Print ( " the results of d1 b1 take: " , b1 * d1)

a1 c1 is multiplied Results: [[12 is. 5]
 [21 is 32]]
B1 results by d1: [[1922]
 [4350]]

array () multiplication of multiply two matrices corresponding position .

MAT () multiplication is matrix multiplication .

array () and mat (), so that if they follow the matrix multiplication, you can use dot () function .

print(np.dot(a1,c1))
print(np.dot(b1,d1))

[[19 22]
 [43 50]]
[[19 22]
 [43 50]] 

 

Guess you like

Origin www.cnblogs.com/keye/p/11195428.html