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 np
In [3]: x = Symbol('x')
In [4]: y = x**2 + 1
In [5]: yprime = y.diff(x)
In [6]: yprime
Out[6]: 2⋅x

In [7]: f = lambdify(x, yprime, 'numpy')
In [8]: f(np.ones(5))
Out[8]: [ 2.  2.  2.  2.  2.]
Answer from MRocklin on Stack Overflow
Top answer
1 of 9
208

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 np
In [3]: x = Symbol('x')
In [4]: y = x**2 + 1
In [5]: yprime = y.diff(x)
In [6]: yprime
Out[6]: 2⋅x

In [7]: f = lambdify(x, yprime, 'numpy')
In [8]: f(np.ones(5))
Out[8]: [ 2.  2.  2.  2.  2.]
2 of 9
82

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 + 1
dydx = 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.

🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-compute-derivative-using-numpy
How to compute derivative using Numpy? - GeeksforGeeks
July 23, 2025 - deriv(): Calculates and gives us the derivative expression · At first, we need to define a polynomial function using the numpy.poly1d() function.
🌐
Medium
medium.com › @whyamit404 › understanding-derivatives-with-numpy-e54d65fcbc52
Understanding Derivatives with NumPy | by whyamit404 | Medium
February 8, 2025 - Let’s get into some code to make this clearer. Suppose we want to find the derivative of y = x². Here’s how you can do it: import numpy as np # Define the range for x from 0 to 10, split into 100 points x = np.linspace(0, 10, 100) # Define the function y = x^2 y = x**2 # Compute the derivative of y with respect to x using np.gradient dy_dx = np.gradient(y, x) # Print the result print(dy_dx)
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.gradient.html
numpy.gradient — NumPy v2.1 Manual
New in version 1.11.0. ... A tuple of ndarrays (or a single ndarray if there is only one dimension) corresponding to the derivatives of f with respect to each dimension.
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.differentiate.derivative.html
derivative — SciPy v1.17.0 Manual
The value at which the derivative of f was evaluated (after broadcasting with args and step_direction). ... The implementation was inspired by jacobi [1], numdifftools [2], and DERIVEST [3], but the implementation follows the theory of Taylor series more straightforwardly (and arguably naively so).
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-compute-derivative-using-numpy
How to Compute Derivative Using Numpy?
2 weeks ago - import numpy as np def f(x): return x**2 # Test the function print(f(3)) ... Specify the x values where you want to compute the derivative.
🌐
Turing
turing.com › kb › derivative-functions-in-python
A Quick Guide to Calculating Derivatives in Python
These examples will showcase the versatility of Python for computing derivatives and highlight the applicability of derivative calculations in different domains. Derivative calculations for different types of functions ... Below are a few code snippets that demonstrate the methods discussed earlier. The functions and input values in these examples can be adapted and modified to specific requirements. 1. Numerical differentiation using central difference · import numpy as np def f(x): return np.sin(x) def central_difference(f, x, h): return (f(x + h) - f(x - h)) / (2 * h) x = 0.5 h = 0.01 f_prime = central_difference(f, x, h) print(f_prime)
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.gradient.html
numpy.gradient — NumPy v2.4 Manual
Gradient is calculated only along the given axis or axes The default (axis = None) is to calculate the gradient for all the axes of the input array. axis may be negative, in which case it counts from the last to the first axis. ... A tuple of ndarrays (or a single ndarray if there is only one dimension) corresponding to the derivatives of f with respect to each dimension.
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.polyder.html
numpy.polyder — NumPy v2.2 Manual
Polynomial to differentiate. A sequence is interpreted as polynomial coefficients, see poly1d. ... A new polynomial representing the derivative.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AutoDiff.html
Automatic Differentiation — Python Like You Mean It
MyGrad takes this one step further, and provides true drop-in automatic differentiation to NumPy. Install MyGrad into your Python environment. Open your terminal, activate your desired Python environment, and run the following command. ... Let’s jump right in with a simple example of using MyGrad to evaluate the derivative of a function at a specific point.
🌐
YouTube
youtube.com › watch
Python Tutorial: Numerical Differentiation with NumPy
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Svitla Systems
svitla.com › home › articles › numerical differentiation methods in python
Python for Numerical Differentiation: Methods & Tools
January 14, 2021 - Then, let’s set the function value in the form of pairs x, y with a step of 0.01 for the range of x from 0 to 4. We’re going to use the scipy derivative to calculate the first derivative of the function. Please don’t write your own code to calculate the derivative of a function until you know why you need it. Scipy provides fast implementations of numerical methods and it is pre-compiled and tested across many use cases. import numpy import matplotlib.pyplot as plt def f(x): return x*x x = numpy.arange(0,4,0.01) y = f(x) plt.figure(figsize=(10,5)) plt.plot(x, y, 'b') plt.grid(axis = 'both') plt.show() Code language: JavaScript (javascript)
Price   $$$
Address   100 Meadowcreek Drive, Suite 102, 94925, Corte Madera
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.diff.html
numpy.diff — NumPy v2.1 Manual
Calculate the n-th discrete difference along the given axis · The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively
🌐
Derivative
docs.derivative.ca › NumPy
NumPy - Derivative
March 23, 2022 - NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.[1]
🌐
GeeksforGeeks
geeksforgeeks.org › differentiate-a-polynomial-and-set-the-derivatives-in-python-numpy
Differentiate a polynomial and set the derivatives in Python-NumPy | GeeksforGeeks
June 3, 2022 - In this article, we will cover how to differentiate a Hermite_e series and set derivatives in Python using NumPy.
🌐
sqlpey
sqlpey.com › python › compute-derivatives-using-numpy-python
Top 8 Methods to Compute Derivatives Using Numpy - sqlpey
December 5, 2024 - Learn 8 ways to compute derivatives of functions with numpy, from basic gradients to advanced numerical methods.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.polyder.html
numpy.polyder — NumPy v2.3 Manual
Polynomial to differentiate. A sequence is interpreted as polynomial coefficients, see poly1d. ... A new polynomial representing the derivative.
🌐
GitHub
github.com › HIPS › autograd
GitHub - HIPS/autograd: Efficiently computes derivatives of NumPy code. · GitHub
Autograd can automatically differentiate native Python and Numpy code. It can handle a large subset of Python's features, including loops, ifs, recursion and closures, and it can even take derivatives of derivatives of derivatives.
Starred by 7.5K users
Forked by 940 users
Languages   Python 99.7% | Shell 0.3%