NumPy: Set the precision value to 6 and print the array of scientific notation numbers
NumPy: Array Object Exercise-199 with Solution
Represent values in scientific notation with precision.
Write a NumPy program to create an array using scientific notation numbers. Set the precision value to 6 and print the array.
Sample Solution:
Python Code:
# Importing NumPy library
import numpy as np
# Creating an array with scientific notation values
nums = np.array([1.2e-7, 1.5e-6, 1.7e-5])
# Displaying the original array
print("Original arrays:")
print(nums)
# Setting the precision value to 10 and suppressing the scientific notation
np.set_printoptions(suppress=True, precision=10)
# Displaying the array with updated precision and suppressed scientific notation
print("Set the precision value to 10:")
print(nums)
Sample Output:
Original arrays: [1.2e-07 1.5e-06 1.7e-05] Set the precision value to 10: [0.00000012 0.0000015 0.000017 ]
Explanation:
In the above exercise -
nums = np.array([1.2e-7, 1.5e-6, 1.7e-5]) creates a NumPy array with three small floating-point numbers in scientific notation.
np.set_printoptions(suppress=True, precision=10) sets the global print options for NumPy. The suppress=True option disables the use of scientific notation when printing NumPy arrays, and the precision=10 option sets the number of decimal places displayed to 10.
print(nums): Finally print() function prints the generated array.
Python-Numpy Code Editor:
Previous: Write a NumPy program to create a 10x4 array filled with random floating point number values with and set the array values with specified precision.
Next: Write a NumPy program to remove a specific column from a given array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-199.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics