numpy.subtract(x1, x2, dtype=None)
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.
- 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.
We start from two arrays, a and b, of dimensions 3x3, defined as follows:
If we subtract b from a using the numpy.subtract function, the result is the following:
We observe how the type of the resulting array is int32 , as well as the arrays involved.
This operation is similar to a - b:
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:
If the subtrahend cannot be expanded to cover the minuend, the function returns an error:
If we specify the value of the dtype argument, the array resulting from the subtraction takes this value: