
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Difference Universal Function (ufunc)
Difference Universal Function (ufunc)
A difference universal function (ufunc) in NumPy is a function used to calculate the difference between elements in an array.
This operation can be applied element-wise between two arrays, or to compute the discrete difference along a specific axis of a single array. The primary function for computing differences in NumPy is numpy.diff().
The NumPy diff() Function
The numpy.diff() function computes the difference between consecutive elements in an array, effectively calculating the first order discrete difference. It can also compute higher-order differences by specifying the n parameter.
Example
In the following example, we use the numpy.diff() function to calculate the difference between consecutive elements of a 1D array −
import numpy as np # Define an array a = np.array([1, 3, 6, 10, 15]) # Compute the first-order differences diff = np.diff(a) print("First-order differences:", diff)
Following is the output obtained −
First-order differences: [2 3 4 5]
NumPy Higher-Order Differences
The numpy.diff() function can also compute higher-order differences. By specifying the n parameter, we can calculate the difference between elements multiple times.
Example
In the following example, we calculate the second-order difference of an array using numpy.diff() function with n=2 −
import numpy as np # Define an array a = np.array([1, 3, 6, 10, 15]) # Compute the second-order differences second_diff = np.diff(a, n=2) print("Second-order differences:", second_diff)
This will produce the following result −
Second-order differences: [1 1 1]
NumPy diff Function for 2D Arrays
The numpy.diff() function can also be used with 2D arrays. By specifying the axis parameter, we can compute differences along a specific axis, such as row-wise or column-wise.
Example
In the following example, we use numpy.diff() function on a 2D array to compute the differences along the rows and columns −
import numpy as np # Define a 2D array a = np.array([[1, 2, 3], [4, 5, 6]]) # Compute the differences along the rows (axis=1) row_diff = np.diff(a, axis=1) # Compute the differences along the columns (axis=0) col_diff = np.diff(a, axis=0) print("Row-wise differences:", row_diff) print("Column-wise differences:", col_diff)
Following is the output obtained −
Row-wise differences: [[1 1] [1 1]] Column-wise differences: [[3 3 3]]
NumPy diff with Periodic Boundary
By default, numpy.diff() function treats the array as open-ended, meaning it calculates differences between adjacent elements. However, we can specify the prepend or append arguments to include boundary elements for periodic data.
Example
In the following example, we use numpy.diff() function with the prepend argument to treat the array as a periodic sequence −
import numpy as np # Define an array a = np.array([1, 3, 6, 10, 15]) # Compute the first-order differences with a periodic boundary periodic_diff = np.diff(a, append=a[0]) print("Periodic differences:", periodic_diff)
The result will be as follows −
Periodic differences: [ 2 3 4 5 -14]
The numpy.gradient() Function
The numpy.gradient() function computes the gradient of an array. The gradient is a multi-dimensional generalization of the derivative.
For a 1D array, it calculates the differences between consecutive elements and takes into account edge effects. For multi-dimensional arrays, it computes the gradient along each axis.
Example
In this example, the gradient() function calculates the gradient of an array −
import numpy as np # Create a NumPy array array = np.array([1, 2, 4, 7, 11]) # Compute the gradient gradient = np.gradient(array) print(gradient)
The output represents the rate of change between each element, with special handling at the boundaries −
[1. 1.5 2.5 3.5 4. ]
The numpy.ediff1d() Function
The numpy.ediff1d() function computes the differences between consecutive elements of an array in a flattened form. It is a simpler and faster alternative to numpy.diff() function for 1D arrays.
Example
In the following example, the ediff1d() function calculates the difference between each consecutive element of the array −
import numpy as np # Create a NumPy array array = np.array([1, 2, 4, 7, 11]) # Compute the element-wise differences ediff1d_result = np.ediff1d(array) print(ediff1d_result)
This will produce the following result −
[1 2 3 4]