Numpy 的 gradient(~)
方法計算給定數據點的梯度,其中梯度定義為 y 的變化相對於 x 的變化。
參數
1. f
| array-like
函數輸出(y-values)。
2. varargs
| scalar
的array
| optional
f 中點對之間的間距。默認情況下,varargs=1
。
3. edge_order
| int
| optional
用於邊計算的精度順序。允許的值為 1 和 2。默認情況下為 edge_order=2
。
4. axis
| None
或int
或tuple
或int
| optional
計算梯度的軸。
返回值
保存數據點梯度的 Numpy 數組。
例子
基本用法
要使用默認值 1 的 step-size 計算梯度:
y = [1,2,4,9,16]
np.gradient(y)
array([1. , 1.5, 3.5, 6. , 7. ])
在幕後,數字的計算方式如下:
(y[1] - y[0]) / 1 = 1.0
(y[2] - y[0]) / 2 = 1.5
(y[3] - y[1]) / 2 = 3.5
(y[4] - y[2]) / 2 = 6.0
(y[4] - y[3]) / 1 = 7.0
請注意,在邊界處,僅計算第一個差值。
指定 data-points 之間的間距
我們可以指定自己的 x-values,而不是默認的 step-size 1:
x = [5,10,20,30]
y = [1,2,4,9]
np.gradient(y, x)
array([0.2 , 0.2 , 0.35, 0.5 ])
在幕後,數字的計算方式如下:
(y[1] - y[0]) / (x[1] - x[0]) = 0.2
(y[2] - y[0]) / (x[2] - x[0]) = 0.2
(y[3] - y[1]) / (x[3] - x[1]) = 0.35
(y[3] - y[2]) / (x[3] - x[2]) = 0.5
相關用法
- Python graphlib.TopologicalSorter用法及代碼示例
- Python graphlib.TopologicalSorter.static_order用法及代碼示例
- Python graphlib.TopologicalSorter.is_active用法及代碼示例
- Python Tableau groups.update用法及代碼示例
- Python Tableau groups.delete用法及代碼示例
- Python Tableau groups.create用法及代碼示例
- Python numpy string greater_equal()用法及代碼示例
- Python Tableau groups.remove_user用法及代碼示例
- Python Tableau groups.populate_users用法及代碼示例
- Python Tableau groups.get用法及代碼示例
- Python Tableau groups.create_AD_group用法及代碼示例
- Python NumPy greater方法用法及代碼示例
- Python Tkinter grid()用法及代碼示例
- Python NumPy greater_equal方法用法及代碼示例
- Python Tableau groups.add_user用法及代碼示例
- Python Django get_language_info用法及代碼示例
- Python Pandas get_dummies方法用法及代碼示例
- Python PIL getbands() and getextrema()用法及代碼示例
- Python PIL getpixel()用法及代碼示例
- Python NumPy genfromtxt方法用法及代碼示例
- Python getattr()用法及代碼示例
- Python OpenCV getTrackbarPos()用法及代碼示例
- Python Django get用法及代碼示例
- Python NumPy get_printoptions方法用法及代碼示例
- Python OpenCV getgaussiankernel()用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | gradient method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。