# NumPy Divide Array by Scalar in Python [5 Methods]

Do you want to divide a NumPy array by a scalar? In this Python blog, I will explain how NumPy divide array by scalar in Python using different methods and some illustrative examples. I will also explain what is np.divide vs /.

To divide a NumPy array by a scalar in Python, we can use the / operator for simple element-wise division or numpy.divide for additional control over the operation. Handling division by zero is achieved with numpy.seterr. NumPy also supports element-wise division with broadcasting for arrays of different shapes, and in-place division using the /= operator.

## NumPy Divide Array by Scalar in Python Methods

There are five different methods and use cases of NumPy divide array by scalar in Python:

1. Basic array division by scalar
2. Using numpy.divide
3. Handling division by zero
4. Element-wise division with broadcasting
5. In-place division

Let’s see them one by one using some illustrative examples:

### 1. Divide NumPy array by scalar in Python using / operator

The simplest way NumPy divide array by scalar in Python is by using the division operator /. When we use this operator, each element in the array is divided by the scalar value. This operation is vectorized, meaning it’s efficiently implemented to work with arrays of any size.

READ:  Convert List to Tensor TensorFlow

Here is an example:

``````import numpy as np

populations = np.array([8.4, 3.9, 2.7, 2.3, 1.7])
result = populations / 2
print(result)``````

Output:

``[4.2  1.95 1.35 1.15 0.85]``

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

### 2. NumPy divide by scalar using np.divide()

A function called np.divide can also be used by NumPy divide array by scalar in Python. It is particularly useful when we need additional control over the operation, such as specifying the data type of the output.

Here’s a basic example to illustrate how this works:

``````import numpy as np

temperatures = np.array([68, 75, 59, 85, 72])
result = np.divide(temperatures, 2)
print(result)``````

Output:

``[34.  37.5 29.5 42.5 36. ]``

The output from running the code in PyCharm is visually represented in the screenshot below.

### 3. NumPy array divide all elements by scalar handling with Division by zero

When NumPy divide array by scalar in Python, there’s a potential risk of dividing by zero, which can raise an error or result in inf or nan values. To handle this, we can use numpy.seterr to control how NumPy handles such errors.

For instance:

``````import numpy as np

rainfall = np.array([49.9, 14.8, 36.9, 49.8, 8.2])
np.seterr(divide='ignore')
result = rainfall / 0
print(result)``````

Output:

``[inf inf inf inf inf]``

The screenshot below showcases the output generated by running the code within the PyCharm editor.

### 4. Divide array by scalar Python element-wise with broadcasting

In NumPy, we can also divide arrays of different shapes using broadcasting. For example, if we have a 2D array and a scalar, NumPy will automatically broadcast the scalar to match the shape of the array.

READ:  How to delete session property in Django

For example:

``````import numpy as np

distances = np.array([[0, 2446, 713, 1625, 2298],
[2446, 0, 1745, 1374, 372],
[713, 1745, 0, 925, 1447]])
result = distances / 2
print(result)``````

Output:

``````[[   0.  1223.   356.5  812.5 1149. ]
[1223.     0.   872.5  687.   186. ]
[ 356.5  872.5    0.   462.5  723.5]]``````

Below is an image displaying the results of the code execution in the PyCharm environment.

### 5. NumPy divide all elements by scalar with in-place division

For memory efficiency, we might want to perform the division operation in place, modifying the original array. This can be done using the /= operator.

For instance:

``````import numpy as np

gdp_growth = np.array([2.5, 3.1, 2.8, 3.6, 3.0])
gdp_growth /= 2
print(gdp_growth)``````

Output:

``[1.25 1.55 1.4  1.8  1.5 ]``

The ensuing image captures the results produced by executing the code in PyCharm.

## What the np.divide vs / is

Below is a table comparing np.divide and the / operator for dividing an array in Python, particularly using NumPy. Both methods are commonly used for array division, but they have some differences in terms of functionality and usage.

## Conclusion

Understanding different methods and use cases of how NumPy divide array by scalar in Python like / operator, numpy.divide, handling division by zero, element-wise division with broadcasting, and in-place division can help to solve one problem easily. Also, what is the main difference between np.divide() function and /(divide operator) too.

READ:  How to Copy a NumPy Array to Clipboard through Python [3 Methods]

You may also like to read: