本教程適合於numpy基礎入門,更多詳盡內容請閱讀官網http://www.numpy.org/,此篇爲numpy基礎學習教程系列之矩陣的簡單屬性和方法篇,傾向於實踐用法,後續還會推出一系列numpy其它方面的教程,歡迎廣大圈友一起交流學習,並指出其中的錯誤。
注意:以下np爲import numpy as np中的np標識符
mat函數創建矩陣時,若輸入爲已知的ndarray或matrix對象,則不會創建副本,調用mat函數和調用matrix函數等價。
在創建矩陣的專用字符串中,行與行之間用分號隔開,行內元素用空格隔開。
實例1:(用專用字符串創建矩陣)
A=np.mat("1 2 3;4 5 6")
print A
結果:[[1 2 3]
[4 5 6]]
實例2:(用numpy數組創建矩陣)
B = np.mat(np.arange(9).reshape(3,3))
print B
結果:[[1 2 3]
[4 5 6]
[7 8 9]]
T屬性獲取矩陣的轉置
A = np.mat("1 2 3;4 5 6")
print A.T
結果:[[1 4]
[2 5]
[3 6]]
I屬性獲取矩陣的逆矩陣
A = np.mat("1 2 3;4 5 6;7 8 10")
print A.I
結果:[[-0.66666667 -1.33333333 1. ]
[-0.66666667 3.66666667 -2. ]
[ 1. -2. 1. ]]
創建分塊矩陣
A = np.eye(2)
B = 2*A
print np.bmat("A B;A B")
結果:[[ 1. 0. 2. 0.]
[ 0. 1. 0. 2.]
[ 1. 0. 2. 0.]
[ 0. 1. 0. 2.]]