日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

詳解Python?NumPy中矩陣和通用函數(shù)的使用_python

作者:PursuitingPeak ? 更新時(shí)間: 2022-08-02 編程語(yǔ)言

在NumPy中,矩陣是 ndarray 的子類,與數(shù)學(xué)概念中的矩陣一樣,NumPy中的矩陣也是二維的,可以使用 mat 、 matrix 以及 bmat 函數(shù)來創(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)建矩陣的專用字符串中,矩陣的行與行之間用分號(hào)隔開,行內(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)

運(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)建新矩陣

希望利用一些已有的較小的矩陣來創(chuàng)建一個(gè)新的大矩陣。這可以用 bmat 函數(shù)來實(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ù),通過一個(gè)Python函數(shù)來創(chuàng)建通用函數(shù),步驟如下:

1)定義一個(gè)回答某個(gè)問題的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è)方法,不過這些方法只對(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)用。除法包含

的過程則較為復(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)寫

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)行如下:

原文鏈接:https://www.cnblogs.com/codingchen/p/16352881.html

欄目分類
最近更新