網站首頁 編程語言 正文
在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( "http:// operator:\n", a//b, b//a) #運算符//對應于floor_divide 函數
print( "http:// 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.cnblogs.com/codingchen/p/16352881.html
相關推薦
- 2022-05-20 Spring-IOC—基于注解配置Bean
- 2022-07-24 詳解Flutter中StatefulBuilder組件的使用_Android
- 2022-08-17 WPF實現Interaction框架的Behavior擴展_C#教程
- 2022-04-01 Python格式化輸出之format用法詳解_python
- 2022-05-16 通過T-SQL語句創建游標與實現數據庫加解密功能_MsSql
- 2022-06-04 Jenkins安裝的時區問題分析解決_安裝教程
- 2023-04-06 Python求字符串的長度示例代碼_python
- 2023-10-16 element 表格里,每一行都循環使用el-popover組件,關閉按鈕失效問題如何解決?
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支