Home >Backend Development >Python Tutorial >How Can I Print a Full NumPy Array Without Truncation?

How Can I Print a Full NumPy Array Without Truncation?

Barbara Streisand
Barbara StreisandOriginal
2024-12-07 15:32:16366browse

How Can I Print a Full NumPy Array Without Truncation?

Printing NumPy Arrays in Full Without Truncation

When working with NumPy arrays, it's common to encounter a problem where large arrays are truncated when printed. This can make it difficult to inspect the full contents of the array. Fortunately, there is a simple solution that allows you to print the full array without any truncation.

To print the full NumPy array, you can use the numpy.set_printoptions function. This function takes several arguments, including the threshold argument, which specifies the maximum number of elements to be printed before truncation occurs. By setting the threshold argument to sys.maxsize, the function will print the entire array without any truncation.

Here's an example:

import sys
import numpy

my_array = numpy.arange(10000)
numpy.set_printoptions(threshold=sys.maxsize)
print(my_array)

This will print the full array without any truncation:

[ 0  1  2 ... 9997 9998 9999]

The above solution can also be applied to multidimensional arrays. For instance, to print a 250x40 array without truncation:

my_array = numpy.arange(10000).reshape(250, 40)
numpy.set_printoptions(threshold=sys.maxsize)
print(my_array)

This will print the full array without any truncation.

The above is the detailed content of How Can I Print a Full NumPy Array Without Truncation?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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