Numpy or SciPy function for uneven interval?

I was wondering if numpy or scipy has a method in their libraries to find the numerical derivative of a list of values ​​with an uneven interval. The idea is to provide timestamps that match the values, and then use timestamps to find the numerical derivative.

+5
source share
5 answers

You can create your own functions with numpy. For derivatives using forward differences (edit thanks to @EOL, but note that NumPy diff() not a differentiable function ):

 def diff_fwd(x, y): return np.diff(y)/np.diff(x) 

β€œcentral” differences (this is not necessarily central, depending on the distance between the data):

 def diff_central(x, y): x0 = x[:-2] x1 = x[1:-1] x2 = x[2:] y0 = y[:-2] y1 = y[1:-1] y2 = y[2:] f = (x2 - x1)/(x2 - x0) return (1-f)*(y2 - y1)/(x2 - x1) + f*(y1 - y0)/(x1 - x0) 

where y contains function scores and x corresponding "times", so you can use an arbitrary interval.

+4
source

This is probably not provided, because instead you can take the derivative of your dependent variable (y values), take the derivative of your independent variable (in your particular case, timestamps), and then split the first vector to the second.

Derivative definition:

 f' = dy / dx 

or in your case:

 f'(t) = dy / dt 

You can use the diff function to compute each of your numeric dy, the diff function to compute your numeric dt, and then the element divide these arrays to define f '(t) at each point.

+3
source

This is a bit late, but it was found that @dllahr's answer would be very useful and easy to use with numpy :

 >>> import numpy as np >>> y = [1, 2, 3, 4, 5] >>> dy = np.gradient(y) >>> x = [1, 2, 4, 8, 10] >>> dx = np.gradient(x) >>> dy/dx array([1., 0.66666667, 0.33333333, 0.33333333, 0.5]) 
+2
source

Scipy UnivariateSpline.derivative is an easy way to find the derivative of any dataset (the x axis must be ordered so that it grows).

 x = [1, 3, 8, 10] y = [0, 8, 12, 4] z = scipy.interpolate.UnivariateSpline.derivative(x,y) dz = z.derivative(n) 

where n is the order of the derivative

ref: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.UnivariateSpline.derivative.html

0
source

Since the new version of NumPy (1.13), numpy.gradient implemented this function ( documentation ):

 >>> #only numpy 1.13 and above >>> import numpy as np >>> y = [1, 3, 4, 6, 11] >>> x = [0, 1, 5, 10, 11] >>> np.gradient(y, x) array([ 2. , 1.65 , 0.31666667, 4.23333333, 5. ]) 

Here the values ​​in x mark the x-coordinates of the y-values ​​in y .

0
source

Source: https://habr.com/ru/post/1202702/


All Articles