Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Print a NumPy Array Without Truncation in Python

  • 24 Dec, 2024
  • Com 0
Printing a NumPy Array Without Truncation in Python

It does not matter whether you are debugging your code, inspecting your extensive data, or running a small-scale simulation; you have to print an array without truncation in a shell or Python interpreter to verify the correctness of computations by examining all elements of an array.

With truncation, your numpy array looks like this:

Truncated numpy array

Without truncation, your numpy array looks like this:

Numpy array without trucated elements

Now that you understand the difference, let’s check out how to do it.

You can print a numpy array without truncation numpy.set_printoptions() function and pass either sys.maxsize or np.inf as an argument to that function.

Method 1: Using numpy.set_printoptions() with sys.maxsize

The numpy.set_printoptions() function sets “printing options” for the array. These options decide how numpy objects are displayed on the console or screen.

We won’t go into every argument of the set_printoptions() method, but one argument, called “threshold,” is fundamental.

By default, NumPy truncates large arrays to avoid overwhelming the output.

We will use the full repr without summarization, so we will pass sys.maxsize as an argument to display each element of the array in the console.

Essentially, we set the threshold to the maximum representable integer, thereby disabling truncation.

import numpy as np
import sys

# Printing all values of array without truncation
np.set_printoptions(threshold=sys.maxsize)

# Creating a 1D array with 121 values
arr = np.arange(121)

print(arr)

Output

Output of using np.printoptions with sys.maxsize

You can see that each element of the array has been delayed.

Method 2: Using numpy.set_printoptions() with numpy.inf

The numpy.inf means infinity. We are setting infinity as a threshold, which means the same thing.

import numpy as np

# Printing all values of array without truncation
# By setting threshold to np.inf
np.set_printoptions(threshold=np.inf)

# Creating a 1D array with 121 values
arr = np.arange(90)

print(arr)

Output

Output of np.inf as threshold

Why shouldn’t we print large arrays to the console?

Printing extremely large arrays to the console can be very difficult to read, and it may cause performance issues by consuming excessive memory and potentially causing your system to hang.

A more effective way to print out a large array is to summarize the data meaningfully or utilize more specialized tools. Only if there is an absolute requirement for this type of operation should you do it.

Post Views: 53
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert Numpy dtypes to Native Python Types
Printing Numpy Objects Without Line Breaks in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend