numpy.floor_divide() Function [7 Python Code Examples]

numpy.floor_divide() Function explained with python code examples in jupyter notebook

In this article, we’ll learn about numpy.floor_divide() function with the help of multiple code examples in which we’ll discuss all the parameters of this function. Easy explanations of these examples will also be provided.

Python numpy.floor_divide() Funtion

We learned about the true divide function in our previous tutorial which is kind of similar to this function. But what the floor_divide() do is that it executes an element-wise floor division on its argument input arrays. Also, it specifies that the largest integer is returned which is equal to or less than each element of the division.

numpy.floor_divide() Function explained with python code examples in jupyter notebook

floor divide function of python numpy code examples

python numpy floor divide Function explanation with practical python code examples

python numpy floor divide parameters with python code examples

Syntax of floor_divide()

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

Explaining the Parameters

1. Required Parameters

  1. The parameters(x1,x2) show the input arrays on which the action will perform. The shape of them should be the same or appropriate to be broadcasted.

The (/) specifies a boundary between the positional and keyword arguments. Positional are x1, x2 and keywords are the remaining ones.

2. Optional Parameters

  1. The parameter ‘out‘ stores a resulting output, if specified. If not, then a new array stores output.
  2. The ‘*’ shows the ending of positional arguments.
  3. The ‘where‘ specifies where the operation of the division is performed. The default is True. It can take an array of Booleans as well.
  4. The ‘casting‘ sets the rules of casting when there are different datatypes of the input arrays. The default value assigned to the parameter ‘casting’ is ‘same_kind’ which defines that the resulting array will have the same datatype as specified in the input arrays.
  5. The ‘order‘ defines the output array’s memory layout. The value it takes can be ‘C'(C style) or ‘F'(Fortran Style). The default one is ‘K’. The ‘K’ specifies that the memory layout will be used by the first array that is being inputted.
  6. The ‘dtype‘ sets the data type of the resulting array. By default, the datatype is taken by the input arrays.
  7. The parameter ‘subok‘ specifies whether to make the resulting array subclass of the same class as the input array or not.
  8. The ‘ufunc ‘floor_divide” performs the floor division operation as this is a Universal Function object.

7 Practical Code Examples

Example 1: 1D Arrays with floor_divide()

import numpy as np

x1 = np.array([9, 35, 53])
x2 = np.array([8, 5, 23])

r = np.floor_divide(x1, x2)

print("Floor division output with x1 and x2:", r)


Floor division output with x1 and x2: [1 7 2]

We applied this operation on numpy arrays and the result shows the resulting integer items that are equal to or less than the result.

Example 2: Parameter ‘out’ and resulting type modification

a1 = np.array([15, 26, 8])
a2 = np.array([4, 7, 20])

o_dtype = np.float64
output = np.empty_like(x1, dtype=o_dtype)

result = np.floor_divide(a1, a2, out=output)



[3. 3. 0.]

In this code, we specified an empty array that has the same shape as the first array(x1). We also specified the type of this array to float. We then pass it to the parameter ‘out’ which stores the result. We can see that the items are now in float format. If we don’t specify the type in the array of the ‘out‘ parameter then the items will be in an integer format.

Example 3: ‘where’ Parameter

ar1 = np.array([5,8,3,8])
ar2 = np.array([4,7,2,9])

condition = np.array([True, False, True,False])
res = np.floor_divide(ar1, ar2, where=condition)



[1 7 1 9]

In this code, we passed an array of Booleans to the ‘where’ parameter. When the True comes then the operation is performed but when the False is executed then no operation is performed instead the second array item is added to the resulting array as it is.

Example 4: Parameter ‘casting’ with float array

array1 = np.array([34,76,23], dtype=np.float64)
array2 = np.array([56,21,32])

resAr = np.floor_divide(array1, array2, casting='unsafe')


[0. 3. 0.]

In this code of Python, we’ve two arrays and one is specified to be of type float. We passed the ‘unsafe‘ to parameter ‘casting‘ which specifies the casting between different datatypes. The resulting array shows that the items are now in float format.

Example 5: ‘order’ Parameter

y1 = np.array([[10, 23, 45], [12, 7, 15]])

order_layout = 'C' if y1.flags['C_CONTIGUOUS'] else 'F'

resultArray = np.floor_divide(y1, 5, order=order_layout)


[[2 4 9]
 [2 1 3]]

We specified a 2D array and also we set the order layout of this array by the use of the ‘flags‘ attribute. We then passed the order layout to the ‘order’ parameter. The resulting array will now have the specified memory layout that is passed to the ‘order’ parameter.

Example 6: ‘dtype’ Parameter of numpy.floor_divide()

arrFirst = np.array([43,52,78])
arrSecond = np.array([7,8,3])

output_datatype = np.float64
resultingArray = np.floor_divide(arrFirst, arrSecond, dtype=output_datatype)



[ 6.  6. 26.]

In this code, we have arrays of int datatypes but we passed float to the ‘dtype’ parameter. As a result, after the operation is performed and the resulting array is formed, the items are in float format.

Example 7: Using the ‘subok’ Parameter

class CustomArray(np.ndarray):

array1 = np.array([10, 23, 45], dtype=np.float64).view(CustomArray)
array2 = np.array([3, 8, 12]).view(CustomArray)

res_subclass = np.floor_divide(array1, array2, subok=True)

res_baseclass = np.floor_divide(array1, array2, subok=False)

print("x1:", x1)
print("x2:", x2)
print("Example output with 'subok=True':", res_subclass)
print("Is 'result_subclass' a subclass of 'x1'? ", isinstance(res_subclass, CustomArray))
print("\nExample output with 'subok=False':", res_baseclass)
print("Is 'result_baseclass' a subclass of 'x1'? ", isinstance(res_baseclass, CustomArray))


x1: [10. 23. 45.]
x2: [ 3  8 12]
Example output with 'subok=True': [3. 2. 3.]
Is 'result_subclass' a subclass of 'x1'?  True

Example output with 'subok=False': [3. 2. 3.]
Is 'result_baseclass' a subclass of 'x1'?  False

In this code, we created a custom class of ‘numpy.ndarray‘. We then make the 2 arrays to be instance of this class by making use of the ‘view()‘ method.

We then pass True and False to the ‘subok’ parameter. The output for both of them is different. We can see that when the ‘subok’ was false then the resulting array is a base class and not a subclass even though its input arrays were instances of the CustomArray. In the other output, we passed True to the ‘subok’ parameter which makes the resulting array to be a subclass of the CustomArray.


In conclusion, hope you have a clear and detailed practical code understanding of how numpy.floor_divide() function works. We discussed its parameters using code examples in order to better understand their role and usage.

Thank you for reading it.


Leave a Comment

Your email address will not be published. Required fields are marked *