# Difference between two NumPy arrays

Learn about the difference between two NumPy arrays in Python.
By Pranit Sharma Last updated : December 21, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

## Problem statement

Suppose that we are given two numpy arrays of integer values and we need to find the difference between these two numpy arrays.

## Finding the difference between of two NumPy arrays

The difference between two numpy arrays is that each value of the second array will get subtracted from its corresponding value in first array.

There are two approaches:

1. By subtracting one NumPy array from another
2. By using the numpy.subtract() method

## By subtracting one NumPy array from another

For this purpose, we will simply subtract the second array from the first array as a whole rather than subtract each value from both arrays.

Let us understand with the help of an example,

### Python code to find the difference between two NumPy arrays

# Import numpy
import numpy as np

# Creating two numpy arrays
arr1 = np.array([100, 200, 14, 9, 45, 112, 237, 974, 32, 2])
arr2 = np.array([398, 283, 23, 54, 23, 63, 2, 67, 2, 87])

# Display original arrays
print("Original Array 1:\n",arr1,"\n")
print("Original Array 2:\n",arr2,"\n")

# Subtracting arr2 from arr1
res = arr2 - arr1

# Display Result
print("Result:\n",res)

## By using the numpy.subtract() method

The numpy.subtract() method can also be used to find the difference of two NumPy arrays.

### Python code to find the difference between two NumPy arrays using subtract() method

# Import numpy
import numpy as np

# Creating two numpy arrays
arr1 = np.array([100, 200, 14, 9, 45, 112, 237, 974, 32, 2])
arr2 = np.array([398, 283, 23, 54, 23, 63, 2, 67, 2, 87])

# Display original arrays
print("Original Array 1:\n",arr1,"\n")
print("Original Array 2:\n",arr2,"\n")

# Subtracting arr2 from arr1
res = np.subtract(arr2, arr1)

# Display Result
print("Result:\n",res)

#### Output

Same as the above.