Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

For an elementwise callable, use scipy.differentiate.derivative(f, x). It estimates the first derivative at each point in an array of input values. It does not, based on this API’s documented purpose, differentiate an array of already sampled measurements.

When to use scipy.differentiate.derivative

SciPy’s derivative function numerically estimates the first derivative of a real-valued function at corresponding elements of x. The function you pass must act elementwise: each output value must correspond to the function evaluated at the matching input value. The returned derivative has the shape of the abscissae after broadcasting.

This distinction matters: pass a callable that can evaluate your function at nearby points, not just a list of measurements whose derivative you want to infer. SciPy describes the routine as approximating “the first derivative of f at the corresponding element of x using finite difference differentiation.” See the SciPy derivative API reference.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Calculate derivatives at array points

For example, to estimate the derivative of f(x) = x² at several points, pass the function and an array of points:

import numpy as np
from scipy.differentiate import derivative

def f(x):
    return x**2

x = np.array([1.0, 2.0, 3.0])
result = derivative(f, x)

print(result.df)

The analytic derivative is 2x, so the expected values are approximately [2, 4, 6]. The function evaluations are numerical estimates, not symbolic differentiation. The result object also contains diagnostics; inspect them rather than treating the displayed estimate as automatically accurate.

How the estimate and defaults work

The routine uses a finite-difference formula. It starts with an initial absolute step size, then changes that step on successive iterations to assess whether the estimate is stabilizing. The documented stopping condition is res.error < atol + rtol * abs(res.df). The error field is the difference between consecutive estimates; it is useful evidence of convergence, but it is not a universal guarantee of the true derivative error.

In the SciPy 1.18.0 API reference, the documented defaults are:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Setting Default Meaning
order 8 Order of the finite-difference formula. An odd requested order is rounded up to the next even order.
initial_step 0.5 Initial absolute step size.
step_factor 2.0 Factor used to change the step between iterations.
maxiter 10 Maximum number of iterations.

These are starting values, not settings that guarantee a desired precision for every function, input scale, dtype, or accuracy target. The default absolute tolerance is the smallest normal number for the relevant dtype, and the default relative tolerance is the square root of that dtype’s precision. Choose tolerances and steps for the problem you are solving.

Choose settings for scale, zero derivatives, and boundaries

Large-magnitude input values

initial_step is an absolute step, not a fraction of each input value. If x has a very large magnitude, adding the default step may not change it in floating-point arithmetic. SciPy’s example notes that a step of 0.5 cannot be resolved at x = 1e20. For such inputs, try a larger initial step that produces a representable change, then verify the result and convergence diagnostics.

Derivatives that may be zero

When the true derivative is exactly zero, the default tolerances can be difficult to satisfy. Set an application-appropriate absolute tolerance with atol; do not assume that failure to meet the default test means the estimate is necessarily unusable or that changing the tolerance proves accuracy.

Points at a domain boundary

Central differences sample on both sides of a point. If the function is undefined on one side, use step_direction to constrain the direction of the steps: zero requests central differences, a negative value requests non-positive directions, and a positive value requests non-negative directions. The direction may be an array if it broadcasts with the other inputs.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make the callable and arrays compatible

The callable has the form f(xi, *argsi) -> ndarray. It must evaluate elementwise and must not mutate the input array or argument arrays. The x values, step_direction, and args must be broadcastable.

With preserve_shape=False, the callable may receive broadcastable shapes. If you set preserve_shape=True, it must handle the documented input shape, or that shape extended with the abscissa-count dimension. This is particularly relevant for functions that assume a fixed array shape or perform operations that do not broadcast.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Read the result diagnostics

The result includes array-valued fields where applicable: df (the estimated derivative), error (the change-based estimate), success, status, nit (iterations), nfev (function evaluations), and x. Status 0 means the requested tolerances were met. Negative statuses report conditions such as increasing error, reaching the iteration limit, a non-finite value, or callback termination.

For a scientific or production result, record the input scale and settings alongside the output. Where an analytic derivative or another trusted reference is available, compare against it; convergence of successive finite-difference estimates alone does not establish correctness.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the related routine by derivative structure

SciPy’s differentiate module also provides jacobian and hessian. Use the routine matching the mathematical structure you need: derivative is for first derivatives of an elementwise real scalar function, while Jacobian and Hessian routines address vector- and matrix-structured derivatives. See the SciPy differentiate module overview, Jacobian reference, and Hessian reference.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.