w3resource

NumPy: Suppresses the use of scientific notation for small numbers in NumPy array

NumPy: Array Object Exercise-84 with Solution

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:

Previous: Write a NumPy program to display NumPy array elements of floating values with given precision.
Next: Write a NumPy program to create a NumPy array of 10 integers from a generator.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://198.211.115.131/python-exercises/numpy/python-numpy-exercise-84.php