`numpy`提供了簡單靈活的介面,用於優化數據數組的計算。 通用計算最大的優勢在於通過向量化操作,將迴圈推送至`numpy`之下的編譯層,從而取得更快的執行效率。 `numpy`的通用計算讓我們計算數組時就像計算單獨一個變數一樣, 不用寫迴圈去遍曆數組中的各個元素。 比如,對於一般的`python ...
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
數組的通用計算方法,通用計算把數組元素迴圈的複雜度封裝起來,讓我們用直觀的方式計算數組,更容易實現各種數學公式和定理。
本篇介紹的算術計算,三角函數,以及指數和對數等常用的方法,但不是全部的通用計算方法,更加複雜的微分和積分計算請參考官方的文檔。