- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Return the gradient of an N-dimensional array over axis 1 in Python

The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array. The 1st parameter, f is an Ndimensional array containing samples of a scalar function. The 2nd parameter is the varargs i.e. the spacing between f values. Default unitary spacing for all dimensions.

The 3rd parameter is the edge_order{1, 2} i.e. the Gradient is calculated using N-th order accurate differences at the boundaries. Default − 1. The 4th parameter is the Gradient, which 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. The method returns a list of ndarrays corresponding to the derivatives of f with respect to each dimension. Each derivative has the same shape as f.

## Steps

At first, import the required library −

import numpy as np

Creating a numpy array using the array() method. We have added elements of float type −

arr = np.array([[20, 35, 57], [70, 85, 120]], dtype = float)

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array −

print("\nResult (gradient)...\n",np.gradient(arr, axis = 1))

## Example

import numpy as np # Creating a numpy array using the array() method # We have added elements of float type arr = np.array([[20, 35, 57], [70, 85, 120]], dtype = float) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries. The returned gradient hence has the same shape as the input array. print("\nResult (gradient)...\n",np.gradient(arr, axis = 1))

## Output

Our Array... [[ 20. 35. 57.] [ 70. 85. 120.]] Dimensions of our Array... 2 Datatype of our Array object... float64 Result (gradient)... [[15. 18.5 22. ] [15. 25. 35. ]]

- Related Questions & Answers
- Return the gradient of an N-dimensional array over axis 0 in Python
- Return the gradient of an N-dimensional array over given axis in Python
- Return the gradient of an N-dimensional array in Python
- Return the gradient of an N-dimensional array and specify edge order in Python
- Expand the shape of an array over axis 1 in Numpy
- Calculate the n-th discrete difference over axis 1 in Python
- Return the cumulative product of array elements over axis 1 treating NaNs as one in Python
- Return the Norm of the vector over axis 1 in Linear Algebra in Python
- Return the cumulative sum of array elements over given axis 1 treating NaNs as zero in Python
- Compute the inverse of an N-dimensional array in Python
- Return the maximum of an array along axis 1 or maximum ignoring any NaNs in Python
- Return the minimum of an array along axis 1 or minimum ignoring any NaNs in Python
- Compute the maximum of the masked array elements over axis 1 in Numpy
- Compute the minimum of the masked array elements over axis 1 in Numpy
- Return the average of the masked array elements over axis 0 in Numpy