How to pretty print a NumPy array by suppressing the scientific notation (like 1e10)

Updated: January 23, 2024 By: Guest Contributor Post a comment

Introduction to NumPy Array Formatting

Dealing with NumPy arrays often involves handling very large or very small numbers. By default, NumPy prints these numbers using scientific notation because it is more concise. However, there are instances when you might prefer to have a more human-readable format, especially when presenting data to an audience not familiar with scientific notation. In this tutorial, we will explore how to pretty print a NumPy array by suppressing the scientific notation.

Before diving into suppressing the scientific notation, it’s crucial to understand the basics of NumPy array formatting. NumPy provides a set of options to set the print options of arrays, which you can tailor to your needs. The most common options include:

  • threshold: to control the number of elements printed
  • edgeitems: to specify the number of items in each dimension to print on the edges
  • linewidth: to set the maximum number of characters per line in the output
  • suppress: to suppress the use of scientific notation for small numbers

Basic Pretty Printing

The most straightforward way to suppress scientific notation in NumPy is to use the set_printoptions function with the suppress parameter. Here’s how you can do this:

import numpy as np

# Create a NumPy array with large values
test_array = np.array([1e10, 1.5e10, 2.5e10])

# Suppress scientific notation
np.set_printoptions(suppress=True)

# Print the array
test_array

When you run this code, the output will be a NumPy array printed without scientific notation:

[10000000000.0, 15000000000.0, 25000000000.0]

Setting Precision for Floating Point Numbers

Besides suppressing scientific notation, you might want to set a specific precision for floating point numbers to make them even more readable. This is done by using the precision parameter:

import numpy as np

# Set precision and suppress scientific notation
np.set_printoptions(precision=2, suppress=True)

# Print an array with floating point numbers
np.array([3.14159265, 2.71828182, 1.41421356])

Output will look much cleaner and rounded:

[3.14, 2.72, 1.41]

Mixed Format Arrays

What if you’re handling an array with both very large and very small numbers? In this case, suppressing scientific notation globally might not be the best idea, as it could make the small numbers unreadable due to a loss of significant digits. Instead, you could selectively format only the large numbers using formatting string options.

import numpy as np

# Set display options globally
np.set_printoptions(suppress=False)

# An array with mixed formats
test_array = np.array([1e10, 3.1415, 1e-5])

# Selectively suppress scientific notation by converting to strings
formatted_array = [f'{x:.2f}' if x > 1e5 else str(x) for x in test_array]

print(formatted_array)

This approach will show large numbers with the desired precision and keep small numbers in scientific notation:

['10000000000.00', '3.1415', '1e-05']

Using Context Managers for Local Settings

In practice, you might want to use pretty printing in a localized section of code without affecting the global NumPy print settings. Python’s context managers can come in handy for this purpose:

import numpy as np

array = np.array([1.2345e-6, 2.3456e-10, 1.5678e5])

# Using context manager to suppress within a local scope
with np.printoptions(precision=4, suppress=True):
    print(array)

The array will print according to local settings within the ‘with’ block, without altering the global settings permanently.

[1.2345  0.0000 156780.0000]

Conclusion

In this tutorial, we have explored different ways to pretty print NumPy arrays by suppressing scientific notation. By using np.set_printoptions and understanding its parameters like suppress and precision, you now have tools to make your NumPy data more readable and presentation-friendly. Whether you are working on data analysis, scientific research, or machine learning, these techniques can significantly improve the accessibility of your numerical results.