10-20-2022, 11:44 AM
How to Print a NumPy Array Without Scientific Notation in Python
Rate this post
[b]Problem Formulation[/b]
» 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:
Output:
Expected Output: Print the given array without scientific notation in Python as:
Without further ado, let’s dive into the different ways of solving the given problem.
☛[b]Method 1: Using set_printoptions() Function[/b]
The 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 function by passing the suppress as to remove the scientific notation of the numpy array.
Approach:
Code:
Output:
Discussion: The function only works for the numbers that fit in the default 8-character space allotted to it, as shown below:
Code:
Output:
When we pass a number that is greater than 8 characters wide, exponential notation is imposed as shown below:
Code:
Output:
☛[b]Method 2: Using set_printoptions() Function with .format[/b]
As in method 1, the function does not work when the number has more than eight characters. That is when 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:
Output:
We can also format the output to only have 2 units precision by using as shown below:
Code:
Output:
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.
☛[b]Method 3: Using printoptions() Function[/b]
The 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 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:
Code:
Output:
☛[b]Method 4: Using array2string() Function[/b]
The 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 argument as . When the argument is , it represents the numbers close to zero as zero.
Approach:
Code:
Output:
[b]Conclusion[/b]
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.)
Rate this post
[b]Problem Formulation[/b]
» 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:
Code:
arr = np.array([1, 5, 10, 20, 35, 5000.5])
print(arr)Output:
Code:
[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:
Code:
[ 1. 5. 10. 20. 35. 5000.5]Without further ado, let’s dive into the different ways of solving the given problem.
☛[b]Method 1: Using set_printoptions() Function[/b]
The
Code:
set_printoptions()Code:
set_printoptions()Code:
TrueApproach:
- Import the Numpy module to create the array.
- Use the function and pass the suppress value as True.Code:
set_printoptions()
- Print the array; it will get displayed without the scientific notation.
Code:
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:
Code:
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
Code:
set_printoptions()Code:
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:
Code:
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:
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:
Code:
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]As in method 1, the
Code:
set_printoptions()Code:
set_printoptions(formatter)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:
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:
Code:
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
Code:
'{:0.2f}' .formatCode:
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:
Code:
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.
☛[b]Method 3: Using printoptions() Function[/b]
The
Code:
printoption()Code:
printoptions()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:
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:
Code:
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]The
Code:
array2string()Code:
suppress_smallCode:
TrueCode:
suppress_smallCode:
TrueApproach:
- Import the numpy module to create the array.
- Use the function and pass theCode:
array2string()argument asCode:suppress_small.Code:True
- Finally, print the array. It will get displayed without the scientific notation.
Code:
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:
Code:
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]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.)

