Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Print Full Numpy Array without Truncation
NumPy is a powerful Python library for handling 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 with ellipsis (...). In this article, we will explore how to print a full NumPy array without truncation.
Understanding the Problem
To understand the truncation issue, consider this example:
import numpy as np # Create a large array with 1100 elements array = np.arange(1100) print(array)
[ 0 1 2 ... 1097 1098 1099]
In the above example, we created an array with 1100 elements. The Python interpreter automatically truncates it and uses triple dots (...) to indicate that some elements are not displayed.
Method 1: Using set_printoptions()
The set_printoptions() method allows us to modify how arrays are displayed when printed. To print the whole array, we set the threshold parameter to np.inf.
Example 1: Random Array
import numpy as np # Generate a random NumPy array array = np.random.rand(5, 3) # Set print options to display entire array np.set_printoptions(threshold=np.inf) # Print the generated array print(array)
[[0.37454012 0.95071431 0.73199394] [0.59865848 0.15601864 0.15599452] [0.05808361 0.86617615 0.60111501] [0.70807258 0.02058449 0.96990985] [0.83244264 0.21233911 0.18182497]]
Example 2: Large Sequential Array
import numpy as np # Create a NumPy array with 40 consecutive values array = np.arange(40) # Set print options to display entire array np.set_printoptions(threshold=np.inf) # Display the result print(array)
[ 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: Using sys.maxsize
import numpy as np import sys # Generate a random NumPy array array = np.random.rand(5, 3) # Set print options using sys.maxsize np.set_printoptions(threshold=sys.maxsize) # Display the result print(array)
[[0.77395605 0.43887844 0.85859792] [0.69736803 0.09417735 0.97562235] [0.7611397 0.78606431 0.12811363] [0.45038594 0.37079802 0.92676499] [0.64386512 0.82276161 0.4434142 ]]
Method 2: Using array2string()
The array2string() method converts an array into a string representation. To print the whole array, we pass the array as a parameter and set the threshold parameter to np.inf.
Example
import numpy as np # Generate a random NumPy array array = np.random.rand(5, 5) # Convert the array to a string without truncation array_string = np.array2string(array, threshold=np.inf) # Print the converted array string print(array_string)
[[0.891773 0.96366276 0.38344152 0.79172504 0.52889492] [0.56804456 0.92559664 0.07103606 0.0871293 0.0202184 ] [0.83261985 0.77815675 0.87001215 0.97861834 0.79915856] [0.46147936 0.78052918 0.11827443 0.63992102 0.14335329] [0.94466892 0.52184832 0.41466194 0.26455561 0.77423369]]
Comparison
| Method | Usage | Best For |
|---|---|---|
set_printoptions() |
Global setting for all arrays | Permanent configuration |
array2string() |
Per-array string conversion | One-time printing without global changes |
Resetting Print Options
To reset print options back to default after using set_printoptions():
import numpy as np # Reset to default print options np.set_printoptions(threshold=1000) # Default threshold
Conclusion
Use set_printoptions(threshold=np.inf) for global array printing configuration or array2string(threshold=np.inf) for individual array string conversion. Both methods effectively prevent NumPy array truncation and display complete arrays regardless of size.
