Open In App

How to suppress the use of scientific notations for small numbers using NumPy?

Sometimes we have elements that are in scientific notations, and we have to suppress the scientific notation for simplicity. For this purpose, we call a function named as numpy.set_printoptions(). This function will help to suppress the scientific notation and display number to a certain precision. In this article we will see How to suppress the use of scientific notations for small numbers using NumPy in Python.

Syntax: numpy.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, formatter=None, sign=None, floatmode=None, *, legacy=None)



Parameters:

  • precision: Number of digits of precision for floating point output (default 8)
  • suppress: If True, always print floating point numbers using fixed point notation, if False, then scientific notation is used when absolute value of the smallest number is < 1e-4

Rest of the parameters are optional.



 Suppress the use of scientific notations using Numpy in Python

Example 1:

Here we suppress the scientific notations for the elements of 1-D NumPy array with precision 2.




# Importing Numpy library
import numpy as np
 
# Creating a 1-D Numpy array
num = np.array([1.8e-10, 1.586, 150.45, 0.2855])
 
# Suppressing 1-D numpy array with precision 2
# using numpy.set_printoptions()
print("Numpy array values with precision 2:\n")
np.set_printoptions(precision = 2, suppress = True)
print(num)

Output:

Numpy array values with precision 2:
[  0.     1.59 150.45   0.29]

Example 2:

Here we suppress the scientific notations for the elements of 2-D NumPy array with precision 3.




# Importing Numpy library
import numpy as np
 
# Creating a 2-D Numpy array
num = np.array([[3.1415, 2.7182],
                 [6.6260e-34, 6.6743e-11]])
 
# Suppressing 2-D numpy array with precision 3
# using numpy.set_printoptions()
print("Numpy array values with precision 3:\n")
np.set_printoptions(precision = 3, suppress = True)
print(num)

Output:

Numpy array values with precision 3:
[[3.142 2.718]
[0.    0.   ]]

Example 3:

Here we suppress the scientific notations for the elements of 3-D NumPy array with precision 4.




# Importing Numpy library
import numpy as np
 
# Creating a 3-D Numpy array
num = np.array([[[3.141527, 2.718283],
                 [6.6268574, 6.6743e-11]],
                [[34.8454, 8.6260e-34],
                 [7, 8]]])
 
# Suppressing 3-D numpy array with precision 4
# using numpy.set_printoptions()
print("Numpy array values with precision 4:\n")
np.set_printoptions(precision = 4, suppress = True)
print(num)

Output:

Numpy array values with precision 4:
[[[ 3.1415  2.7183]
 [ 6.6269  0.    ]]
[[34.8454  0.    ]
 [ 7.      8.    ]]]

Example 4:

Here we have used the formatter parameter for the set_printoptions() function.




import numpy as np
 
np.set_printoptions(suppress=True,
   formatter={'float_kind':'{:0.2f}'.format})
 
a = np.array([1.8e-10, 1.586, 150.45, 0.2855])  
 
print(a)

Output:

[0.00 1.59 150.45 0.29]

Article Tags :