numpy提供了簡單靈活的介面,用于優化資料陣列的計算,
通用計算最大的優勢在于通過向量化操作,將回圈推送至numpy之下的編譯層,從而取得更快的執行效率,
numpy的通用計算讓我們計算陣列時就像計算單獨一個變數一樣,
不用寫回圈去遍歷陣列中的各個元素,
比如,對于一般的python二維陣列,我們要給陣列中每個值加1:
l = [[1, 2], [3, 4]]
print(l)
#運行結果
[[1, 2], [3, 4]]
for i in range(len(l)):
for j in range(len(l[i])):
l[i][j] += 1
print(l)
#運行結果
[[2, 3], [4, 5]]
如果用numpy的通用計算的話:
import numpy as np
l = np.array([[1,2], [3,4]])
print(l)
#運行結果
[[1, 2], [3, 4]]
l = l + 1
print(l)
#運行結果
[[2, 3], [4, 5]]
1. 算術計算
算術計算是最基本的,numpy陣列支持直接用運算子或者通用函式來進行運算,
| 運算子 | 通用函式 | 說明 |
|---|---|---|
| + | np.add | 加法運算 |
| - | np.subtract | 減法運算 |
| * | np.multiply | 乘法運算 |
| / | np.divide | 除法運算 |
| // | np.floor_divide | 向下整除運算 |
| ** | np.power | 指數運算 |
| % | np.mod | 模運算 |
算術運算比較簡單,就不一一演示各個運算子了,
需要注意的一點是,當numpy陣列和單一數字運算時,陣列中每個元素都單獨和此數字運算,
arr = np.array([[1,2], [3, 4]])
print(arr)
#運行結果
[[1 2]
[3 4]]
print(arr * 2)
#運行結果
[[2 4]
[6 8]]
arr * 2 相當于arr中每個元素都 * 2,
當numpy陣列和另一個numpy陣列運算時,是兩個陣列對應位置的元素進行運算,
這就要求兩個陣列的 shape 要一樣,否則會出錯,
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 0], [0, 1]])
print(arr1, arr2)
#運行結果
[[1 2]
[3 4]]
[[1 0]
[0 1]]
print(arr1 * arr2)
#運行結果
[[1 0]
[0 4]]
對應元素相乘,所以只保留了對角線上的元素,
2. 三角函式
除了常用的算術運算,numpy的陣列支持各類三角函式運算,
下面演示幾個常用的三角函式:
arr = np.array([0, np.pi/6, np.pi/4, np.pi/2])
print("sin(arr) = ", np.sin(arr))
print("cos(arr) = ", np.cos(arr))
print("tan(arr) = ", np.tan(arr))
#運行結果
sin(arr) = [0. 0.5 0.70710678 1. ]
cos(arr) = [1.00000000e+00 8.66025404e-01 7.07106781e-01 6.12323400e-17]
tan(arr) = [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.63312394e+16]
arr = np.array([-1, 0, 1])
print("arcsin(arr) = ", np.arcsin(arr))
print("arccos(arr) = ", np.arccos(arr))
print("arctan(arr) = ", np.arctan(arr))
#運行結果
arcsin(arr) = [-1.57079633 0. 1.57079633]
arccos(arr) = [3.14159265 1.57079633 0. ]
arctan(arr) = [-0.78539816 0. 0.78539816]
3. 指數和對數
常用的指數和對數如下:
x = np.array([1, 2, 4, 10])
print("e^x = ", np.exp(x))
print("2^x = ", np.exp2(x))
print("3^x = ", np.power(3, x))
#運行結果
e^x = [2.71828183e+00 7.38905610e+00 5.45981500e+01 2.20264658e+04]
2^x = [ 2. 4. 16. 1024.]
3^x = [ 3 9 81 59049]
print("ln(x) = ", np.log(x))
print("log2(x) = ", np.log2(x))
print("log10(x) = ", np.log10(x))
#運行結果
ln(x) = [0. 0.69314718 1.38629436 2.30258509]
log2(x) = [0. 1. 2. 3.32192809]
log10(x) = [0. 0.30103 0.60205999 1. ]
4. 通用特性
除了通用的計算方法,還有一些特性也很有用,
下面介紹兩個常用的特性,一個可以節約記憶體,提高程式的運行效率;另一個可以簡化編碼,提高程式的撰寫效率,
4.1. 指定輸出位置
進行兩個陣列的計算時,比如x陣列和y陣列,計算的結果常常要用新的陣列(比如z陣列)來保存,
如果計算之后x陣列或y陣列不再需要的話,我們可以把運算結果保存在x陣列或y陣列中,這樣就不用申請信的記憶體,
x = np.random.randint(1, 10, (3,3))
y = np.random.randint(1, 10, (3,3))
print(x)
print(y)
#運行結果
[[3 9 3]
[8 6 9]
[9 7 4]]
[[4 4 5]
[1 6 6]
[2 5 6]]
np.multiply(x, y, out=y)
print(x)
print(y)
#運行結果
[[3 9 3]
[8 6 9]
[9 7 4]]
[[12 36 15]
[ 8 36 54]
[18 35 24]]
設定引數 out=y,可以看到計算結果保存在了y陣列中,
4.2. 簡單的聚合
對于任意一個陣列,按行或者列聚合合計值時:
x = np.random.randint(1, 10, (3,3))
print(x)
#運行結果
[[8 6 5]
[4 8 4]
[9 2 3]]
#每列的合計值
print(np.add.reduce(x))
#運行結果
[21 16 12]
#每行的合計值
print(np.add.reduce(x, axis=1))
#運行結果
[19 16 14]
上面是用np.add來聚合的,也可以使用 np.multiply,np.divide等等前面介紹的各種算術計算,
除了聚合合計值,numpy還提供了一個可以計算合計程序中每步計算結果的方法accumulate,
x = np.random.randint(1, 10, 5)
print(x)
#運算結果
[6 1 6 9 7]
print(np.add.accumulate(x))
#運算結果:[x[0], x[0]+x[1], x[0]+x[1]+x[2]...]
[ 6 7 13 22 29]
print(np.multiply.accumulate(x))
#運算結果:[x[0], x[0]*x[1], x[0]*x[1]*x[2]...]
[6 6 36 324 2268]
5. 總結回顧
本篇主要介紹了 numpy陣列的通用計算方法,通用計算把陣列元素回圈的復雜度封裝起來,讓我們用直觀的方式計算陣列,更容易實作各種數學公式和定理,
本篇介紹的算術計算,三角函式,以及指數和對數等常用的方法,但不是全部的通用計算方法,更加復雜的微分和積分計算請參考官方的檔案,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/555744.html
標籤:其他
下一篇:返回列表
