Wrap your print() call in NumPy’s with np.printoptions(threshold=np.inf): context manager (or call np.set_printoptions(threshold=np.inf) globally) so NumPy prints every element instead of inserting once an array exceeds the default 1,000-element threshold, optionally adding linewidth=np.inf to prevent line wrapping across wide rows.
Basic Example
import numpy as np
arr = np.arange(1_500).reshape(30, 50)
# Default behavior (truncates with '...' because size 1500 > default threshold 1000):
# print(arr)
# 1. Recommended: Temporary context manager (restores original print settings automatically)
with np.printoptions(threshold=np.inf, linewidth=np.inf):
print(arr)
# 2. Alternative: Set globally for the entire script / notebook session
np.set_printoptions(threshold=np.inf, linewidth=np.inf)
print(arr)
# Restore NumPy defaults later if needed:
np.set_printoptions(edgeitems=3, infstr="inf", linewidth=75, nanstr="nan",
precision=8, suppress=False, threshold=1000, formatter=None)
Key Details & Edge Cases
- Why
np.inf(orsys.maxsize) triggers full output:- NumPy’s default
thresholdis1000(arr.size > 1000summarizes the array usingedgeitems=3elements at each corner separated by). Settingthreshold=np.inf(orthreshold=sys.maxsize) guaranteesarr.size > thresholdis alwaysFalse, disabling summary mode for arrays of any dimension.
- NumPy’s default
- Line wrapping vs. ellipsis truncation (
linewidthvs.threshold):- Setting
threshold=np.infremovestruncation, but NumPy still wraps rows wider than75characters onto multiple lines unless you also passlinewidth=np.inf(or a wider integer such aslinewidth=200).
- Setting
- Prefer
with np.printoptions(...)over globalnp.set_printoptions(...):- Mutating global print options in a library or Jupyter notebook affects every subsequent
print()call and can freeze your terminal or browser tab if a multi-million-element array is printed accidentally. Thewith np.printoptions(...):context manager scopes full printing strictly to the block insidewith.
- Mutating global print options in a library or Jupyter notebook affects every subsequent
- Clean formatting for floating-point arrays (
precisionandsuppress=True):- When inspecting large float arrays without truncation, combine
threshold=np.infwithprecision=4, suppress=True(with np.printoptions(threshold=np.inf, precision=4, suppress=True):) so small numbers render as0.0001instead of scientific notation (1.00000000e-04), keeping columns compact and aligned.
- When inspecting large float arrays without truncation, combine
- Alternative for custom delimiters or clean copy-pasteable Python lists:
- If you need comma-separated output without alignment spaces or brackets for logging/exporting, use
np.array2string(arr, threshold=np.inf, separator=", ")or convert small arrays witharr.tolist().
- If you need comma-separated output without alignment spaces or brackets for logging/exporting, use
Comparison of NumPy Full-Array Printing Options
| Method | Scope | Removes Ellipsis? |
Prevents Wide-Row Line Wrapping? | Best Used When |
|---|---|---|---|---|
with np.printoptions(threshold=np.inf, linewidth=np.inf): (Recommended) |
Temporary (with block only) |
Yes | Yes | Inspecting a specific large array during debugging without risking terminal freezes later. |
np.set_printoptions(threshold=np.inf) |
Global (entire process / notebook) | Yes | Only if linewidth=np.inf is also passed |
Exploratory scripts or notebooks where all printed arrays are moderately sized. |
np.array2string(arr, threshold=np.inf, ...) |
Single string conversion | Yes | Only if max_line_width=np.inf is passed |
Building formatted log strings, custom separators (separator=","), or prefix/suffix alignment. |
print(arr.tolist()) |
Single print call | Yes | Prints on one single line | Exporting raw Python nested lists without NumPy’s whitespace column padding. |
You must log in or register to comment.
