bundles / numpy latest / numpy / set_printoptions
function
numpy:set_printoptions
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/_core/arrayprint.py :122
Signature
def set_printoptions ( precision = None , threshold = None , edgeitems = None , linewidth = None , suppress = None , nanstr = None , infstr = None , formatter = None , sign = None , floatmode = None , * , legacy = None , override_repr = None ) Summary
Set printing options.
Extended Summary
These options determine the way floating point numbers, arrays and other NumPy objects are displayed.
Parameters
precision: int or None, optionalNumber of digits of precision for floating point output (default 8). May be None if
floatmodeis notfixed, to print as many digits as necessary to uniquely specify the value.threshold: int, optionalTotal number of array elements which trigger summarization rather than full repr (default 1000). To always use the full repr without summarization, pass
sys.maxsize.edgeitems: int, optionalNumber of array items in summary at beginning and end of each dimension (default 3).
linewidth: int, optionalThe number of characters per line for the purpose of inserting line breaks (default 75).
suppress: bool, optionalIf True, always print floating point numbers using fixed point notation, in which case numbers equal to zero in the current precision will print as zero. If False, then scientific notation is used when absolute value of the smallest number is < 1e-4 or the ratio of the maximum absolute value to the minimum is > 1e3. The default is False.
nanstr: str, optionalString representation of floating point not-a-number (default nan).
infstr: str, optionalString representation of floating point infinity (default inf).
sign: string, either '-', '+', or ' ', optionalControls printing of the sign of floating-point types. If '+', always print the sign of positive values. If ' ', always prints a space (whitespace character) in the sign position of positive values. If '-', omit the sign character of positive values. (default '-')
formatter: dict of callables, optionalIf not None, the keys should indicate the type(s) that the respective formatting function applies to. Callables should return a string. Types that are not specified (by their corresponding keys) are handled by the default formatters. Individual types for which a formatter can be set are:
'bool'
'int'
'timedelta'a numpy.timedelta64
'datetime'a numpy.datetime64
'float'
'longfloat'128-bit floats
'complexfloat'
'longcomplexfloat'composed of two 128-bit floats
'numpystr'types numpy.bytes_ and numpy.str_
'object'
np.object_arrays
Other keys that can be used to set a group of types at once are:
'all'sets all types
'int_kind'sets 'int'
'float_kind'sets 'float' and 'longfloat'
'complex_kind'sets 'complexfloat' and 'longcomplexfloat'
'str_kind'sets 'numpystr'
floatmode: str, optionalControls the interpretation of the
precisionoption for floating-point types. Can take the following values (default maxprec_equal):'fixed': Always print exactly
precisionfractional digits,even if this would print more or fewer digits than necessary to specify the value uniquely.
'unique': Print the minimum number of fractional digits necessary
to represent each value uniquely. Different elements may have a different number of digits. The value of the
precisionoption is ignored.
'maxprec': Print at most
precisionfractional digits, but ifan element can be uniquely represented with fewer digits only print it with that many.
'maxprec_equal': Print at most
precisionfractional digits,but if every element in the array can be uniquely represented with an equal number of fewer digits, use that many digits for all elements.
legacy: string or `False`, optionalIf set to the string
'1.13'enables 1.13 legacy printing mode. This approximates numpy 1.13 print output by including a space in the sign position of floats and different behavior for 0d arrays. This also enables 1.21 legacy printing mode (described below).If set to the string
'1.21'enables 1.21 legacy printing mode. This approximates numpy 1.21 print output of complex structured dtypes by not inserting spaces after commas that separate fields and after colons.If set to
'1.25'approximates printing of 1.25 which mainly means that numeric scalars are printed without their type information, e.g. as3.0rather thannp.float64(3.0).If set to
'2.1', shape information is not given when arrays are summarized (i.e., multiple elements replaced with...).If set to
'2.2', the transition to use scientific notation for printingnp.float16andnp.float32types may happen later or not at all for larger values.If set to
False, disables legacy mode.Unrecognized strings will be ignored with a warning for forward compatibility.
override_repr: callable, optionalIf set a passed function will be used for generating arrays' repr. Other options will be ignored.
Notes
formatteris always reset with a call to set_printoptions.Use printoptions as a context manager to set the values temporarily.
These print options apply only to NumPy ndarrays, not to scalars.
Examples
Floating point precision can be set:import numpy as np np.set_printoptions(precision=4)✓
np.array([1.123456789])
✗np.set_printoptions(threshold=5) np.arange(10)✓
eps = np.finfo(float).eps x = np.arange(4.) x**2 - (x + eps)**2 np.set_printoptions(suppress=True) x**2 - (x + eps)**2✓
np.set_printoptions(formatter={'all':lambda x: 'int: '+str(-x)}) x = np.arange(3) x np.set_printoptions() # formatter gets reset x✓
np.set_printoptions(edgeitems=3, infstr='inf', linewidth=75, nanstr='nan', precision=8, suppress=False, threshold=1000, formatter=None)✓
with np.printoptions(precision=2, suppress=True, threshold=5): np.linspace(0, 10, 10)✓
See also
- array2string
- get_printoptions
- printoptions
Aliases
-
numpy.set_printoptions