python numpy--矩陣的通用函式
阿新 • • 發佈:2018-11-10
一、概念
通用函式(ufunc)是一種對ndarray中的資料執行元素級運算的函式。你可以將其看作簡單函式(接受一個或多個標量值,併產生一個或多個標量值)的向量化包裝器通用函式的輸入是一組標量,輸出也是一組標量,它們通常可以對應於基本數學運算,如加、減、乘、除等。
二、分類
1.一元函式
它們接受一個數組。返回一個結果陣列,當然也能返回兩個陣列(modf函式),但是這種的不是很常見;
(1)abs fabs
import numpy as np #匯入模組
a = np.mat(np.arange(-4,3)) #建立一個矩陣
np.abs(a) # 對矩陣a取絕對值
np.fabs(a) # 對矩陣a取浮點類的絕對值
(2) sqrt () 平方根 square() 平方
b = np.mat(range(1,6)) #建立一個矩陣
np.sqrt(b) #b的平方根
np.square(b) #b的平方
(3)log log10 log2 log1p
c = np.mat([1,2,np.e,np.e+1,4,10,100]) #建立一個矩陣
np.log(c) #以e為底
np.log10(c)# log以10為底
np.log2(c)#log2以2為底
np.log1p(c) #在c的基礎上每一個值加上一個1,再以e為底的對數 log1p(x)==log(1+x)
np.log1p(np.e-1)
(4)sign ceil floor rint
d = np.mat([
[2.3,4.6],
[1.2,1.8]
]) #建立一個矩陣
np.sign(d) #符號位 +1:正數 -1:負數 0:0
np.ceil(d) #向上取整 右
np.floor(d)#向下取整 左
np.rint(d) #四捨五入
e = np.mat([
[1,4,8],
[2,3,7]
])
# e*0.1 #快速變成浮點數
np.rint(e)#四捨五入的方法也可以
(5)modf 分別返回小數部分和整數部分
這就是輸入一個數組,返回兩個陣列的函式
arr1,arr2=np.modf(d)
#arr1 返回的是小數部分,arr2返回的是整數部分
(6)isnan() 判斷不是數字
nan: not a number
f=np.array([1,2,np.nan,np.nan,3]) #建立一個矩陣 不是數字的就轉換為np.nan np.inf 是無窮大,是個數字型別
np.isnan(f)
(7)cos sin tan
g=np.mat([0,np.pi/4,np.pi/2,np.pi*3/4]) #建立一個矩陣,裡面表示的是角度
g*2 #所有的角度都放大2倍
np.cos(g) # 求角度的cos值
np.set_printoptions(precision=3)#科學計數法設定顯示3位小數,作為了解吧!
np.tan(g) #求角度的tan值
(8)logical_not
import numpy as np
a = np.mat(np.arange(-4,3))
print(a)
b = np.logical_not(a)
print(b)
只有0為真,其他都為假
2.二元函式
它們接受兩個陣列,並返回一個結果陣列。
#準備三個矩陣
a = np.mat([1,2,3,4])
b = np.mat([5,6,7,8])
c = np.mat([9,10,11,12])
(1)power() 求冪
np.power(b,a) #矩陣本身是二維的,有人問為什麼返回的結果是兩個中括號
np.power(b,2)
(2)maximum、minimum 元素級運算
如果兩個矩陣的元素不一樣多的話則會報錯
#準備兩個矩陣
arr1 = np.mat([1,8,2,9])
arr2 = np.mat([6,3,5,4])
np.maximum(arr1,arr2)
matrix([[6, 8, 5, 9]])
返回的是兩個陣列中對應位大的數值。
np.minimum(arr1,arr2)
matrix([[1, 3, 2, 4]])
返回的是兩個陣列中對應位小的數值
(3)greater 大於 ,greater_equal 大於等於
得到的是布林矩陣或則陣列
np.greater(arr1,arr2)
matrix([[False, True, False, True]])
(4)邏輯"與":logical_and ,“或”:logical_or,“非”:logical_xor
在python中非0為真
#準備一個矩陣
d = np.mat('2 0;1 0')
e = np.mat('0 2;1 0')
#與
np.logical_and(d,e) #對應位都為真,結果為真,否則為假
matrix([[False, False],
[ True, False]])
#或
np.logical_or(d,e) #對應位其中一位為真則為真,都為假則為假
matrix([[ True, True],
[ True, False]])
#非
np.logical_xor(d,e) #相同為false ,不同是true
matrix([[ True, True],
[False, False]])
三、自定義通用函式
步驟:
step1:定義並設定函式內容
step2:使用np.frompyfunc(函式名,輸入引數個數 Int ,輸出值的個數 int)建立通用函式
1、自定義函式1,簡單定義寫個程式碼
# 寫一個通用函式 返回與引數結構相同的zero矩陣
#step1:
def copyshape(a):
return np.zeros_like(a)
#step2:
ucopyshape = np.frompyfunc(copyshape,1,1)
#step3:使用函式
f = np.mat('1,2;3,4') #建立一個2*2的矩陣
ucopyshape(f) #返回的是與f矩陣相同結構2*2的值為0 的矩陣
matrix([[0, 0],
[0, 0]], dtype=object)
2、自定義函式2,返回所有元素的平方,傳入一個引數,輸出一個引數
# step1:
def square(a): # 定義函式名和引數
return a**2 # 返回引數的平方
#step2
usquare = np.frompyfunc(square,1,1) #使用該函式建立通用函式,傳入一個引數,輸出一個引數
#step3:使用這個通用函式
usquare(np.mat('1 3 5 7'))
matrix([[1, 9, 25, 49]], dtype=object)
3、自定義函式3,返回兩個矩陣對應位的平方和,傳入2個,輸出1個
# step1
def square_add(a,b):
return a**2 + b**2
#step2
usquare_add = np.frompyfunc(square_add,2,1)
#step3:使用引數
g1 = np.mat('1 2 3 4')
g2 = np.mat('6 5 4 3')
usquare_add(g1,g2)
4、自定義函式4,返回一個矩陣的平方 第二個矩陣的立方 傳入兩個引數,輸出2個引數
# step1
def square_cubic(a,b):
return a**2,b**3
#step2
usquare_cubic = np.frompyfunc(square_cubic,2,2)
#step3:使用函式
a,b = usquare_cubic(np.mat('1 2 3'),np.mat('4 5 6')) #因為輸出的是2個,所以放2個變數來進行儲存
四、numpy中已有的通用函式
有四種:
1…add.accumulate()
遞迴作用於輸入的陣列,將運算的中間結果返回
axis決定方向
a = np.arange(9) #準備一個數組
np.add.accumulate(a)
array([ 0, 1, 3, 6, 10, 15, 21, 28, 36], dtype=int32)
以下加入axis:
axis=0表述列
axis=1表述行
a.reshape(3,3) #把陣列a變成3*3的陣列
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
np.add.accumulate(a.reshape(3,3),axis=0) #豎著加
array([[ 0, 1, 2],
[ 3, 5, 7],
[ 9, 12, 15]], dtype=int32)
np.add.accumulate(a.reshape(3,3),axis=1) #橫著加
array([[ 0, 1, 3],
[ 3, 7, 12],
[ 6, 13, 21]], dtype=int32)
2.add.reduce() 求和
axis決定方向
a陣列是:
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
np.add.reduce(a) #引數要是一個數組,矩陣就不適用了
36
以下加入axis:
b=np.arange(12).reshape(3,4) #準備一個3行4列的陣列
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
np.add.reduce(b,axis=0) #豎著加
array([12, 15, 18, 21])
np.add.reduce(b,axis=1) #橫著加
array([ 6, 22, 38])
3.add.reduceat()
需要輸入陣列以及索引值列表作為引數 按照區間計算方式進行求和
a陣列是:
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
#相當於sum(a[0:5]) sum(s[5]) sum(a[2:7]) sum(a[7:])
np.add.reduceat(a,[0,5,2,7])
array([10, 5, 20, 15], dtype=int32)
4.add.outer(a,b)
a的每一個值加上b的所有值,作用於第一個引數的元素以及第二個引數的整體
a陣列是:
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
c = np.array([1,3,5,7])
np.add.outer(c,a) # c的每一個值加上a的所有值
array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 3, 4, 5, 6, 7, 8, 9, 10, 11],
[ 5, 6, 7, 8, 9, 10, 11, 12, 13],
[ 7, 8, 9, 10, 11, 12, 13, 14, 15]])
np.add.outer(a,c) #a的每一個值加上c的所有值
array([[ 1, 3, 5, 7],
[ 2, 4, 6, 8],
[ 3, 5, 7, 9],
[ 4, 6, 8, 10],
[ 5, 7, 9, 11],
[ 6, 8, 10, 12],
[ 7, 9, 11, 13],
[ 8, 10, 12, 14],
[ 9, 11, 13, 15]])
np.add.outer(np.mat('1,2;3,4'),np.mat('4 5;6 7')) #返回的是一個數組
array([[[[ 5, 6],
[ 7, 8]],
[[ 6, 7],
[ 8, 9]]],
[[[ 7, 8],
[ 9, 10]],
[[ 8, 9],
[10, 11]]]])