How to Print a Full NumPy Array Without Truncation | Towards Data Science

When printing out NumPy arrays, the output may be truncated by default due to the large number of elements. While in most of the cases this isn’t a problem, you may sometimes have to print out the array in full in order to be able to inspect the full content.
In today’s short guide we will explore a couple of different ways for printing NumPy arrays to the standard output without any sort of truncation. Specifically, we will showcase how to do so
using set_printoptions method
using printoptions context manager
by converting the NumPy array into a list of lists
First, let’s create an example NumPy array that we’ll reference throughout this short article in order to demonstrate a few concepts.
import numpy as np# Create a dummy NumPy array consisting of 250 rows and 40 columnsmy_arr = np.arange(10000).reshape(250, 40)print(my_arr)array([[   0,    1,    2, ...,   37,   38,   39],       [  40,   41,   42, ...,   77,   78,   79],       [  80,   81,   82, ...,  117,  118,  119],       ...,       [9880, 9881, 9882, ..., 9917, 9918, 9919],       [9920, 9921, 9922, ..., 9957, 9958, 9959],       [9960, 9961, 9962, ..., 9997, 9998, 9999]])
As you can see above, both the columns as well as the rows of the numpy array are truncated when printed out to standard output.
numpy.set_printoptions is a method used to configure display options such as the way arrays, floating point numbers and other numpy objects are displayed to the standard output.
One of the parameters accepted in set_printoptions is threshold that corresponds to the total number of array elements that trigger summarisation over full representation. By default this value is set to 1000. In order to trigger the full representation without summarisation, you should set threshold to sys.maxsize.
import sysimport numpy as npnp.set_printoptions(threshold=sys.maxsize)
Additionally, if you also want to tweak the precision for floating point output by specifying the precision argument when calling set_printoptions().
In many occasions, you may want to fully print only one or just a few numpy arrays and then use the default options. In this case, a Context Manager is a quite useful construct that can help you do exactly this.
NumPy comes with a Context Manager called np.printoptions that helps you set print options within the scope of the with block and then restored the old options at the end.
import sysimport numpy as npwith np.printoptions(threshold=sys.maxsize):    print(my_arr)
Another alternative to the previous approaches is to actually convert the NumPy array into a list using numpy.ndarray.tolist() method.
import numpy as npprint(my_arr.tolist())
In today’s short guide we explore a few different ways for printing out NumPy arrays in full. By default, arrays printed in standard output might be truncated (depending on their size/elements).
In many cases, you may want to view all the elements in full and thus the options introduced in this article could be a good reference that will help you do so.
Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.
gmyrianthous.medium.com
You may also like