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