Print Full Numpy Array without Truncation


Numpy is a powerful Python library that serves to handle large, multi-dimensional arrays. However, when printing large numpy arrays, the interpreter often truncates the output to save space and shows only a few elements of that array. In this article, we will show how to print a full Numpy array without truncation.

To understand the problem statement properly, consider the below example:

Input

aray = np.arange(1100)

Output

[   0    1    2 ... 1097 1098 1099]

In the above example, we have created an array with 1100 elements. When we print it the Python interpreter will automatically truncate it to show only a small piece of output as represented above. It uses triple dots (…) to specify that some elements are not displayed.

Python Program to print full Numpy Array without Truncation

To print all the elements of the numpy array without truncation, we can use the below mentioned methods:

  • set_printoptions()

  • array2string()

Using set_printoptions()

The set_printoptions() method of NumPy allows us to modify the way arrays are displayed when printed. To print the whole array, we will set the 'threshold' parameter to 'np.inf'.

Example 1

The following example illustrates how to use set_printoptions() to print the whole numpy array without truncation.

Approach

  • The first step is to import numpy package with reference name 'np'.

  • Next, we create a numpy array using the random.rand() method to generate a random array of size 5x3 filled with values between 0 and 1.

  • Now, use the set_printoptions() to display entire array by setting the threshold parameter to np.inf. Here, np.inf indicates that there is no threshold.

  • Finally, we use the built-in method print() to show the result.

import numpy as np
# generating a random NumPy array
aray = np.random.rand(5, 3)
# Setting the print options 
np.set_printoptions(threshold = np.inf)
# to print the generated array 
print(aray)

Output

[[0.20389255 0.29142771 0.26634027]
 [0.95128389 0.11253549 0.55029953]
 [0.42578651 0.26762357 0.0058134 ]
 [0.47427662 0.080491   0.98576308]
 [0.29514599 0.44207715 0.51177261]]

Example 2

In this example, instead of generating a random array, we will use the built-in method arange() of numpy to create an array that contains 40 consecutive values starting from 0. The arange() method returns a numpy array.

import numpy as np
# creating a NumPy array with 40 values
aray = np.arange(40)
# Setting the print options here
np.set_printoptions(threshold = np.inf)
# to display the result
print(aray)

Output

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]

Example 3

This is another example that shows how to print full numpy array without truncation. In this particular example, we will create a numpy array using the random.rand() method as we did in the first example. Using set_printoptions() by setting the threshold to 'sys.maxsize', we will display the whole array. Here, the sys.maxsize attribute provides the maximum value that can be represented as an integer in the current system.

# importing the required packages
import numpy as np
import sys
# generating a random NumPy array
aray = np.random.rand(5, 3)
# Setting the print options 
np.set_printoptions(threshold = sys.maxsize)
# to display the result
print(aray)

Output

[[0.83641797 0.63958049 0.22548871]
 [0.07813775 0.75781466 0.0283849 ]
 [0.79322127 0.2648928  0.19495721]
 [0.44913602 0.17464489 0.67625814]
 [0.55415935 0.40091674 0.1828985 ]]

Using array2string()

The array2string() method of NumPy converts a given array into a string representation. Again, To print the whole array, we will pass the given array as a parameter and set the additional parameter named 'threshold' to 'np.inf'. The 'np.inf' indicates that there is no threshold for truncation.

Example

In the following example, we will create a random numpy array of size 5x5 filled with values between 0 and 1. To print the entire array without truncation, we will use the np.array2string() method by passing the numpy array and setting the threshold parameter to np.inf.

import numpy as np
# generating a random NumPy array
aray = np.random.rand(5, 5)
# Converting the array to a string 
arr_st = np.array2string(aray, threshold = np.inf)
# to print the converted array string
print(arr_st)

Output

[[0.20558096 0.63450548 0.84019308 0.80807701 0.50960877]
 [0.30689032 0.54547474 0.23338944 0.67067941 0.81017394]
 [0.69467855 0.57111774 0.98147844 0.4711527  0.85500914]
 [0.83951451 0.83768907 0.027058   0.06053751 0.57541982]
 [0.45509108 0.6337008  0.65374078 0.38031754 0.0921497 ]]

Conclusion

In this article, we have learned two approaches to print a full Numpy array without truncation using example programs. These two approaches are array2string() and set_printoptions() methods. They take a parameter named 'threshold' that can be set to 'np.inf' or 'sys.maxsize' to print all the elements of the numpy array without truncation.

Updated on: 21-Jul-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements