Python

NumPy Gradient Method

NumPy” is a useful library in Python for scientific computing, particularly in numerical analysis. It provides an array object that can store homogeneous data and various operations to manipulate these arrays. One such operation is the “gradient()” method, which is utilized to determine/find the gradient of a function at a given point. This article will explain the NumPy “gradient()” method with relevant examples.

What is the numpy “gradient()” Method?

The “np.gradient()” method returns the N-dimensional array’s gradient. The gradient is determined by first or second-order differences at the boundaries and central differences at the interior points. The gradient retrieved matches the input array in shape. To calculate the gradient, the method takes multiple arguments to specify the spacing between values, the axis or axes, and the edge order.

Syntax

np.gradient(f, *varargs, axis= None, edge_order= 1)

In the above syntax:

  • f” refers to samples of a scalar function in an N-dimensional array.
  • “*varargs” correspond to the distance between values of “f”.
  • axis” is the axis or axes along which the gradient must be determined.
  • edge_order” indicates the order of the differences at the boundaries.

How to Calculate the Gradient of Function?

The direction of maximum increase for a scalar function is indicated by a vector known as the gradient vector. It is calculated using the partial derivative of each variable of the function. Therefore, for a function “f(x,y,z)”, the gradient vector is given by:

∇f(x,y,z) = [ ∂f/∂x, ∂f/∂y, ∂f/∂z ]

The gradient vector is also known as the “del” or “nabla” operator.

Let’s perform a few examples that are used to calculate the gradient in Python:

Example 1: Calculate the Gradient of the Function in Python Using “numpy.gradient()” Method

The “numpy.gradient()” method takes an array of values representing the function and returns an array of the same shape representing the gradient at each point. Here is an example code:

import numpy

def f(x):
  return x**2

x = numpy.array([21, 32, 43, 54, 65])
grad = numpy.gradient(f(x))

print(grad)

In this example:

  • The “numpy” library is imported and a user-defined function is defined, respectively.
  • Now, the “np.gradient()” method is utilized to determine the function’s gradient at each point, as discussed.

Output

The above output is an array representing the gradient at each point.

Example 2: Determine the Gradient of an N-Dimensional Array in Python Using “numpy.gradient()” Method

The following code is used to determine the gradient for an “N” Dimensional array:

import numpy

f = numpy.array([[27, 34, 18, 33], [32, 46, 55, 29]])

result = numpy.gradient(f)

print(result)

According to the above code:

  • The “numpy” library is imported and a two-dimensional array named “f” is created.
  • The function “numpy.gradient()” finds the gradient of “f”, which is a vector that shows the direction where “f” changes the most rapidly.
  • It is such that to calculate the gradient, we use central differences for the inner points and first differences for the edge points.

Output

As analyzed, the gradient of the given “N-Dimensional” array has been calculated appropriately.

Conclusion

The “numpy.gradient()” method is utilized in Python to calculate the gradient of the function or Numpy “N-Dimensional” array. The “gradient()” method can be utilized for a wide range of applications, including numerical differentiation, optimization, and image processing. This Python post delivered a thorough guide on the Numpy gradient method using appropriate examples.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.