It does not matter whether you are debugging your code, inspecting your large data, or running a small-scale simulation; you have to print an array without truncation in a shell or Python interpreter to verify the correctness of computations by examining all elements of an array.
With truncation, your numpy array looks like this:
Without truncation, your numpy array looks like this:
Now that you understand the difference let’s check out how to do it.
You can print a numpy array without truncation numpy.set_printoptions() function and pass either sys.maxsize or np.inf as an argument to that function.
Method 1: Using numpy.set_printoptions() with sys.maxsize
The numpy.set_printoptions() function sets “printing options” for the array. These options decide how numpy objects are displayed on the console or screen.
We won’t go into every argument of the set_printoptions() method, but one argument called “threshold” is very important.
By default, NumPy truncates large arrays to avoid overwhelming the output.
We will use the full repr without summarization, so we will pass sys.maxsize as an argument to display each element of the array in the console. Basically, we are setting the threshold to the maximum representable integer, disabling truncation.
import numpy as np import sys # Printing all values of array without truncation np.set_printoptions(threshold=sys.maxsize) # Creating a 1D array with 121 values arr = np.arange(121) print(arr)
Output
You can see that each element of the array has been delayed.
Method 2: Using numpy.set_printoptions() with numpy.inf
The numpy.inf means infinity. We are setting an infinity as a threshold, which means the same thing.
import numpy as np # Printing all values of array without truncation # By setting threshold to np.inf np.set_printoptions(threshold=np.inf) # Creating a 1D array with 121 values arr = np.arange(90) print(arr)
Output
Why we should not print large arrays in the console?
Printing extremely large arrays to the console makes it very difficult to read, and it can cause performance issues by consuming more memory and hanging your system.
The better way to print out a large array is to summarize the data meaningfully or use more specialized tools. Only if there is an absolute requirement for this type of operation should you do it.