w3resource

NumPy: Get true division of the element-wise array inputs

NumPy Mathematics: Exercise-3 with Solution

Write a NumPy program to get true division of the element-wise array inputs.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Creating an array from 0 to 9
x = np.arange(10)

# Displaying the original array
print("Original array:")
print(x)

# Performing element-wise division of the array by 3 using true_divide function
print("Division of the array inputs, element-wise:")
print(np.true_divide(x, 3)) 

Sample Output:

Original array:                                                        
[0 1 2 3 4 5 6 7 8 9]                                                  
Division of the array inputs, element-wise:                            
[ 0.          0.33333333  0.66666667  1.          1.33333333  1.6666666
7                                                                      
  2.          2.33333333  2.66666667  3.        ]

Explanation:

In the above exercise -

x = np.arange(10): This line creates an array of integers from 0 to 9 using np.arange(10).

np.true_divide(x, 3): This line performs element-wise true division of x by 3.

The output of this code will be a NumPy array where each element is the corresponding element from x divided by 3, and the result is a floating-point number. So, the output will be an array of decimal values.

Python-Numpy Code Editor:

Previous: Write a NumPy program to compute logarithm of the sum of exponentiations of the inputs, sum of exponentiations of the inputs in base-2.
Next: Write a NumPy program to get the largest integer smaller or equal to the division of the inputs.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.