numpy.subtract

Full name
numpy.subtract
Library
NumPy
Syntax

numpy.subtract(x1, x2, dtype=None)

Description

The numpy.subtract function subtracts the values of the two arrays involved, element by element. If x1 and x2 are scalars, the result of the subtraction will also be a scalar.

Parameters
  • x1: Array NumPy whose values will be considered minuends of the subtraction.
  • x2: Array NumPy whose values will be considered subtrahends of the subtraction.
  • dtype: If specified, it is assigned as the type of the resulting array, ignoring the result of the calculation.
Examples

We start from two arrays, a and b, of dimensions 3x3, defined as follows:

numpy.subtract

If we subtract b from a using the numpy.subtract function, the result is the following:

numpy.subtract

We observe how the type of the resulting array is int32 , as well as the arrays involved.

This operation is similar to a - b:

numpy.subtract

Continuing with the previous example, if the array that plays the role of the subtrahend has the dimensions of the rows of the array that plays the role of the minuend, the first is subtracted from each row of the second:

numpy.subtract

If the subtrahend cannot be expanded to cover the minuend, the function returns an error:

numpy.subtract

If we specify the value of the dtype argument, the array resulting from the subtraction takes this value:

numpy.subtract
Submitted by admin on Mon, 02/25/2019 - 17:16