# Numpy Divide | How to Use Numpy.divide() Function in Python

The Numpy divide function is a part of numpy arithmetic operations. There are basic arithmetic operators available in the numpy module, which are add, subtract, multiply, and divide. The significance of the python divide is equivalent to the division operation in mathematics.

Contents

## What does Numpy Divide Function do?

The numpy divide function calculates the division between the two arrays. It calculates the division between the two arrays, say a1 and a2, element-wise. The numpy.divide() is a universal function, i.e., supports several parameters that allow you to optimize its work depending on the specifics of the algorithm.

## Syntax of Numpy Divide

``numpy.divide(a1, a2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘divide’)``

## Parameters of Numpy Divide

• a1: [arrayLike]
1st Input array for calculating the division.
• a2: [arrayLike]
2nd input array for calculating the division.
• out: [ndarray, None, or tuple of ndarray and None, optional]
out will be the location where the result is to be stored. have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
• where: [array_like, optional]
If the value of where is true, it indicates to calculate the unfunc at that position, whereas if the value is false, then it denotes to leave the value in output only.

## Return Value of Numpy Divide

The divide function returns the division between a1 and a2. The divide() function can be scalar of nd-array. It depends on the a1 and a2. If a1 and a2 are scalar, than numpy.divide() will return a scalar value. Else it will return an nd-array.

Note: The input a1 and a2 must be broadcastable to a common shape (which becomes the shape of the output).

## Examples of Numpy Divide Function

Let’s go through the examples of Numpy divide() function and see how it works.

### Example 1: Using Np.divide() Function To Find Division of two numbers

```import numpy as np
a1 = 24
a2 = 13

print ("1st Input number : ", a1)
print ("2nd Input number : ", a2)

div = np.divide(a1, a2)
print ("Division of two input number : ", div)
```

Output:

``````1st Input number :  24
2nd Input number :  13
Division of two input number :  1.8461538461538463``````

### Explanation

In this simple first example, we just divided two numbers and get the result. Let’s take a look at each step and know what happens in each stage. First of all, we imported the numpy module as np it’s obvious because we are working on the numpy library. After that, we have taken two pre-defined inputs ’24’, ’13’, and stored them in variables ‘a1’, ‘a2’ respectively. We printed our inputs to check whether they are specified properly or not. Then the main part comes where we will find the division between the two numbers.

Herewith the help of the np.divide() function, we will calculate the division between a1 and a2. This division operation is identical to what we do in mathematics.

So, we will get the division between the number 24 and 13 which is 11.

### Example 2: Using Np.Divide() Function to find the division between two input arrays

```import numpy as np
a1 = [20, 21, 5, 9]
a2 = [13, 17, 6, 11]

print ("1st Input array : ", a1)
print ("2nd Input array : ", a2)

div = np.divide(a1, a2)
print ("Division of two input arrays : ", div)
```

Output:

```1st Input array :  [20, 21, 5, 9]
2nd Input array :  [13, 17, 6, 11]
Division of two input arrays :  [1.53846154 1.23529412 0.83333333 0.81818182]
```

### Explanation

From this example, things get Lil bit tricky; instead of numbers, we have used arrays as our input value.
We can now see we have two input arrays a1 & a2 with array inputs [20, 21, 5, 9] and [13, 17, 6, 11], respectively. The divide() function will find the division between a1 & a2 array arguments, element-wise.

So, the solution will be an array with the shape equal to input arrays a1 and a2. The division between a1 and a2 will be calculated parallelly, and the result will be stored in the ad variable.

### Example 3: Using Np.divide() Function To Find Division Between Two Multi-Dimensional Arrays

```import numpy as np
a1 = [[20, 21, 5], [-9, 11, 1]]
a2 = [[13, 17, 6], [1, -8, 7]]

print ("1st Input array : ", a1)
print ("2nd Input array : ", a2)

div = np.divide(a1, a2)
print ("Division of two input arrays : ", div)
```

Output:

``````1st Input array :  [[20, 21, 5], [-9, 11, 1]]
2nd Input array :  [[13, 17, 6], [1, -8, 7]]
Division of two input arrays :  [[ 1.53846154  1.23529412  0.83333333]
[-9. -1.37  0.14285714]]``````

### Explanation

The third example in this divide() function tutorial is slightly similar to the second example which we have already gone through. What we have done here in this example is instead of a simple array we have used a multi-dimensional array in both of our input values a1 and a2.

Make sure both the input arrays should be of the same dimension and same shapes. The numpy.divide() function will find the Division between array arguments, element-wise.

## Can We Find Division Between Two Numpy Arrays With Different Shapes?

In simple words, No, we can’t find division or use the numpy divide function in two numpy arrays that have different shapes.

Let’s look it through one example,

```import numpy as np
a1 = [[20, 21, 5], [-9, 11, 1]]
a2 = [[7, 13, 17, 6], [1, -8, 7]]

print ("1st Input array : ", a1)
print ("2nd Input array : ", a2)

div = np.divide(a1, a2)
print ("Division of two input arrays : ", div)
```

Output:

``ValueError: operands could not be broadcast together with shapes (2,3) (2,)``

### Explanation

If the shape of two numpy arrays will be different than we will get a value error. The value error will say something like for example.

`ValueError: operands could not be broadcast together with shapes (2,3) (2,)`

Here in this example, we get a value error because the a2 input array has a different shape than the a1 input array. In order to get the division without any value error, make sure to check the shape of arrays.

## What’s Next?

NumPy is very powerful and incredibly essential for information science in Python. That being true, if you are interested in data science in Python, you really ought to find out more about Python.

You might like our following tutorials on numpy.

## Conclusion

The numpy divide() is a compelling and essential function available in the numpy module, which can be very useful and highly recommended by many experts while finding the division between very large data sets.

If you still have any questions regarding the NumPy divide function. Leave your question in the comments below.

Happy Pythonning!

Subscribe
Notify of 