In this tutorial, I’ll explain how to use the Numpy divide function – AKA np.divide – to divide the values of one Numpy array by another.
I’ll explain the syntax of np.divide, how the function works, and how to use it.
If you need something specific, you can click on any of the following links.
Table of Contents:
Ok. Let’s get to it.
A Quick Introduction to Numpy Divide
The Numpy divide function – as you might have guessed – divides Numpy arrays.
The most important way to use this function is to divide two same-sized arrays. When you divide two same sized arrays, np.divide will divide values of the arrays, element-wise. This is sometimes called “Hadamard division,” since it is analogous to the Hadamard product, which is performed in Numpy by the Numpy multiply function.
You can also use the function to divide a Numpy array by a scalar value (i.e., divide a matrix by a scalar).
And you can use it to divide a Numpy array by a 1-dimensional array (or smaller array). This is similar to dividing a matrix by a vector in linear algebra. The way this is implemented in Numpy is with a technique called “broadcasting.”
For the most part, this technique is fairly easy to understand, and I’ll show you the various ways to use it in the examples section.
But first, let’s look at the syntax.
The syntax of np.divide
The syntax for Numpy divide function is fairly simple:
Keep in mind that the syntax above assumes that you’ve imported Numpy with the alias
Format of the input arrays
Before I move on, I want to make a few comments about the two input arrays.
Notice in the syntax explanation above that the np.divide function has two input arguments. In the image, I’ve named these arguments
arr1. Typically, these inputs will be Numpy arrays or array-like objects like Python lists.
Additionally, there are some restrictions on the shape of the input arrays. How the function operates will be dictated by the shape of the inputs.
The first way to use np.divide is with two same-sized arrays (i.e., arrays with exactly the same number of rows and columns). If the two input arrays have the same shape, then Numpy divide will divide the elements of the first array by the elements of the second array, in an element-wise fashion.
Alternatively, you can provide input arrays that have a different shape. But in this case, the second array will need to be of an appropriate size such that it can be broadcasted across the first array. I’ll show you an example of broadcasting in the examples section.
In addition to the two input arguments, the Numpy divide function has a few optional parameters:
These are somewhat rarely used, but so I won’t explain them here.
Output of np.divide
The output of np.division is a new Numpy array that contains the element-wise division of the input array values. Note that np.divide performs true division instead of floor division.
Additionally, there is a special case for scalars. If the both inputs to np.divide are scalar values, then the output will be a scalar.
Examples: how to perform division with Numpy arrays
Now that we’ve looked at the syntax of Numpy divide, let’s look at some examples.
- Use Numpy divide on two scalars
- Use Numpy divide with one array and one scalar
- Divide two same-sized Numpy arrays
- Divide differently sized Numpy arrays with broadcasting (i.e., divide a matrix by a vector)
Preliminary code: Import Numpy and Create Arrays
Before you run these examples, you’ll need to run some code to import Numpy and to create some sample arrays that we can use.
First, let’s just import Numpy.
You can import Numpy with the following code:
import numpy as np
Next, let’s create some arrays that we can work with in our examples.
We’ll actually create a few different types of arrays:
- a 1-dimensional ‘vector’ of numbers
- a 2-dimensional ‘matrix’ of the numbers from 1 to 9 (ordered)
- a 2-dimensional ‘matrix’ of the numbers from 1 to 9 (randomized)
To do this, we’ll use a several Numpy tools, like the Numpy array function, the Numpy arange function, Numpy reshape, and Numpy random choice.
We’ll use Numpy array to create a 1-dimensional array with three values. We’ll use Numpy arange to create an array with a sequence of numbers. We’ll use Numpy reshape to create an ordered 2D array of numbers, created from our 1D sequence of numbers. And we’ll use Numpy random choice to create a randomized 2D array created from our 1D sequence.
If you’re confused about any of these operations, click on the links above. The associated tutorials will explain everything in more detail.
# CREATE 1D 'VECTOR' vector_1d = np.array([10,20,30]) # CREATE 2D MATRIX OF NUMBERS, 1 TO 9 numbers_1_to_9 = np.arange(start = 1, stop = 10) matrix_2d_ordered = numbers_1_to_9.reshape((3,3)) # CREATE 2D MATRIX OF NUMBERS, 1 TO 9, RANDOMIZED np.random.seed(22) matrix_2d_random = np.random.choice(size = (3,3), a = numbers_1_to_9, replace = False)
After you run this code to create the arrays, you’ll be ready to run the examples.
EXAMPLE 1: Use the Numpy divide on two scalars
Let’s start with a simple example.
Here, we’ll use Numpy divide to divide one scalar value by another scalar value.
This is very simple.
Here, we’re simply dividing 12 by 4. The result is 3.0.
EXAMPLE 2: Divide an array by a scalar
Next, we’ll divide a 2-dimensional Numpy array by a scalar value.
array([[0.5, 1. , 1.5], [2. , 2.5, 3. ], [3.5, 4. , 4.5]])
Here, we’ve divided every value of
matrix_2d_ordered by the scalar value 2.
The output is simply every value of
matrix_2d_ordered divided by 2.
EXAMPLE 3: Divide two same-sized Numpy arrays
Next, let’s operate on two same-sized arrays.
Specifically, we’ll divide
array([[0.11111111, 1. , 0.75 ], [1.33333333, 0.71428571, 6. ], [0.875 , 1.6 , 1.5 ]])
Here, the Numpy divide function is dividing each value of
matrix_2d_ordered by the corresponding value in
Another way of saying this is that it’s performing element-wise division of the array values (i.e., Hadamard division).
Notice that the output array is the same size as the input arrays.
EXAMPLE 4: Divide a matrix by a vector (i.e., broadcasting)
Finally, let’s divide a 2-dimensional array by a 1-dimensional array.
array([[0.1 , 0.1 , 0.1 ], [0.4 , 0.25, 0.2 ], [0.7 , 0.4 , 0.3 ]])
In this example, we’ve divided
When we do this, Numpy “broadcasts” the 1D array across the 2D array as it performs the division (i.e., it broadcasts
So the function divides the values of row 1
vector_1d, element wise. Then it divides the values of row 2
vector_1d. And so on.
Notice that this is possible because the number of elements in
vector_1d is the same as the number of elements in each row of
matrix_2d_ordered. For broadcasting to work like this, the arrays need to be sized such that the second array can be broadcast over the first.
Leave your other questions in the comments below
Do you have other questions about how to use Numpy divide on Numpy arrays?
If so, leave your questions in the comments section below.
Join our course to learn more about Numpy
In this tutorial, I’ve explained how to divide Numpy arrays with np.divide.
This should help you with array division, but if you really want to learn Numpy, there’s a lot more to learn.
If you’re serious about mastering Numpy, and serious about data science in Python, you should consider joining our premium course called Numpy Mastery.
Numpy Mastery will teach you everything you need to know about Numpy, including:
- How to create Numpy arrays
- How to reshape, split, and combine your Numpy arrays
- What the “Numpy random seed” function does
- How to use the Numpy random functions
- How to perform mathematical operations on Numpy arrays
- and more …
Moreover, this course will show you a practice system that will help you master the syntax within a few weeks. We’ll show you a practice system that will enable you to memorize all of the Numpy syntax you learn. If you have trouble remembering Numpy syntax, this is the course you’ve been looking for.
Find out more here:
2 thoughts on “How to Use the Numpy Divide Function”
dope stuff, helped me with this csr stuff