在NumPy中,矩陣是 ndarray 的子類,與數學概念中的矩陣一樣,NumPy中的矩陣也是二維的,可以使用 mat 、 matrix 以及 bmat 函式來創建矩陣,
一、創建矩陣
mat 函式創建矩陣時,若輸入已為 matrix 或 ndarray 物件,則不會為它們創建副本, 因此,呼叫 mat() 函式和呼叫 matrix(data, copy=False) 等價,
1) 在創建矩陣的專用字串中,矩陣的行與行之間用分號隔開,行內的元素之間用空格隔開,使用如下的字串呼叫 mat 函式創建矩陣:
import numpy as np A = np.mat('1 2 3; 4 5 6; 7 8 9') print("Creation from string:", A)
運行結果:
Creation from string: [[1 2 3] [4 5 6] [7 8 9]]
2)用T屬性獲取轉置矩陣
print("transpose A:", A.T) # 用T屬性獲取轉置矩陣
3)用I屬性獲取逆矩陣
print("Inverse A:", A.I) # 用I屬性獲取逆矩陣
4)用NumPy陣列進行創建矩陣
B = np.mat(np.arange(9).reshape(3, 3)) print("Creation from array:", B)#使用NumPy陣列進行創建
上述運行結果:
Creation from string:
[[1 2 3] [4 5 6] [7 8 9]] transpose A:
[[1 4 7] [2 5 8] [3 6 9]] Inverse A:
[[ 3.15251974e+15 -6.30503948e+15 3.15251974e+15] [-6.30503948e+15 1.26100790e+16 -6.30503948e+15] [ 3.15251974e+15 -6.30503948e+15 3.15251974e+15]] Creation from array:
[[0 1 2] [3 4 5] [6 7 8]]
二、從已有矩陣創建新矩陣
希望利用一些已有的較小的矩陣來創建一個新的大矩陣,這可以用 bmat 函式來實作,這里的 b 表示“分塊”, bmat 即分塊矩陣(block matrix),
1)先創建一個3*3的單位矩陣:
C = np.eye(3) print("C:",C)
運行結果:
C: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
2)創建一個與C同型的矩陣,乘以2
D = 2 * C print ("D:",D)
運行結果:
D:
[[2. 0. 0.] [0. 2. 0.] [0. 0. 2.]]
3)使用字串創建復合矩陣:
字串的格式與 mat 函式中一致,只是在這里你可以用矩陣變數名代替數字:
print("Compound matrix\n", np.bmat("C D;C D"))
運行結果:
Compound matrix: [[1. 0. 0. 2. 0. 0.] [0. 1. 0. 0. 2. 0.] [0. 0. 1. 0. 0. 2.] [1. 0. 0. 2. 0. 0.] [0. 1. 0. 0. 2. 0.] [0. 0. 1. 0. 0. 2.]]
三、通用函式
通用函式的輸入是一組標量,輸出也是一組標量,它們通常可以對應于基本數學運算,如加、減、乘、除等,
1、使用NumPy中的 frompyfunc 函式,通過一個Python函式來創建通用函式,步驟如下:
1)定義一個回答某個問題的Python函式
2)用 zeros_like 函式創建一個和 a 形狀相同,并且元素全部為0的陣列 result
3)將剛生成的陣列中的所有元素設定其值為42
2、在 add 上呼叫通用函式的方法
通用函式并非真正的函式,而是能夠表示函式的物件,通用函式有四個方法,不過這些方法只對輸入兩個引數、輸出一個引數的ufunc物件有效,例如 add 函式,
其他不符合條件的ufunc物件呼叫這些方法時將拋出 ValueError 例外,因此只能在二元通用函式上呼叫這些方法,以下將逐一介紹這4個方法:
reduce()、accumulate()、 reduceat()、outer()
1) 沿著指定的軸,在連續的陣列元素之間遞回呼叫通用函式,即可得到輸入陣列的規約(reduce)計算結果,
對于 add 函式,其對陣列的reduce計算結果等價于對陣列元素求和,呼叫reduce 方法:
a = np.arange(9) print("Reduce:", np.add.reduce(a)) #呼叫add函式的reduce方法
運行結果:
Reduce 36
2) accumulate 方法同樣可以遞回作用于輸入陣列
在 add 函式上呼叫 accumulate 方法,等價于直接呼叫 cumsum 函式,在 add 函式上呼叫 accumulate 方法:
print( "Accumulate", np.add.accumulate(a)) #呼叫add函式的accumulate方法
運行結果:
Accumulate [ 0 1 3 6 10 15 21 28 36]
3)educeat 方法需要輸入一個陣列以及一個索引值串列作為引數,
print ("Reduceat", np.add.reduceat(a, [0, 5, 2, 7]))
educeat 方法的作用是,在數列a中,分別計算索引間的累加,比如上述的 [0, 5, 2, 7],分別計算索引0-5,5-2(5>2,所以直接取索引為5的資料),2-7,7-(-1) 等四組序列形成的
比如,0-5就是計算A-E列中的資料,結果為10;5-2,直接取索引為5,即F的資料5;2-7,即B-G的計算結果為20;7-(-1)即索引7到最后,也即H、I的計算結果為15,

4)outer 方法
回傳一個陣列,它的秩(rank)等于兩個輸入陣列的秩的和,它會作用于兩個輸入陣列之間存在的所有元素對,在 add 函式上呼叫 outer 方法:
print("Outer:\n", np.add.outer(np.arange(3), a))
運行結果:
Outer: [[ 0 1 2 3 4 5 6 7 8] [ 1 2 3 4 5 6 7 8 9] [ 2 3 4 5 6 7 8 9 10]]
四、算術運算
在NumPy中,基本算術運算子+、-和 * 隱式關聯著通用函式 add 、 subtract 和 multiply ,對NumPy陣列使用這些算術運算子時,對應的通用函式將自動被呼叫,除法包含
的程序則較為復雜,在陣列的除法運算中涉及
三個通用函式 divide 、 true_divide 和floor_division ,以及兩個對應的運算子 / 和 // ,
1、除法運算:
import numpy as np a = np.array([2, 6, 5]) b = np.array([1, 2, 3]) print("Divide:\n", np.divide(a, b), np.divide(b, a))
除了divide()函式外,還有floor_divide(),以及運算子‘/’和‘//’,(‘/’和‘//’分別和divide和floor_divide作用一樣)如下代碼:
import numpy as np a = np.array([2, 6, 5]) b = np.array([1, 2, 3]) print("Divide:\n", np.divide(a, b), np.divide(b, a)) print("True Divide:\n", np.true_divide(a, b), np.true_divide(b, a))#回除法的浮點數結果而不作截斷 print("Floor Divide:\n", np.floor_divide(a, b), np.floor_divide(b, a)) #回傳整數結果 c = 3.14*b print("Floor Divide2:\n", np.floor_divide(c, b), np.floor_divide(b, c)) #回傳整數結果 print( "/ operator:\n", a/b, b/a) # "/"運算子相當于呼叫 divide 函式 print( "// operator:\n", a//b, b//a) #運算子//對應于floor_divide 函式 print( "// operator2:\n", c//b, b//c)
運行結果:
Divide: [2. 3. 1.66666667] [0.5 0.33333333 0.6 ] True Divide: [2. 3. 1.66666667] [0.5 0.33333333 0.6 ] Floor Divide: [2 3 1] [0 0 0] Floor Divide2: [3. 3. 3.] [0. 0. 0.] / operator: [2. 3. 1.66666667] [0.5 0.33333333 0.6 ] // operator: [2 3 1] [0 0 0] // operator2: [3. 3. 3.] [0. 0. 0.]
2、模運算
計算模數或者余數,可以使用NumPy中的 mod 、 remainder 和 fmod 函式,當然,也可以使用 % 運算子,這些函式的主要差異在于處理負數的方式,
a = np.arange(-4, 4) print('a:',a) print ("Remainder", np.remainder(a, 2)) # remainder 函式逐個回傳兩個陣列中元素相除后的余數 print ("Mod", np.mod(a, 2)) # mod 函式與 remainder 函式的功能完全一致 print ("% operator", a % 2) # % 運算子僅僅是 remainder 函式的簡寫 print ("Fmod", np.fmod(a, 2))# fmod 函式處理負數的方式與 remainder 、 mod 和 % 不同
運行結果:
a: [-4 -3 -2 -1 0 1 2 3] Remainder [0 1 0 1 0 1 0 1] Mod [0 1 0 1 0 1 0 1] % operator [0 1 0 1 0 1 0 1] Fmod [ 0 -1 0 -1 0 1 0 1]
實際代碼運行如下:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487451.html
標籤:Python
上一篇:為什么使用這行代碼不起作用?
