NumPy divide()
The numpy.divide()
function performs element-wise division of two arrays. It supports broadcasting, allowing division between arrays of different shapes.
Syntax
</>
Copy
numpy.divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x1 | array_like | The dividend array. |
x2 | array_like | The divisor array. If x1 and x2 have different shapes, they must be broadcastable. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior for the operation. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array containing the element-wise division results of x1 / x2
. If both x1
and x2
are scalars, a scalar is returned.
Examples
1. Element-wise Division of Two Arrays
Performing element-wise division of two equally shaped arrays.
</>
Copy
import numpy as np
# Define two arrays
x1 = np.array([10, 20, 30, 40])
x2 = np.array([2, 4, 5, 8])
# Compute element-wise division
result = np.divide(x1, x2)
# Print the results
print("Element-wise division result:", result)
Output:
Element-wise division result: [ 5. 5. 6. 5. ]

2. Broadcasting in NumPy divide()
Using broadcasting when one of the arrays is a scalar or a lower-dimensional array.
</>
Copy
import numpy as np
# Define an array and a scalar divisor
x1 = np.array([[10, 20, 30], [40, 50, 60]])
x2 = 10 # Scalar divisor
# Compute division using broadcasting
result = np.divide(x1, x2)
# Print the results
print("Broadcasted division result:\n", result)
Output:
Broadcasted division result:
[[1. 2. 3.]
[4. 5. 6.]]

3. Using the out
Parameter
Storing the output of the division in a pre-allocated array.
</>
Copy
import numpy as np
# Define two arrays
x1 = np.array([12, 24, 36])
x2 = np.array([3, 6, 9])
# Create an output array
out_array = np.ndarray(x1.shape)
# Compute division and store result in out_array
np.divide(x1, x2, out=out_array)
# Print the results
print("Stored output array:", out_array)
Output:
Stored output array: [4. 4. 4.]

4. Using the where
Parameter
Computing division only for selected elements using a boolean mask.
</>
Copy
import numpy as np
# Define two arrays
x1 = np.array([10, 20, 30, 40])
x2 = np.array([2, 4, 5, 8])
# Define a mask (only divide for selected elements)
mask = np.array([True, False, True, False])
# Compute division where mask is True
result = np.divide(x1, x2, where=mask)
# Print the results
print("Division result with mask:", result)
Output:
Division result with mask: [5. 0. 6. 0.]

Only the elements where mask=True
are computed. The others retain their original values.