NumPy: Suppresses the use of scientific notation for small numbers in NumPy array
Suppress Scientific Notation in Array
Write a NumPy program to suppress the use of scientific notation for small numbers in a NumPy array.
Sample Solution:
Python Code:
# Importing the NumPy library and aliasing it as 'np'
import numpy as np
# Creating a NumPy array 'x' containing floating-point values
x = np.array([1.6e-10, 1.6, 1200, .235])
# Printing a message indicating the original array elements will be shown
print("Original array elements:")
# Printing the original array 'x' with its elements
print(x)
# Printing a message indicating the suppression of scientific notation in the array display
print("Array - scientific notation is suppressed")
# Setting the print options to suppress scientific notation in array printing
np.set_printoptions(suppress=True)
# Printing the array 'x' with scientific notation suppressed
print(x)
Sample Output:
Original array elements: [1.60e-10 1.60e+00 1.20e+03 2.35e-01] Print array values with precision 3: [ 0. 1.6 1200. 0.235]
Explanation:
In the above code –
x = np.array([1.6e-10, 1.6, 1200, .235]): Create a NumPy array x containing 4 float values, including a very small number and numbers with different magnitudes.
np.set_printoptions(suppress=True): Set the print options for NumPy arrays, specifying that the scientific notation should be suppressed when displaying the elements.
print(x): Print the NumPy array x.
Python-Numpy Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics