Python

# NumPy np.divide()

The divide() function in NumPy is used to perform element-by-element division of the provided arrays.

This function takes the elements from the first input array and divides them with the corresponding array in the second input array.

Let us explore further.

## Function Syntax

The function syntax is as shown below:

numpy.divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'true_divide'>

## Parameters

The function accepts the following parameters:

1. x1 – refers to the input array or array_like object whose elements act as dividends.
2. x2 – defines the input array or array_like object whose elements are used as the divisors.
3. out – represents the output array. The defined output array must have the same shape as the input.

The above are some of the standard parameters used with the divide function. Again, you can check the docs for more information.

NOTE: Although the shape of the input arrays can be different, they must be broadcastable to a standard shape.

## Return Value

The divide function will then return an array with the results of dividing the elements of x1 and x2. (x1/x2).

The function will return a scalar value if both arrays contain scalar elements. Otherwise, the function will return an array.

NOTE: Dividing by zero (if x2 contains a 0) will result in an error.

## Example #1

The code below shows how to use the divide function to divide two scalar values.

# import numpy
import numpy as np
print(np.divide(20,2))

We pass two scalar values instead of an array to the divide function in this example.

Since the divide function performs a true division, it will always return a floating-point value as shown:

10.0

## Example #2

Consider the second example shown below:

x1 = np.array([11,45,22])
x2 = np.array([3,4,5])
print(np.divide(x1, x2))

In this example, we have two one-dimensional arrays. We then perform an element-by-element division against them using the divide function.

This operation should return an array as shown below:

[ 3.66666667 11.25 4.4]

## Example #3

In some cases, you may want to divide an array with a common divisor. For example, as shown, we can divide all elements of an array with the common divisor of 2.

arr_2d = np.array([[12,43,76], [23,86,69]])
divisor = 2
print(np.divide(arr_2d, divisor))

We have a 2D array and a divisor as a scalar value in this case. To divide all the elements in the array with a divisor, we can arr_2d as x1 and the scalar values as x2.

The operation should return output as:

[[ 6.  21.5 38. ]
[11.5 43.  34.5]]

## Example #4

As mentioned, the function will return an error if any of the elements in the x2 parameter is equal to zero.

The code below demonstrates this functionality.

arr_2d = np.array([[12,43,76], [23,86,69]])
divisor = np.array([[0,1,3], [0,4,5]])
print(np.divide(arr_2d, divisor))

In this case, two elements in the divisor array are equal to zero. Therefore, running the code above should return an error as shown: NOTE: Although the function returns an error, it will attempt to perform the division operation and return the corresponding values.

## Conclusion

In this article, we covered the division function in NumPy. This function allows you to perform an element-wise division between two arrays.
Thanks for reading & Happy coding!! 