subtract
Description
Subtract arguments element-wise.
Mandatory Input Parameters
Parameter |
Type |
Description |
|---|---|---|
x1, x2 |
array_like |
Arrays or scalars to be subtracted from each other. If x1.shape!=x2.shape, they must be broadcastable to a common shape. |
Optional Input Parameters
Return Value
Type |
Description |
|---|---|
ndarray/scalar |
Difference of x1 and x2, element-wise |
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | >>> import numpy as np >>> np.subtract(1.1, 5.5) -4.4 >>> x1 = np.arange(9.0).reshape((3,3)) >>> x2 = np.arange(3.0) >>> x1 array([[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]]) >>> x2 array([0., 1., 2.]) >>> >>> np.subtract(x1, x2) array([[ 0., 0., 0.], [ 3., 3., 3.], [ 6., 6., 6.]]) >> # If both x1 and x2 are of the ndarray type, you can use - to replace np.subtract. >>> x1 = np.arange(0, 4).reshape((2,2)) >>> x1 array([[0, 1], [2, 3]]) >>> x2 = np.arange(4, 8).reshape((2,2)) >>> x2 array([[4, 5], [6, 7]]) >>> >>> x1 - x2 array([[ -4, -4], [ -4, -4]]) >>> |
Parent topic: Basic Operation Functions