Friday, December 27, 2024
Google search engine
HomeLanguagesPrint full Numpy array without truncation

Print full Numpy array without truncation

When printing a big NumPy array in the shell, the Python interpreter automatically shows only a small, truncated piece of the array, indicating that some values are missing in the textual representation of the array with the triple dots ‘…’. To overcome this we use some methods that we will discuss below:

Print the full NumPy array without truncation

Let us print numbers from 0 to 1000 by using simple NumPy functions.

Python3




import numpy as np
arr = np.arange(1001)
print(arr)


Output:

[   0    1    2 ...  998  999 1000]

Whenever we have to print a large range of numbers then the output will be truncated and the result will be displayed within one line. But what to do if we don’t want truncation of output?

Print the full NumPy array without truncation using numpy.set_printoptions()

In NumPy, it is possible to remove truncation and display results as it is. We use np.set_printoptions() function having attribute threshold=np.inf or threshold=sys.maxsize.

Syntax: numpy.set_printoptions(threshold=None, edgeitems=None, linewidth=None, suppress=None)

Using threshold = sys.maxsize

Printing first 1000 numbers without truncation using np.printoptions(threshold=sys.maxsize). Here, we set the threshold to sys.maxsize which represents the maximum values that python can handle.

Python3




# Importing Numpy and sys modules
import numpy as np
import sys
 
# Creating a 1-D array with 100 values
arr = np.arange(101)
 
# Printing all values of array without truncation
np.set_printoptions(threshold=sys.maxsize)
print(arr)


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  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100]

Using threshold = np.inf

Printing first 1200 numbers without truncation using np.printoptions(threshold = np.inf). Here, we set the threshold to np.inf which is the floating-point representation of infinity.

Python3




# Importing Numpy and sys modules
import numpy as np
import sys
 
# Creating a 1-D array with 50 values
arr = np.arange(51)
 
# Printing all values of array without truncation
np.set_printoptions(threshold=np.inf)
print(arr)


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 40 41 42 43 44 45 46 47 48 49 50]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments