Problem Formulation
Β» Problem Statement: Given a NumPy array. How to print the NumPy array without scientific notation in Python?
Note: Python represents very small or very huge floating-point numbers in their scientific form. Scientific notation represents the number in terms of powers of 10 to display very large or very small numbers. For example, the scientific notation for the number 0.000000321 is described as 3.21e07.
In Python, the NumPy module generally uses scientific notation instead of the actual number while printing/displaying the array items.
Example: Look at the following code snippet:
arr = np.array([1, 5, 10, 20, 35, 5000.5]) print(arr)
Output:
[1.0000e+00 5.0000e+00 1.0000e+01 2.0000e+01 3.5000e+01 5.0005e+03]
Expected Output: Print the given array without scientific notation in Python as:
[ 1. 5. 10. 20. 35. 5000.5]
Without further ado, let’s dive into the different ways of solving the given problem.
βMethod 1: Using set_printoptions() Function
The set_printoptions()
is a function in the numpy module that is used to set how the floating-point numbers, NumPy arrays and numpy objects are to be displayed. By default, the very big or very small numbers of the array are represented using scientific notation. We can use the set_printoptions()
function by passing the suppress as True
to remove the scientific notation of the numpy array.
Approach:
- Import the Numpy module to create the array.
- Use the
set_printoptions()
function and pass the suppress value as True. - Print the array; it will get displayed without the scientific notation.
Code:
# Importing the numpy module import numpy as np # Creating a NumPy array a = np.array([1, 5, 10, 20, 35, 5000.5]) print("Numpy array with scientific notation", a) np.set_printoptions(suppress = True) print("Numpy array without scientific notation", a)
Output:
Numpy array with scientific notation [1.0000e+00 5.0000e+00 1.0000e+01 2.0000e+01 3.5000e+01 5.0005e+03] Numpy array without scientific notation [ 1. 5. 10. 20. 35. 5000.5]
Discussion: The set_printoptions()
function only works for the numbers that fit in the default 8-character space allotted to it, as shown below:
Code:
import numpy as np # Array with element index 1 having 8 digits a = np.array([5.05e-5, 15.6, 2.1445678e5]) print("Numpy array with scientific notation", a) np.set_printoptions(suppress = True) print("Numpy array without scientific notation", a)
Output:
Numpy array with scientific notation [5.0500000e-05 1.5600000e+01 2.1445678e+05] Numpy array without scientific notation [ 0.0000505 15.6 214456.78 ]
When we pass a number that is greater than 8 characters wide, exponential notation is imposed as shown below:
Code:
import numpy as np # Array with element index 1 having more than 8 digits a = np.array([5.05e-5, 15.6, 2.1445678e10]) print("Numpy array with scientific notation", a) np.set_printoptions(suppress = True) print("Numpy array without scientific notation", a)
Output:
Numpy array with scientific notation [5.0500000e05 1.5600000e+01 2.1445678e+10] Numpy array without scientific notation [5.0500000e05 1.5600000e+01 2.1445678e+10]
βMethod 2: Using set_printoptions() Function with .format
As in method 1, the set_printoptions()
function does not work when the number has more than eight characters. That is when set_printoptions(formatter)
is used to specify the options for printing and rounding. We have to set the function to print the float variable.
Python’s built-in format(value, spec) function transforms the input of one format into the output of another format defined by you. Specifically, it applies the format specifier spec to the argument value and returns a formatted representation of value. Read more about the “Python format() Function.”
Code:
import numpy as np # Creating a NumPy array # Array with element index 1 having more than 8 digits a = np.array([5.05e-5, 15.6, 2.1445678e10]) print("Numpy array with scientific notation", a) np.set_printoptions(suppress = True, formatter = {'float_kind':'{:f}'.format}) print("Numpy array without scientific notation", a)
Output:
Numpy array with scientific notation [5.0500000e-05 1.5600000e+01 2.1445678e+10] Numpy array without scientific notation [0.000051 15.600000 21445678000.000000]
We can also format the output to only have 2 units precision by using '{:0.2f}' .format
as shown below:
Code:
import numpy as np # Array with element index 1 having more than 8 digits a = np.array([5.05e-5, 15.6, 2.1445678e10]) print("Numpy array with scientific notation", a) np.set_printoptions(suppress = True, formatter = {'float_kind':'{:0.2f}'.format}) print("Numpy array without scientific notation", a)
Output:
Numpy array with scientific notation [5.0500000e-05 1.5600000e+01 2.1445678e+10] Numpy array without scientific notation [0.00 15.60 21445678000.00]
Discussion: The disadvantage of using this method to suppress the exponential notion in the numpy arrays is when the array gets a very large float value. When we try to print this array, we are going to get a whole page of numbers.
βMethod 3: Using printoptions() Function
The printoption()
function is a function in the Numpy module used as a context manager for setting print options. By passing the precision as 3 and suppress as True in the printoptions()
function, we can remove the scientific notation and print the Numpy array.
Note: This function only works if you use NumPy versions 1.15.0 or later.
Approach:
- Import the numpy module to create the array.
- Use the printoption() function inside the “with” and pass the precision value as 3 and the suppress value as True.
- Print the array; it will get displayed without the scientific notation.
Code:
import numpy as np # Creating a NumPy array a = np.array([1, 5, 10, 20, 35, 5000.5]) print("Numpy array with scientific notation", a) print("Numpy array without scientific notation:") with np.printoptions(precision = 3, suppress = True): print(a)
Output:
Numpy array with scientific notation [1.0000e+00 5.0000e+00 1.0000e+01 2.0000e+01 3.5000e+01 5.0005e+03] Numpy array without scientific notation: [ 1. 5. 10. 20. 35. 5000.5]
βMethod 4: Using array2string() Function
The array2string()
is a function in the numpy module that returns a string representation of an array. We can use this function to print a NumPy array without scientific notation by passing the array as the argument and setting the suppress_small
argument as True
. When the suppress_small
argument is True
, it represents the numbers close to zero as zero.
Approach:
- Import the numpy module to create the array.
- Use the
array2string()
function and pass thesuppress_small
argument asTrue
. - Finally, print the array. It will get displayed without the scientific notation.
Code:
import numpy as np # Creating a NumPy array a = np.array([1, 5, 10, 20, 35, 5000.5]) print("Numpy array with scientific notation", a) a = np.array2string(a, suppress_small = True) print("Numpy array without scientific notation:", a)
Output:
Numpy array with scientific notation [1.0000e+00 5.0000e+00 1.0000e+01 2.0000e+01 3.5000e+01 5.0005e+03] Numpy array without scientific notation: [ 1. 5. 10. 20. 35. 5000.5]
Conclusion
Hurrah! We have successfully solved the mission-critical question in numerous ways in this article. I hope you found it helpful. Please stay tuned and subscribe for more such interesting articles.
πInteresting Read: How to Suppress Scientific Notation in Python?
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)