🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.subtract()

AI & DATA SCIENCE // np-subtract

np.subtract() subtracts one array (or scalar) from another, element-wise, and is exactly what the - operator calls on ndarrays.

Syntax

np.subtract(x1, x2)
x1 - x2

Deep Dive Course

Like np.add(), np.subtract(a, b) is the ufunc underlying the - operator, computing a minus b element-wise and following the same broadcasting rules for arrays of differing but compatible shapes. A common use is computing element-wise differences or distances, such as subtracting a mean array from a dataset to center it, or subtracting one array of coordinates from another to get displacement vectors.

1Understanding np.subtract()

Like np.add(), np.subtract(a, b) is the ufunc underlying the - operator, computing a minus b element-wise and following the same broadcasting rules for arrays of differing but compatible shapes. A common use is computing element-wise differences or distances, such as subtracting a mean array from a dataset to center it, or subtracting one array of coordinates from another to get displacement vectors.

💡

Subtracting arrays of unsigned integer dtypes, like uint8, can silently wrap around to a huge positive number instead of going negative, since unsigned types can't represent negative values — cast to a signed type first if the subtraction result could be negative.

editor.html
import numpy as np

a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
print(np.subtract(a, b))
localhost:3000

2Practical Example

Here is a real-world application of np.subtract() showing how it is used in production NumPy code.

editor.html
import numpy as np

data = np.array([[1, 2], [3, 4], [5, 6]])
mean = data.mean(axis=0)
centered = data - mean
print(centered)
localhost:3000

3Best Practices

Follow these guidelines when working with np.subtract():

1. Cast unsigned integer arrays to a signed dtype before subtracting if the result could be negative, to avoid silent integer wraparound

2. Use broadcasting, e.g. subtracting a 1D mean array from a 2D dataset, instead of looping row by row to center or normalize data

3. Use np.subtract(a, b, out=result_array) when you specifically want to write into a pre-allocated array instead of creating a new one

⚠️

Tip: Subtracting arrays of unsigned integer dtypes, like uint8, can silently wrap around to a huge positive number instead of going negative, since unsigned types can't represent negative values — cast to a signed type first if the subtraction result could be negative.

editor.html
import numpy as np

a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
print(np.subtract(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
print(np.subtract(a, b))
Example 02Advanced Example
import numpy as np

data = np.array([[1, 2], [3, 4], [5, 6]])
mean = data.mean(axis=0)
centered = data - mean
print(centered)

Best Practices

  • Cast unsigned integer arrays to a signed dtype before subtracting if the result could be negative, to avoid silent integer wraparound
  • Use broadcasting, e.g. subtracting a 1D mean array from a 2D dataset, instead of looping row by row to center or normalize data
  • Use np.subtract(a, b, out=result_array) when you specifically want to write into a pre-allocated array instead of creating a new one

Interview Question

Why can subtracting two arrays with dtype uint8 produce a surprisingly large positive number instead of a negative one?

Hint: Think about how unsigned integer types represent numbers in a fixed number of bits.

An unsigned 8-bit integer can only represent values from 0 to 255 — it has no bit pattern reserved for negative numbers. When a subtraction's true mathematical result would be negative, the fixed-width unsigned arithmetic wraps around, effectively computing the result modulo 256, which produces a large positive number instead of a negative one, silently, with no warning or error. Casting the arrays to a signed integer dtype, like int16, before subtracting avoids this wraparound entirely.

Exercises

MediumPractice using np.subtract() in a real scenario.
View Solution
import numpy as np

a = np.array([10, 20, 30])
b = np.array([1, 2, 3])
print(np.subtract(a, b))

Frequently Asked Questions

Why can subtracting two arrays with dtype uint8 produce a surprisingly large positive number instead of a negative one?

An unsigned 8-bit integer can only represent values from 0 to 255 — it has no bit pattern reserved for negative numbers. When a subtraction's true mathematical result would be negative, the fixed-width unsigned arithmetic wraps around, effectively computing the result modulo 256, which produces a large positive number instead of a negative one, silently, with no warning or error. Casting the arrays to a signed integer dtype, like int16, before subtracting avoids this wraparound entirely.

Related Functions

np-addnp-absolutendarray-dtype