Understanding numpy.floor_divide() function (5 examples)

Updated: February 25, 2024 By: Guest Contributor Post a comment

Introduction

Numpy is a fundamental package for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. One such tool is the numpy.floor_divide() function, which carries out floor division on arrays. This tutorial aims to delve deep into the workings of the numpy.floor_divide() function, illustrated with five examples ranging from basic to advanced.

What is Floor Division?

Floor division, denoted by the // operator in Python, is a type of division that rounds down to the nearest integer. It differs from normal division, which returns the exact quotient. numpy.floor_divide() applies this concept element-wise over numpy arrays.

Basic Example

Let’s start with the most basic example of using numpy.floor_divide():

import numpy as np

# Define two arrays
a = np.array([9, 8, 7])
b = np.array([2, 2, 2])

# Apply numpy.floor_divide
result = np.floor_divide(a, b)

print(result)

Output:

[4 4 3]

This example simply shows the floor division of two arrays, element by element.

Working with Scalars

Scalars can also be involved in numpy.floor_divide(), as shown below:

import numpy as np

a = np.array([10, 20, 30])
b = 5

result = np.floor_divide(a, b)

print(result)

Output:

[2 4 6]

Here, each element of the array a is floor-divided by the scalar b.

Handling Different Data Types

Numpy allows for operations between different data types. See how numpy.floor_divide() handles it:

import numpy as np

a = np.array([1, 2, 3], dtype=np.int64)
b = np.array([2.5, 3.5, 4.5], dtype=np.float64)

result = np.floor_divide(a, b)

print(result)

Output:

[0 0 0]

In this case, despite the differing data types (integer and float), numpy.floor_divide() manages to perform the operation seamlessly, applying type coercion as necessary.

Using with Multi-dimensional Arrays

Moving on to a slightly more advanced scenario, let’s see how numpy.floor_divide() works with multi-dimensional arrays:

import numpy as np

a = np.array([[1, 2], [3, 4]])
b = np.array([[2, 2], [2, 2]])

result = np.floor_divide(a, b)

print(result)

Output:

[[0 1] [1 2]]

This demonstrates that the operation can be applied just as easily to arrays of higher dimensions.

Advanced Example: Broadcasting

For a more intricate example, let’s explore broadcasting with numpy.floor_divide(). Broadcasting automates certain actions on arrays of different shapes, making operations more flexible. Consider:

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([2, 2, 2])

result = np.floor_divide(a, b)

print(result)

Output:

[[0 1 1] [2 2 3]]

Here, the one-dimensional array b is broadcast over the two-dimensional array a, allowing for the operation to proceed element-wise across dimensions. Broadcasting is a powerful feature that greatly enhances numpy’s flexibility.

Conclusion

The numpy.floor_divide() function is a versatile tool for performing floor division on arrays, regardless of their shapes, sizes, or data types. Through examples ranging from basic to advanced, we have seen its practical applications in scientific computing. Armed with this knowledge, you should now be able to effectively utilize numpy.floor_divide() in your own Python projects.