日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python乘法表运算_Python入门教程(三):史上最全的Numpy计算函数总结,建议收藏!...

發(fā)布時(shí)間:2025/3/12 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python乘法表运算_Python入门教程(三):史上最全的Numpy计算函数总结,建议收藏!... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

Numpy提供了靈活的、靜態(tài)類型的、可編譯的程序接口口來優(yōu)化數(shù)組的計(jì)算,也被稱作向量操作,因此在Python數(shù)據(jù)科學(xué)界Numpy顯得尤為重要。Numpy的向量操作是通過通用函數(shù)實(shí)現(xiàn)的。今天小編會(huì)給大家較為全面地介紹下Numpy的通用函數(shù)。

01

數(shù)組的運(yùn)算

Numpy通用函數(shù)涉及到Python原生的算術(shù)運(yùn)算符,標(biāo)準(zhǔn)的加減乘除都可以使用,同時(shí)這些運(yùn)算符也是Numpy內(nèi)置函數(shù)的簡(jiǎn)單封裝器,例如“+”就是add函數(shù)的封裝器。下圖匯總了Numpy實(shí)現(xiàn)的算術(shù)運(yùn)算符。

Numpy的加減乘除運(yùn)算

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ù)運(yùn)算符以及%表示求%運(yùn)算符,請(qǐng)看下面示例:

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]

如果多個(gè)運(yùn)算符組合使用時(shí),需要考慮這些運(yùn)算符的一個(gè)優(yōu)先級(jí)。

-(0.5*x?+?1)?**?2# array([-1. , -2.25, -4. , -6.25])

使用封裝函數(shù)計(jì)算標(biāo)準(zhǔn)的加減乘除。

np.add(x, 2)# array([2, 3, 4, 5])

02

絕對(duì)值

Numpy通用的絕對(duì)值函數(shù)是np.absolute,也可以用其別名來訪問np.abs。這個(gè)通用函數(shù)也可以處理復(fù)數(shù),處理復(fù)數(shù)時(shí),絕對(duì)值返回的是該復(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ù),這里會(huì)講解三角函數(shù)的計(jì)算以及逆三角函數(shù)的計(jì)算。

首先,我們需要先定義一個(gè)角度數(shù)組,然后通過cos(),sin(),tan()等三角函數(shù)進(jìn)行計(jì)算。

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]

同樣我們也可以計(jì)算逆三角函數(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ù)和對(duì)數(shù)

Numpy中的指數(shù)運(yùn)算。

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中的對(duì)數(shù)運(yùn)算。

對(duì)數(shù)運(yùn)算是指數(shù)運(yùn)算的逆運(yùn)算,最基礎(chǔ)的np.log是以自然對(duì)數(shù)為底數(shù)的對(duì)數(shù),同時(shí)也可以使用np.log2,np.log10等計(jì)算以2或10為底的對(duì)數(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. ]

特殊情況下,對(duì)于非常小的輸入值可以保持較好的精度。當(dāng)x很小時(shí),以下函數(shù)給出的值np.log和np.exp的計(jì)算更加精確。

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ù),比特位運(yùn)算,比較運(yùn)算符,弧度轉(zhuǎn)化為角度的運(yùn)算,取整和求余運(yùn)算。除此之外呢,Python中還有更加專用的通用函數(shù)模塊scipy.special,下面會(huì)為大家展示一部分的代碼片段。

?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ù)(高斯積分)

高斯積分的實(shí)現(xiàn)和逆實(shí)現(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ù)來指定計(jì)算結(jié)果的存放位置。

x = np.arange(5)y = np.empty(5)np.multiply(x, 10, out=y)print(y)#?[??0.??10.??20.??30.??40.]這個(gè)特性也可以被稱為數(shù)組視圖,例如將計(jì)算結(jié)果寫入指定數(shù)組的每隔一個(gè)元素的位置。y = np.zeros(10)np.power(2, x, out=y[::2])print(y)# [ 1. 0. 2. 0. 4. 0. 8. 0. 16. 0.]

07

聚合

我們希望用一個(gè)特定的運(yùn)算reduce一個(gè)數(shù)組,那么可以用任何通用函數(shù)的reduce方法。一個(gè)reduce方法會(huì)對(duì)給定元素和操作重復(fù)執(zhí)行,直到得到這個(gè)結(jié)果。

對(duì)add通用函數(shù)調(diào)用reduce方法會(huì)返回?cái)?shù)組中所有元素的和。

x = np.arange(1, 6)np.add.reduce(x)# 15

對(duì)multiply通用函數(shù)調(diào)用reduce方法會(huì)返回?cái)?shù)組中所有元素的乘積。

np.multiply.reduce(x)# 120

accumulate函數(shù)可以儲(chǔ)存每次計(jì)算的中間結(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方法獲得兩個(gè)不同輸入數(shù)組所有元素對(duì)函數(shù)運(yùn)算的結(jié)果。這意味著一行代碼實(shí)現(xiàn)一個(gè)乘法表。

通用函數(shù)還能夠操縱形狀和大小不一樣的數(shù)組,一組這樣的操作被稱為廣播,后面會(huì)細(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]])

全部代碼已上傳,公眾號(hào)后臺(tái)回復(fù)【Numpy計(jì)算函數(shù)】即可獲得。

python入門系列文章持續(xù)更新中,歡迎加入數(shù)據(jù)人專屬交流群

往期推薦

Python入門教程(一):初識(shí)Numpy

Python入門教程(二):Numpy數(shù)組基礎(chǔ)

SQL知識(shí)大全(六):SQL中的開窗函數(shù)

刷爆全網(wǎng)的動(dòng)態(tài)條形圖,原來5行Python代碼就能實(shí)現(xiàn)!

?

分享數(shù)據(jù)知識(shí),成就數(shù)據(jù)理想

點(diǎn)個(gè)在看 你最好看

總結(jié)

以上是生活随笔為你收集整理的python乘法表运算_Python入门教程(三):史上最全的Numpy计算函数总结,建议收藏!...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。