How to get element-wise true division of an array using Numpy?
Last Updated :
13 Mar, 2023
True Division in Python3 returns a floating result containing the remainder of the division. To get the true division of an array, NumPy library has a function numpy.true_divide(x1, x2). This function gives us the value of true division done on the arrays passed in the function. To get the element-wise division we need to enter the first parameter as an array and the second parameter as a single element.
Syntax: np.true_divide(x1,x2)
Parameters:
- x1: The dividend array
- x2: divisor (can be an array or an element)
Return: If inputs are scalar then scalar; otherwise array with arr1 / arr2(element- wise) i.e. true division
Now, let's see an example:
Example 1:
Python3
# import library
import numpy as np
# create 1d-array
x = np.arange(5)
print("Original array:",
x)
# apply true division
# on each array element
rslt = np.true_divide(x, 4)
print("After the element-wise division:",
rslt)
Output :
Original array: [0 1 2 3 4]
After the element-wise division: [0. 0.25 0.5 0.75 1. ]
The time complexity of applying true division on each array element using NumPy's true_divide() function is also O(n), since we're applying a single operation to each element in the array. In this case, since the size of the array is 5, the time complexity of this operation is O(5) = O(1).
The auxiliary space complexity of this code is O(n), since we're creating a new array rslt with the same number of elements as the original array x. However, since the size of the array is fixed at 5, the space complexity of this code is O(5) = O(1).
Example 2:
Python3
# import library
import numpy as np
# create a 1d-array
x = np.arange(10)
print("Original array:",
x)
# apply true division
# on each array element
rslt = np.true_divide(x, 3)
print("After the element-wise division:",
rslt)
Output:
Original array: [0 1 2 3 4 5 6 7 8 9] After the element-wise division: [0. 0.33333333 0.66666667 1. 1.33333333 1.66666667 2. 2.33333333 2.66666667 3. ]
Time complexity: O(n), where n is the length of the array x.
Auxiliary space: O(n), as a new array of size n is created to store the result of the element-wise division.
Similar Reads
NumPy| How to get the unique elements of an Array To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4
2 min read
Using NumPy to Convert Array Elements to Float Type There are often when we must convert an array in Python to a differing type. One of these times would be when given an array and having to convert it to an array of float types. This is often useful when conducting data analysis and there are a variety of ways of doing this. Whilst iterating through
5 min read
How to get the floor, ceiling and truncated values of the elements of a numpy array? In this article, let's discuss how to get the floor, ceiling, and truncated values of the elements of a Numpy array. First, we need to import the NumPy library to use all the functions available in it. This can be done with this import statement: import numpy as np Getting the floor value The greate
3 min read
How to calculate the element-wise absolute value of NumPy array? Let's see the program for finding the element-wise absolute value of NumPy array. For doing this task we are using numpy.absolute() function of NumPy library. This mathematical function helps to calculate the absolute value of each element in the array. Syntax: numpy.absolute(arr, out = None, ufunc
2 min read
How to get the n-largest values of an array using NumPy? Let's see the program for how to get the n-largest values of an array using NumPy library. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing. Syntax: numpy.argsort(arr, ax
2 min read