numpy.floor_divide() in Python
Last Updated :
27 Aug, 2022
numpy.floor_divide(arr1, arr2, /, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is divided by the elements from second array(all happens element-wise). Both arr1 and arr2 must have same shape. It is equivalent to the Python // operator and pairs with the Python % (remainder), function so that b = a % b + b * (a // b) up to roundoff. Parameters :
arr1 : [array_like]Input array or object which works as numerator.
arr2 : [array_like]Input array or object which works as denominator.
out : [ndarray, None, optional]Output array with same dimensions as
Input array, placed with result.
**kwargs : Allows you to pass keyword variable length of argument to a function.
It is used when we want to handle named argument in a function.
where : [array_like, optional]True value means to calculate the universal
functions(ufunc) at that position, False value means to leave the
value in the output alone.
Return :
An array with floor(x1 / x2)
Code 1 : arr1 divided by arr2
Python
# Python program explaining
# floor_divide() function
import numpy as np
# input_array
arr1 = [2, 2, 2, 2, 2]
arr2 = [2, 3, 4, 5, 6]
print ("arr1 : ", arr1)
print ("arr1 : ", arr2)
# output_array
out = np.floor_divide(arr1, arr2)
print ("\nOutput array : ", out)
Output :
arr1 : [2, 2, 2, 2, 2]
arr1 : [2, 3, 4, 5, 6]
Output array : [1 0 0 0 0]
Code 2 : elements of arr1 divided by divisor
Python
# Python program explaining
# floor_divide() function
import numpy as np
# input_array
arr1 = [2, 7, 3, 11, 4]
divisor = 3
print ("arr1 : ", arr1)
# output_array
out = np.floor_divide(arr1, divisor)
print ("\nOutput array : ", out)
Output :
arr1 : [2, 7, 3, 11, 4]
Output array : [0 2 1 3 1]
Code 3 : floor_divide handling results if arr2 has -ve elements
Python
# Python program explaining
# floor_divide() function
import numpy as np
# input_array
arr1 = [2, 6, 21, 21, 12]
arr2 = [2, 3, 4, -3, 6]
print ("arr1 : ", arr1)
print ("arr2 : ", arr2)
# output_array
out = np.floor_divide(arr1, arr2)
print ("\nOutput array : ", out)
Output :
arr1 : [2, 6, 21, 21, 12]
arr2 : [2, 3, 4, -3, 6]
Output array : [ 1 2 5 -7 2]
Time Complexity: O(1)
Auxiliary Space: O(1)
References : https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.floor_divide.html .
Similar Reads
numpy.divide() in Python numpy.divide(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is divided by elements from second element (all happens element-wise). Both arr1 and arr2 must have same shape and element in arr2 must not be zero; otherwise it will
3 min read
numpy.floor() in Python The numpy.floor() function returns the largest integer less than or equal to each element in the input array. It effectively rounds numbers down to the nearest whole number. Let's understand with an example:Pythonimport numpy as np a = [0.5, 1.5, 2.5, 3, 4.5, 10.1] res = np.floor(a) print("Floored:"
1 min read
numpy.true_divide() in Python (arr1, arr22, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, ufunc 'true_divide') : Array element from first array is divided by the elements from second array(all happens element-wise). Both arr1 and arr2 must have same shape. Returns true division element-wise. Python trad
3 min read
Python | Numpy ndarray.__ifloordiv__() With the help of Numpy ndarray.__ifloordiv__(), we can divide a particular value that is provided as a parameter in the ndarray.__ifloordiv__() method. Value will be divided to each and every element in a numpy array and remember it always gives the floor value after division. Syntax: ndarray.__iflo
1 min read
numpy.gcd() in Python numpy.gcd() function computes the greatest common divisor (GCD) of two integers element-wise. The GCD of two numbers is the largest positive integer that divides both numbers without leaving a remainder.Pythonimport numpy as np res = np.gcd(36, 60) print(res)Output12 The GCD of 36 and 60 is 12, whic
2 min read
Python | Numpy np.legdiv() method np.legdiv() method is used to divide one Legendre series to another.It returns the quotient-with-remainder of two Legendre series c1 / c2. Syntax : np.legdiv(c1, c2) Parameters: c1, c2 :[ array_like ] 1-D arrays of Legendre series coefficients ordered from low to high. Return : [ndarray] Legendre se
1 min read
Python | Numpy numpy.ndarray.__divmod__() With the help of Numpy numpy.ndarray.__divmod__() method, we will get two arrays one is having elements that is divided by value that is provided in numpy.ndarray.__divmod__() method and second is having elements that perform mod operation with same value as provided in numpy.ndarray.__divmod__() me
1 min read
numpy.mod() in Python numpy.mod() is another function for doing mathematical operations in numpy.It returns element-wise remainder of division between two array arr1 and arr2 i.e. arr1 % arr2 .It returns 0 when arr2 is 0 and both arr1 and arr2 are (arrays of) integers. Syntax : numpy.mod(arr1, arr2, /, out=None, *, where
2 min read
Python | Numpy ndarray.__imod__() With the help of Numpy ndarray.__imod__(), every element in an array is operated on binary operator i.e mod(%). Remember we can use any type of values in an array and value for mod is applied as the parameter in ndarray.__imod__(). Syntax: ndarray.__imod__($self, value, /) Return: self%=value Exampl
1 min read
Python | Numpy MaskedArray.__div__ numpy.ma.MaskedArray class is a subclass of ndarray designed to manipulate numerical arrays with missing data. With the help of Numpy MaskedArray.__div__ we can divide a particular value that is provided as a parameter in the MaskedArray.__div__() method. Syntax: numpy.MaskedArray.__div__ Return: Di
1 min read