Pretty-print a NumPy array without scientific notation and with given precision
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
NumPy prints arrays using formatting rules that favor compactness, which is why you often see scientific notation such as 1.23e-05 or 4.56e+08. That is useful for raw numerical work, but it is not always ideal for logs, reports, demos, or debugging.
To pretty-print a NumPy array without scientific notation and with a fixed precision, you usually want either a local formatting context or a one-off string conversion. The best choice depends on whether the formatting should affect just one print call or the whole session.
The Simplest Local Solution
For most code, the cleanest answer is np.printoptions() as a context manager:
Output looks like this:
Here is what the options do:
- '
precision=4limits the printed decimal places' - '
suppress=Truediscourages scientific notation for small floating-point values'
Because this uses a context manager, the formatting applies only inside the with block.
Global Session-Wide Print Options
If you want the formatting to affect later prints too, use np.set_printoptions():
This is convenient in a notebook, but be careful: it changes global NumPy printing behavior for the rest of the process.
That is why np.printoptions() is often better in reusable application code.
array2string for One-Off Formatting
If you need a formatted string rather than printing directly, use np.array2string():
This is useful when you are writing to logs, files, or UI components and want precise control over the resulting string.
Forcing a Custom Float Format
Sometimes you want a stricter format than NumPy's general print options provide. In that case, pass a custom formatter:
Output:
This is the most explicit approach. It is especially handy when you want fixed-width decimal formatting regardless of NumPy's default heuristics.
Choose the Right Tool
A good rule of thumb is:
- use
np.printoptions()for a temporary local override - use
np.set_printoptions()for notebook-wide or session-wide formatting - use
np.array2string()when you need the result as a string - use a custom formatter when exact float formatting matters
The formatting choice does not change the underlying numeric values. It only changes how they are displayed.
Common Pitfalls
- Using
np.set_printoptions()in library code and unexpectedly changing global formatting for other parts of the program. - Expecting display formatting to round the actual stored values. It only affects output.
- Forgetting that
suppress=Trueis about notation style, not about the number of decimal places. - Printing very large arrays without adjusting
threshold, then assuming values disappeared. NumPy may summarize long arrays with ellipses. - Confusing
suppress_small=Trueinarray2string()with the globalsuppress=Trueoption. They solve related but slightly different formatting cases.
Summary
- Use
np.printoptions(precision=..., suppress=True)for the safest local pretty-printing. - Use
np.set_printoptions()only when global behavior is acceptable. - Use
np.array2string()when you need a formatted string rather than direct output. - Custom formatters give you the strictest control over decimal rendering.
- Formatting changes the display, not the underlying array values.
Related reading
- Pretty-print an entire Pandas Series / DataFrame
- Pretty-print an entire Pandas Series / DataFrame
- Pretty Printing a pandas dataframe
- Principal Component Analysis in MATLAB
- Pretty Git branch graphs
- Prevent stack trace logging for custom exception in Spring Boot application
- Pretty-Print JSON Data to a File using Python
- Pretty printing XML in Python

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.