Python數(shù)據(jù)科學(xué):史上最全的Numpy計算函數(shù)總結(jié),建議收藏!

點擊上方 藍(lán)字 關(guān)注我們


Numpy提供了靈活的、靜態(tài)類型的、可編譯的程序接口口來優(yōu)化數(shù)組的計算,也被稱作向量操作,因此在Python數(shù)據(jù)科學(xué)界Numpy顯得尤為重要。Numpy的向量操作是通過通用函數(shù)實現(xiàn)的。今天小編會給大家較為全面地介紹下Numpy的通用函數(shù)。
01
數(shù)組的運算
Numpy通用函數(shù)涉及到Python原生的算術(shù)運算符,標(biāo)準(zhǔn)的加減乘除都可以使用,同時這些運算符也是Numpy內(nèi)置函數(shù)的簡單封裝器,例如“+”就是add函數(shù)的封裝器。下圖匯總了Numpy實現(xiàn)的算術(shù)運算符。

Numpy的加減乘除運算
x = np.arange(4)print("x =", x)print("x + 5 =", x + 5)print("x - 5 =", x - 5)print("x * 2 =", x * 2)print("x / 2 =", x / 2)print("x?//?2?=",?x?//?2)??#?向下整除# x = [0 1 2 3]# x + 5 = [5 6 7 8]# x - 5 = [-5 -4 -3 -2]# x * 2 = [0 2 4 6]# x / 2 = [ 0. 0.5 1. 1.5]#?x?//?2?=?[0?0?1?1]
求負(fù)數(shù)、**表示求指數(shù)運算符以及%表示求%運算符,請看下面示例:
print("-x = ", -x)print("x ** 2 = ", x ** 2)print("x % 2 = ", x % 2)# -x = [ 0 -1 -2 -3]# x ** 2 = [0 1 4 9]# x % 2 = [0 1 0 1]
如果多個運算符組合使用時,需要考慮這些運算符的一個優(yōu)先級。
-(0.5*x?+?1)?**?2# array([-1. , -2.25, -4. , -6.25])
使用封裝函數(shù)計算標(biāo)準(zhǔn)的加減乘除。
np.add(x, 2)
02
絕對值
Numpy通用的絕對值函數(shù)是np.absolute,也可以用其別名來訪問np.abs。這個通用函數(shù)也可以處理復(fù)數(shù),處理復(fù)數(shù)時,絕對值返回的是該復(fù)數(shù)的模。
x = np.array([-2, -1, 0, 1, 2])abs(x)# array([2, 1, 0, 1, 2])np.absolute(x)#??array([2,?1,?0,?1,?2])np.abs(x)# array([2, 1, 0, 1, 2])x = np.array([3 - 4j, 4 - 3j, 2 + 0j, 0 + 1j])np.abs(x)# array([ 5., 5., 2., 1.])
03
三角函數(shù)
三角函數(shù)是數(shù)據(jù)科學(xué)中常用到的函數(shù),這里會講解三角函數(shù)的計算以及逆三角函數(shù)的計算。
首先,我們需要先定義一個角度數(shù)組,然后通過cos(),sin(),tan()等三角函數(shù)進(jìn)行計算。
theta = np.linspace(0, np.pi, 3)print("theta = ", theta)print("sin(theta) = ", np.sin(theta))print("cos(theta) = ", np.cos(theta))print("tan(theta) = ", np.tan(theta))# theta = [ 0. 1.57079633 3.14159265]# sin(theta) = [ 0.00000000e+00 1.00000000e+00 1.22464680e-16]# cos(theta) = [ 1.00000000e+00 6.12323400e-17 -1.00000000e+00]# tan(theta) = [ 0.00000000e+00 1.63312394e+16 -1.22464680e-16]
同樣我們也可以計算逆三角函數(shù)。
x = [-1, 0, 1]print("x = ", x)print("arcsin(x) = ", np.arcsin(x))print("arccos(x) = ", np.arccos(x))print("arctan(x) = ", np.arctan(x))# x = [-1, 0, 1]# arcsin(x) = [-1.57079633 0. 1.57079633]# arccos(x) = [ 3.14159265 1.57079633 0. ]# arctan(x) = [-0.78539816 0. 0.78539816]
04
指數(shù)和對數(shù)
Numpy中的指數(shù)運算。
x = [1, 2, 3]print("x =", x)print("e^x =", np.exp(x))print("2^x =", np.exp2(x))print("3^x =", np.power(3, x))# x = [1, 2, 3]# e^x = [ 2.71828183 7.3890561 20.08553692]# 2^x = [ 2. 4. 8.]# 3^x = [ 3 9 27
Numpy中的對數(shù)運算。
對數(shù)運算是指數(shù)運算的逆運算,最基礎(chǔ)的np.log是以自然對數(shù)為底數(shù)的對數(shù),同時也可以使用np.log2,np.log10等計算以2或10為底的對數(shù)。
x = [1, 2, 4, 10]print("x =", x)print("ln(x) =", np.log(x))print("log2(x) =", np.log2(x))print("log10(x) =", np.log10(x))# x = [1, 2, 4, 10]# ln(x) = [ 0. 0.69314718 1.38629436 2.30258509]# log2(x) = [ 0. 1. 2. 3.32192809]# log10(x) = [ 0. 0.30103 0.60205999 1. ]
特殊情況下,對于非常小的輸入值可以保持較好的精度。當(dāng)x很小時,以下函數(shù)給出的值np.log和np.exp的計算更加精確。
x = [0, 0.001, 0.01, 0.1]print("exp(x) - 1 =", np.expm1(x))print("log(1 + x) =", np.log1p(x))# exp(x) - 1 = [ 0. 0.0010005 0.01005017 0.10517092]# log(1 + x) = [ 0. 0.0009995 0.00995033 0.09531018]
05
專用的通用函數(shù)
Numpy還提供了很多通用函數(shù),包括了雙曲三角函數(shù),比特位運算,比較運算符,弧度轉(zhuǎn)化為角度的運算,取整和求余運算。除此之外呢,Python中還有更加專用的通用函數(shù)模塊scipy.special,下面會為大家展示一部分的代碼片段。
?Gamma函數(shù)(廣義階乘,generlized?factorials)和相關(guān)函數(shù)
from?scipy?import?specialx = [1, 5, 10]print("gamma(x) =", special.gamma(x))print("ln|gamma(x)| =", special.gammaln(x))print("beta(x, 2) =", special.beta(x, 2))# gamma(x) = [ 1.00000000e+00 2.40000000e+01 3.62880000e+05]# ln|gamma(x)| = [ 0. 3.17805383 12.80182748]# beta(x, 2) = [ 0.5 0.03333333 0.00909091]
誤差函數(shù)(高斯積分)
高斯積分的實現(xiàn)和逆實現(xiàn)
# Error function (integral of Gaussian)# its complement, and its inversex = np.array([0, 0.3, 0.7, 1.0])print("erf(x) =", special.erf(x))print("erfc(x) =", special.erfc(x))print("erfinv(x) =", special.erfinv(x))# erf(x) = [ 0. 0.32862676 0.67780119 0.84270079]# erfc(x) = [ 1. 0.67137324 0.32219881 0.15729921]# erfinv(x) = [ 0. 0.27246271 0.73286908 inf]
06
指定輸出
所有的通用函數(shù)都可以通過out參數(shù)來指定計算結(jié)果的存放位置。
x = np.arange(5)y = np.empty(5)np.multiply(x, 10, out=y)print(y)#?[??0.??10.??20.??30.??40.]
這個特性也可以被稱為數(shù)組視圖,例如將計算結(jié)果寫入指定數(shù)組的每隔一個元素的位置。
y = np.zeros(10)np.power(2, x, out=y[::2])print(y)# [ 1. 0. 2. 0. 4. 0. 8. 0. 16. 0.]
07
聚合
我們希望用一個特定的運算reduce一個數(shù)組,那么可以用任何通用函數(shù)的reduce方法。一個reduce方法會對給定元素和操作重復(fù)執(zhí)行,直到得到這個結(jié)果。
對add通用函數(shù)調(diào)用reduce方法會返回數(shù)組中所有元素的和。
x = np.arange(1, 6)np.add.reduce(x)# 15
對multiply通用函數(shù)調(diào)用reduce方法會返回數(shù)組中所有元素的乘積。
np.multiply.reduce(x)# 120
accumulate函數(shù)可以儲存每次計算的中間結(jié)果表。
np.add.accumulate(x)#?array([?1,??3,??6,?10,?15]
np.multiply.accumulate(x)array([ 1, 2, 6, 24, 120])# array([ 1, 2, 6, 24, 120])
任何通用函數(shù)都可以用outer方法獲得兩個不同輸入數(shù)組所有元素對函數(shù)運算的結(jié)果。這意味著一行代碼實現(xiàn)一個乘法表。
通用函數(shù)還能夠操縱形狀和大小不一樣的數(shù)組,一組這樣的操作被稱為廣播,后面會細(xì)講。
x = np.arange(1, 6)np.multiply.outer(x, x)# array([[ 1, 2, 3, 4, 5],# [ 2, 4, 6, 8, 10],# [ 3, 6, 9, 12, 15],# [ 4, 8, 12, 16, 20],# [ 5, 10, 15, 20, 25]])

相關(guān)閱讀:
