Ufuncs

以下資料轉載自:[Day08]Learning Numpy - Ufuncs、broadcasting、運算子 - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天

Ufuncs

Numpy陣列中的計算,許多Ufuncs都是在C程式實作,計算速度比較快,接下來介紹一些比較常用的Ufuncs

絕對值:abs()

x = np.array([-2,-1,0,1,-3])
abs(x)
# 輸出結果
array([2, 1, 0, 1, 3])

三角函數:abs()、cos()、tan()

theta = np.linspace(0, np.pi, 3)  #建立一個角度的陣列
print("sin()---->", np.sin(theta))
print("cos()---->", np.cos(theta))
print("tan()---->", np.tan(theta))
# 輸出結果
sin()----> [0.0000000e+00 1.0000000e+00 1.2246468e-16]
cos()----> [ 1.000000e+00  6.123234e-17 -1.000000e+00]
tan()----> [ 0.00000000e+00  1.63312394e+16 -1.22464680e-16]

指數和對數:exp()、exp2()、power()、log()、log2()、log10()

x = [1,2,9,10, 16]
print("exp()---->", np.exp(x))          # e^x
print("exp2()---->", np.exp2(x))        # 2^x
print("power()---->", np.power(3, x))   # 3^x
print("log()---->", np.log(x))          # log自然對數
print("log2()---->", np.log2(x))        # 以2為基底的對數
print("log10()---->", np.log10(x))      # 以10為基底的對數
# 輸出結果
exp()----> [2.71828183e+00 7.38905610e+00 8.10308393e+03 2.20264658e+04
 8.88611052e+06]
exp2()----> [2.0000e+00 4.0000e+00 5.1200e+02 1.0240e+03 6.5536e+04]
power()----> [       3        9    19683    59049 43046721]
log()----> [0.         0.69314718 2.19722458 2.30258509 2.77258872]
log2()----> [0.         1.         3.169925   3.32192809 4.        ]
log10()----> [0.         0.30103    0.95424251 1.         1.20411998]

外積:outer()

y = np.arange(1,6)
np.multiply.outer(y, y)
# 輸出結果
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]])

sum():陣列做加總

如果想要計算陣列中所有元素的加總,可以使用sum()

import numpy as np
L = np.random.random(100)  #在0到1間亂數值取100個
np.sum(L)
# 輸出結果
48.87105412779822

sum()除了可以使用一維陣列,現在用多維陣列

m = np.random.random((3,4))
print(m)
print('column 加總', m.sum(axis=0)) #column加總
print('row 加總', m.sum(axis=1)) #row加總
# 輸出結果
[[0.90141178 0.44072501 0.5813923  0.06718993]
 [0.74907766 0.65631017 0.47611455 0.33805445]
 [0.79914804 0.9814414  0.23869758 0.10919935]]
column 加總 [2.44963747 2.07847657 1.29620443 0.51444374]
row 加總 [1.99071901 2.21955683 2.12848637]

min(),max():陣列中的最大值和最小值

取得陣列中元素的最大值和最小值

ironman_int = np.random.randint(0,10000,200) #在整數0~10000之間取值200個
print('min---->', np.min(ironman_int))
print('max---->', np.max(ironman_int))
min----> 22
max----> 9946

min()、max()除了可以使用一維陣列,現在用多維陣列

ironman_int_multi = np.random.randint(0,10000,(3,4)) #建立3*4的陣列,值介在整數0~10000之間
print('ironman_int_multi---->', ironman_int_multi)

print('ironman_int_multi.min(axis=0)----->', ironman_int_multi.min(axis=0))
print('ironman_int_multi.max(axis=1)----->', ironman_int_multi.max(axis=1))
# 輸出結果
ironman_int_multi----> [[9931 4228 5996 3076]
 [2969 9102  374 1900]
 [5982 1051 9939 4320]]
ironman_int_multi.min(axis=0)-----> [2969 1051  374 1900]
ironman_int_multi.max(axis=1)-----> [9931 9102 9939]
  • 比較np.min和min
    之前在第二天的時候有學到也可以使用Python內建的min()來取得陣列的最小值,在這邊比較兩者運算所耗費的時間。
big_array = np.random.random(10000000) #在0到1間亂數值取10000000個
%timeit min(big_array)
%timeit np.min(big_array)
# 輸出結果
612 ms ± 23.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
7.38 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)

Numpy其他陣列計算函式

名稱 說明
np.sum 所有元素的加總
np.prod 所有元素的乘積
np.mean 所有元素的平均值
np.std 標準差
np.var 變異量
np.min 找出元素的最小值
np.max 找出元素的最大值
np.argmin 找出最小值的索引
np.argmax 找出最大值的索引
np.median 元素的中位數
np.any 當陣列中有任一值是True或是非零值時傳回True
np.all 當陣列中有所有值是True或是非零值時傳回True