---
title: diff()
url: https://docs.vultr.com/python/third-party/numpy/diff
description: Calculate Discrete Differences
publish_date: 2024-11-18T16:40:00.094980Z
last_updated: 2024-11-18T16:40:00.094980Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/947/113d4c9c-0c8f-41ec-a496-bd10ea973895.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Python Numpy diff() - Calculate Discrete Differences

## Introduction
The `numpy.diff()` function in Python is a powerful tool for computing the discrete differences between consecutive elements in an array or along a specified axis in a multi-dimensional array. This function is a cornerstone for mathematical computations involving numerical data where the differences between successive elements are of interest, such as in time series analysis or differential equations.

In this article, you will learn how to effectively use the `numpy.diff()` function to analyze numerical data. Explore the different ways this function can be applied to single-dimensional and multi-dimensional arrays along with adjustments of the difference order.

## Using numpy.diff() on One-Dimensional Arrays

### Basic Difference Calculation
1. Import the `numpy` library.
1. Create a one-dimensional numpy array.
1. Apply `numpy.diff()` to calculate the consecutive differences.

    ```python
    import numpy as np

    data = np.array([1, 2, 4, 7, 11])
    differences = np.diff(data)
    print(differences)
    ```

    This code calculates the differences between consecutive elements in the `data` array. The resulting array, `[1, 2, 3, 4]`, represents the discrete differences.

### Adjusting the Difference Order
1. Understand that the order of the difference can be a crucial parameter.
1. Apply `numpy.diff()` with an increased difference order.

    ```python
    second_order_diff = np.diff(data, n=2)
    print(second_order_diff)
    ```

    This snippet calculates the second-order difference, meaning it applies the difference operation twice. The outcome `[1, 1, 1]` shows the second-order differences.

## Working with Multi-Dimensional Arrays

### Calculating Differences Along an Axis
1. Recognize how multi-dimensional arrays handle data in different axes.
1. Create a two-dimensional numpy array.
1. Utilize `numpy.diff()` to compute differences along a specified axis.

    ```python
    matrix = np.array([[1, 3, 6], [2, 4, 8]])
    col_diff = np.diff(matrix, axis=0)
    print(col_diff)
    ```

    In this example, differences between corresponding elements of rows across columns (vertical differences) are calculated because `axis=0` refers to the row-wise operation in numpy. The result is `[[1, 1, 2]]`.

### Comprehensive Array Difference Analysis
1. Create a more complex two-dimensional array.
1. Apply `numpy.diff()` across both axes to analyze changes more thoroughly.

    ```python
    complex_matrix = np.array([[1, 2, 3], [3, 5, 9], [2, 8, 7]])
    row_diff = np.diff(complex_matrix, axis=1)
    col_diff = np.diff(complex_matrix, axis=0)
    print("Row-wise differences:")
    print(row_diff)
    print("Column-wise differences:")
    print(col_diff)
    ```

    This approach calculates the differences row-wise (`axis=1`) and column-wise (`axis=0`). Row-wise results show differences within each row, and column-wise results show differences across rows, offering insights into the dataset's variation across different dimensions.

## Conclusion
The `numpy.diff()` function in Python serves as an essential tool for computing discrete differences across arrays. Its versatility in handling both one-dimensional and multi-dimensional arrays makes it indispensable for numerical analyses where understanding the incremental changes within data is crucial. Implementing `numpy.diff()` in your data analysis toolkit allows for sophisticated insights into numerical datasets, enhancing both analysis quality and performance.