Using finite Difference
import numpy as np
def deriv(x,y):
return np.diff(y)/np.diff(x)
the above function works if the inputs are both numpy arrays of same size. The returned array has 1 less elements than the input arrays.
If the input arrays have some small noise than taking the naive finite difference derivative will result in answers which are almost useless. One can use Savgol filter to smoothen out that noise while taking derivatives.
from scipy.signal import savgol_filter
def deriv_savgol(x,y,window=25,polyorder=1):
dy = savgol_filter(y,window,polyorder,deriv=1)
dx = savgol_filter(x,window,polyorder,deriv=1)
return dy/dx
Note that this method returns array of same size as the input.