Home > Backend Development > Python Tutorial > How to Prevent NumPy Array Truncation When Printing?

How to Prevent NumPy Array Truncation When Printing?

DDD
Release: 2024-12-11 02:49:13
Original
419 people have browsed it

How to Prevent NumPy Array Truncation When Printing?

How to Display Full NumPy Array Without Truncation

When printing NumPy arrays, you often encounter truncated representations. To display the entire array, utilize the numpy.set_printoptions function.

import sys
import numpy

# Set threshold to maximum size to disable truncation
numpy.set_printoptions(threshold=sys.maxsize)
Copy after login

Consider the following example:

>>> numpy.arange(10000)
array([   0,    1,    2, ..., 9997, 9998, 9999])

>>> numpy.arange(10000).reshape(250, 40)
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]])
Copy after login

Now, using numpy.set_printoptions, we can print the full array without truncation:

numpy.set_printoptions(threshold=sys.maxsize)

print(numpy.arange(10000))
print(numpy.arange(10000).reshape(250, 40))
Copy after login

Output:

[0 1 2 ... 9997 9998 9999]
[[  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]]
Copy after login

The above is the detailed content of How to Prevent NumPy Array Truncation When Printing?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template