HOWTO · NumPy

Element-Wise Division in Python NumPy

Discover how to perform element-wise division in Python using NumPy. Learn about the two main methods: numpy.divide() function and the / operator. This article provides clear examples and explanations to help you master element-wise operations in your data analysis projects.

On this page

To divide corresponding values in NumPy arrays, use either numpy.divide(x1, x2) or x1 / x2. Both perform element-wise true division and return an array; use the function form when its optional arguments, such as out or where, help control the result.

Using numpy.divide()

numpy.divide() takes the dividend first and the divisor second. For arrays with matching shapes, each value in the first array is divided by the value at the same position in the second. Assign the returned array to a variable when you need to use it later. The out argument is optional: omit it to have NumPy create and return a result, or pass an existing array with a compatible shape and dtype when you want to place results into that storage.

Run this example:

import numpy as np

numerators = np.array([12, 18, 24])
denominators = np.array([3, 6, 8])
by_function = np.divide(numerators, denominators)
destination = np.array([0.0, 0.0, 0.0])
np.divide(numerators, denominators, out=destination)
by_operator = numerators / denominators
print(by_function)
print(by_operator)
print(destination)
[4. 3. 3.]
[4. 3. 3.]
[4. 3. 3.]

The first function call omits out and returns a newly created array. The second call writes into destination, which was created with the result’s shape and dtype. The operator uses the same inputs, so all three arrays contain the same quotients. The inputs are integers, while true division produces floating-point values.

Using the / Operator

For most expressions, / is the shortest and clearest way to divide NumPy arrays element by element. Write result = numerators / denominators; the operands are evaluated position by position, and the resulting array can be assigned, indexed, or passed to another NumPy operation. NumPy applies its array arithmetic rules when an operand is a scalar or when the two arrays have compatible shapes.

The operator is not a separate integer-division operation: with integer arrays, / performs true division and the quotient values are represented as floating point. This differs from Python’s //, which requests floor division. Choose the operator based on the desired operation, and do not use // when fractional quotients are needed.

Broadcasting, Data Types, and Zero Denominators

Arrays with the same shape divide corresponding elements directly. NumPy also broadcasts compatible shapes: dimensions are compared from the right, and each pair of dimensions must be equal or one of them must be 1. For example, a two-dimensional array with shape (2, 3) can be divided by a one-dimensional array of shape (3,), applying each divisor across a row. Shapes that do not meet the broadcasting rule cannot be divided element-wise; inspect array.shape on both operands when a shape mismatch occurs.

The resulting dtype depends on the input dtypes and division operation. In particular, integer inputs to / or numpy.divide() produce true-division results rather than truncating each quotient to an integer. If a later step requires a specific dtype, convert deliberately after division and consider whether that conversion discards fractional information.

For true division, both / and numpy.divide() produce floating-point results from integer inputs. A zero divisor can therefore produce infinity for a nonzero numerator or nan for 0 / 0, with a runtime warning, just as it can for floating-point inputs. This article covers those true-division forms; floor division with // is a different operation and is outside their behavior. If zero-divisor positions should be excluded, initialize an output array first, then use numpy.divide(a, b, out=result, where=b != 0); positions where the condition is false retain the initialized values. Choose a fill value that makes sense for the later calculation, and separately decide how to handle those excluded positions.

Choosing a Division Method

Use / for a direct expression when you want a concise element-wise quotient. Use numpy.divide() when its function arguments are useful, especially out for an existing destination array or where for selecting which positions to calculate. For either form, confirm that the operands have compatible shapes and decide how zero divisors should be treated for the dtypes in your data. These checks make the code easier to adapt to vectors, matrices, and broadcastable inputs.