詳解Python NumPy中矩陣和通用函數(shù)的使用
在NumPy中,矩陣是 ndarray 的子類,與數(shù)學概念中的矩陣一樣,NumPy中的矩陣也是二維的,可以使用 mat 、 matrix 以及 bmat 函數(shù)來創(chuàng)建矩陣。
一、創(chuàng)建矩陣
mat 函數(shù)創(chuàng)建矩陣時,若輸入已為 matrix 或 ndarray 對象,則不會為它們創(chuàng)建副本。 因此,調(diào)用 mat() 函數(shù)和調(diào)用 matrix(data, copy=False) 等價。
1) 在創(chuàng)建矩陣的專用字符串中,矩陣的行與行之間用分號隔開,行內(nèi)的元素之間用空格隔開。使用如下的字符串調(diào)用 mat 函數(shù)創(chuàng)建矩陣:
import numpy as np A = np.mat('1 2 3; 4 5 6; 7 8 9') print("Creation from string:", A)
運行結(jié)果:
Creation from string:
[[1 2 3]
[4 5 6]
[7 8 9]]
2)用T屬性獲取轉(zhuǎn)置矩陣
print("transpose A:", A.T) # 用T屬性獲取轉(zhuǎn)置矩陣
3)用I屬性獲取逆矩陣
print("Inverse A:", A.I) # 用I屬性獲取逆矩陣
4)用NumPy數(shù)組進行創(chuàng)建矩陣
B = np.mat(np.arange(9).reshape(3, 3)) print("Creation from array:", B)#使用NumPy數(shù)組進行創(chuàng)建
上述運行結(jié)果:
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]]
二、從已有矩陣創(chuàng)建新矩陣
希望利用一些已有的較小的矩陣來創(chuàng)建一個新的大矩陣。這可以用 bmat 函數(shù)來實現(xiàn)。這里的 b 表示“分塊”, bmat 即分塊矩陣(block matrix)。
1)先創(chuàng)建一個3*3的單位矩陣:
C = np.eye(3) print("C:",C)
運行結(jié)果:
C:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
2)創(chuàng)建一個與C同型的矩陣,乘以2
D = 2 * C print ("D:",D)
運行結(jié)果:
D:
[[2. 0. 0.]
[0. 2. 0.]
[0. 0. 2.]]
3)使用字符串創(chuàng)建復合矩陣:
字符串的格式與 mat 函數(shù)中一致,只是在這里你可以用矩陣變量名代替數(shù)字:
print("Compound matrix\n", np.bmat("C D;C D"))
運行結(jié)果:
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.]]
三、通用函數(shù)
通用函數(shù)的輸入是一組標量,輸出也是一組標量,它們通??梢詫诨緮?shù)學運算,如加、減、乘、除等。
1、使用NumPy中的 frompyfunc 函數(shù),通過一個Python函數(shù)來創(chuàng)建通用函數(shù),步驟如下:
1)定義一個回答某個問題的Python函數(shù)
2)用 zeros_like 函數(shù)創(chuàng)建一個和 a 形狀相同,并且元素全部為0的數(shù)組 result
3)將剛生成的數(shù)組中的所有元素設置其值為42
2、在 add 上調(diào)用通用函數(shù)的方法
通用函數(shù)并非真正的函數(shù),而是能夠表示函數(shù)的對象。通用函數(shù)有四個方法,不過這些方法只對輸入兩個參數(shù)、輸出一個參數(shù)的ufunc對象有效,例如 add 函數(shù)。
其他不符合條件的ufunc對象調(diào)用這些方法時將拋出 ValueError 異常。因此只能在二元通用函數(shù)上調(diào)用這些方法。以下將逐一介紹這4個方法:
reduce()、accumulate()、 reduceat()、outer()
1) 沿著指定的軸,在連續(xù)的數(shù)組元素之間遞歸調(diào)用通用函數(shù),即可得到輸入數(shù)組的規(guī)約(reduce)計算結(jié)果。
對于 add 函數(shù),其對數(shù)組的reduce計算結(jié)果等價于對數(shù)組元素求和。調(diào)用reduce 方法:
a = np.arange(9) print("Reduce:", np.add.reduce(a)) #調(diào)用add函數(shù)的reduce方法
運行結(jié)果:
Reduce 36
2) accumulate 方法同樣可以遞歸作用于輸入數(shù)組
在 add 函數(shù)上調(diào)用 accumulate 方法,等價于直接調(diào)用 cumsum 函數(shù)。在 add 函數(shù)上調(diào)用 accumulate 方法:
print( "Accumulate", np.add.accumulate(a)) #調(diào)用add函數(shù)的accumulate方法
運行結(jié)果:
Accumulate [ 0 1 3 6 10 15 21 28 36]
3)educeat 方法需要輸入一個數(shù)組以及一個索引值列表作為參數(shù)。
print ("Reduceat", np.add.reduceat(a, [0, 5, 2, 7]))
educeat 方法的作用是,在數(shù)列a中,分別計算索引間的累加,比如上述的 [0, 5, 2, 7],分別計算索引0-5,5-2(5>2,所以直接取索引為5的數(shù)據(jù)),2-7,7-(-1) 等四組序列形成的
比如,0-5就是計算A-E列中的數(shù)據(jù),結(jié)果為10;5-2,直接取索引為5,即F的數(shù)據(jù)5;2-7,即B-G的計算結(jié)果為20;7-(-1)即索引7到最后,也即H、I的計算結(jié)果為15。
4)outer 方法
返回一個數(shù)組,它的秩(rank)等于兩個輸入數(shù)組的秩的和。它會作用于兩個輸入數(shù)組之間存在的所有元素對。在 add 函數(shù)上調(diào)用 outer 方法:
print("Outer:\n", np.add.outer(np.arange(3), a))
運行結(jié)果:
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]]
四、算術(shù)運算
在NumPy中,基本算術(shù)運算符+、-和 * 隱式關(guān)聯(lián)著通用函數(shù) add 、 subtract 和 multiply ,對NumPy數(shù)組使用這些算術(shù)運算符時,對應的通用函數(shù)將自動被調(diào)用。除法包含
的過程則較為復雜,在數(shù)組的除法運算中涉及
三個通用函數(shù) 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()函數(shù)外,還有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))#回除法的浮點數(shù)結(jié)果而不作截斷 print("Floor Divide:\n", np.floor_divide(a, b), np.floor_divide(b, a)) #返回整數(shù)結(jié)果 c = 3.14*b print("Floor Divide2:\n", np.floor_divide(c, b), np.floor_divide(b, c)) #返回整數(shù)結(jié)果 print( "/ operator:\n", a/b, b/a) # "/"運算符相當于調(diào)用 divide 函數(shù) print( "http:// operator:\n", a//b, b//a) #運算符//對應于floor_divide 函數(shù) print( "http:// operator2:\n", c//b, b//c)
運行結(jié)果:
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、模運算
計算模數(shù)或者余數(shù),可以使用NumPy中的 mod 、 remainder 和 fmod 函數(shù)。當然,也可以使用 % 運算符。這些函數(shù)的主要差異在于處理負數(shù)的方式。
a = np.arange(-4, 4) print('a:',a) print ("Remainder", np.remainder(a, 2)) # remainder 函數(shù)逐個返回兩個數(shù)組中元素相除后的余數(shù) print ("Mod", np.mod(a, 2)) # mod 函數(shù)與 remainder 函數(shù)的功能完全一致 print ("% operator", a % 2) # % 操作符僅僅是 remainder 函數(shù)的簡寫 print ("Fmod", np.fmod(a, 2))# fmod 函數(shù)處理負數(shù)的方式與 remainder 、 mod 和 % 不同
運行結(jié)果:
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]
實際代碼運行如下:
以上就是詳解Python NumPy中矩陣和通用函數(shù)的使用的詳細內(nèi)容,更多關(guān)于NumPy矩陣 通用函數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
django框架基于模板 生成 excel(xls) 文件操作示例
這篇文章主要介紹了django框架基于模板 生成 excel(xls) 文件操作,結(jié)合具體實例形式分析了Django框架基于模板生成excel的實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2019-06-06python的ping網(wǎng)絡狀態(tài)監(jiān)測的實現(xiàn)(含多IP)
本文主要介紹了python的ping網(wǎng)絡狀態(tài)監(jiān)測的實現(xiàn)(含多IP),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03