How do I compute derivative using Numpy? How do I compute derivative using Numpy? numpy numpy

How do I compute derivative using Numpy?


You have four options

  1. Finite Differences
  2. Automatic Derivatives
  3. Symbolic Differentiation
  4. Compute derivatives by hand.

Finite differences require no external tools but are prone to numerical error and, if you're in a multivariate situation, can take a while.

Symbolic differentiation is ideal if your problem is simple enough. Symbolic methods are getting quite robust these days. SymPy is an excellent project for this that integrates well with NumPy. Look at the autowrap or lambdify functions or check out Jensen's blogpost about a similar question.

Automatic derivatives are very cool, aren't prone to numeric errors, but do require some additional libraries (google for this, there are a few good options). This is the most robust but also the most sophisticated/difficult to set up choice. If you're fine restricting yourself to numpy syntax then Theano might be a good choice.

Here is an example using SymPy

In [1]: from sympy import *In [2]: import numpy as npIn [3]: x = Symbol('x')In [4]: y = x**2 + 1In [5]: yprime = y.diff(x)In [6]: yprimeOut[6]: 2⋅xIn [7]: f = lambdify(x, yprime, 'numpy')In [8]: f(np.ones(5))Out[8]: [ 2.  2.  2.  2.  2.]


The most straight-forward way I can think of is using numpy's gradient function:

x = numpy.linspace(0,10,1000)dx = x[1]-x[0]y = x**2 + 1dydx = numpy.gradient(y, dx)

This way, dydx will be computed using central differences and will have the same length as y, unlike numpy.diff, which uses forward differences and will return (n-1) size vector.


NumPy does not provide general functionality to compute derivatives. It can handles the simple special case of polynomials however:

>>> p = numpy.poly1d([1, 0, 1])>>> print p   21 x + 1>>> q = p.deriv()>>> print q2 x>>> q(5)10

If you want to compute the derivative numerically, you can get away with using central difference quotients for the vast majority of applications. For the derivative in a single point, the formula would be something like

x = 5.0eps = numpy.sqrt(numpy.finfo(float).eps) * (1.0 + x)print (p(x + eps) - p(x - eps)) / (2.0 * eps * x)

if you have an array x of abscissae with a corresponding array y of function values, you can comput approximations of derivatives with

numpy.diff(y) / numpy.diff(x)