Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to suppress the use of scientific notations for small numbers using NumPy?
When working with NumPy arrays, small numbers are often displayed in scientific notation (like 1e-10). While compact, this format can be difficult to read and compare. This guide covers four methods to suppress scientific notation for small numbers in NumPy arrays: using numpy.vectorize with string formatting, numpy.ndarray.round, string formatting with list comprehension, and numpy.set_printoptions.
Method 1: Using numpy.vectorize with String Formatting
The numpy.vectorize function combined with string formatting can suppress scientific notation by converting array elements to formatted strings ?
Syntax
formatted_array = numpy.vectorize('{:.Nf}'.format)(array)
Here, N denotes the decimal places to retain, and '{:.Nf}' represents the string formatting syntax for floating-point numbers with N decimal places.
Example
import numpy as np
array = np.array([1e-10, 2e-10, 3e-10])
formatted_array = np.vectorize('{:.10f}'.format)(array)
print(formatted_array)
['0.0000000001' '0.0000000002' '0.0000000003']
Advantages: Provides specific formatting control for each element and is flexible for different formatting requirements.
Disadvantages: Returns an array of strings, making it unsuitable for further numerical operations.
Method 2: Using numpy.ndarray.round
The round() method rounds array elements to a specified number of decimal places, effectively suppressing scientific notation while keeping numerical values ?
Syntax
rounded_array = array.round(N)
Here, N represents the decimal places to retain.
Example
import numpy as np array = np.array([1e-10, 2e-10, 3e-10]) rounded_array = array.round(10) print(rounded_array)
[0.0000000001 0.0000000002 0.0000000003]
Advantages: Maintains output as a NumPy array with numerical values, suitable for further numerical operations.
Disadvantages: Limited formatting options compared to string formatting methods.
Method 3: Using String Formatting with List Comprehension
String formatting with list comprehension provides another way to format each element, similar to numpy.vectorize but using Python's built-in list comprehension ?
Syntax
formatted_array = ['{:.Nf}'.format(x) for x in array]
Example
import numpy as np
array = np.array([1e-10, 2e-10, 3e-10])
formatted_array = ['{:.10f}'.format(x) for x in array]
print(formatted_array)
['0.0000000001', '0.0000000002', '0.0000000003']
Advantages: Provides specific formatting control and is adaptable to different formatting requirements.
Disadvantages: Returns a list of strings, unsuitable for numerical operations.
Method 4: Using numpy.set_printoptions
The numpy.set_printoptions() function sets global printing options for all NumPy arrays, including suppressing scientific notation ?
Syntax
np.set_printoptions(suppress=True, precision=N)
Here, suppress=True disables scientific notation, and precision=N sets the decimal places to display.
Example
import numpy as np np.set_printoptions(suppress=True, precision=10) array = np.array([1e-10, 2e-10, 3e-10]) print(array)
[0.0000000001 0.0000000002 0.0000000003]
Advantages: Changes default printing behavior globally and maintains numerical values suitable for operations.
Disadvantages: Affects all NumPy arrays in your code, which may not be desired in all cases.
Comparison
| Method | Output Type | Numerical Operations | Scope |
|---|---|---|---|
numpy.vectorize |
String array | No | Specific array |
round() |
Numerical array | Yes | Specific array |
| List comprehension | String list | No | Specific array |
set_printoptions |
Numerical array | Yes | Global |
Conclusion
Use numpy.set_printoptions() for global suppression of scientific notation, round() for maintaining numerical values, or string formatting methods when you need specific display formatting. Choose based on whether you need numerical operations and the desired scope of the formatting changes.
