numpy.set_printoptions#

numpy.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)[source]#

Set printing options.

These options determine the way floating point numbers, arrays andother NumPy objects are displayed.

Parameters:
precisionint or None, optional

Number of digits of precision for floating point output (default 8).May be None iffloatmode is notfixed, to print as many digits asnecessary to uniquely specify the value.

thresholdint, optional

Total number of array elements which trigger summarizationrather than full repr (default 1000).To always use the full repr without summarization, passsys.maxsize.

edgeitemsint, optional

Number of array items in summary at beginning and end ofeach dimension (default 3).

linewidthint, optional

The number of characters per line for the purpose of insertingline breaks (default 75).

suppressbool, optional

If True, always print floating point numbers using fixed pointnotation, in which case numbers equal to zero in the current precisionwill print as zero. If False, then scientific notation is used whenabsolute value of the smallest number is < 1e-4 or the ratio of themaximum absolute value to the minimum is > 1e3. The default is False.

nanstrstr, optional

String representation of floating point not-a-number (default nan).

infstrstr, optional

String representation of floating point infinity (default inf).

signstring, either ‘-’, ‘+’, or ‘ ‘, optional

Controls printing of the sign of floating-point types. If ‘+’, alwaysprint 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 ‘-‘)

Changed in version 2.0:The sign parameter can now be an integer type, previouslytypes were floating-point types.

formatterdict of callables, optional

If not None, the keys should indicate the type(s) that the respectiveformatting function applies to. Callables should return a string.Types that are not specified (by their corresponding keys) are handledby the default formatters. Individual types for which a formattercan be set are:

  • ‘bool’

  • ‘int’

  • ‘timedelta’ : anumpy.timedelta64

  • ‘datetime’ : anumpy.datetime64

  • ‘float’

  • ‘longfloat’ : 128-bit floats

  • ‘complexfloat’

  • ‘longcomplexfloat’ : composed of two 128-bit floats

  • ‘numpystr’ : typesnumpy.bytes_ andnumpy.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’

floatmodestr, optional

Controls the interpretation of theprecision option forfloating-point types. Can take the following values(default maxprec_equal):

  • ‘fixed’: Always print exactlyprecision fractional digits,

    even if this would print more or fewer digits thannecessary to specify the value uniquely.

  • ‘unique’: Print the minimum number of fractional digits necessary

    to represent each value uniquely. Different elements mayhave a different number of digits. The value of theprecision option is ignored.

  • ‘maxprec’: Print at mostprecision fractional digits, but if

    an element can be uniquely represented with fewer digitsonly print it with that many.

  • ‘maxprec_equal’: Print at mostprecision fractional digits,

    but if every element in the array can be uniquelyrepresented with an equal number of fewer digits, use thatmany digits for all elements.

legacystring orFalse, optional

If set to the string'1.13' enables 1.13 legacy printing mode. Thisapproximates numpy 1.13 print output by including a space in the signposition of floats and different behavior for 0d arrays. This alsoenables 1.21 legacy printing mode (described below).

If set to the string'1.21' enables 1.21 legacy printing mode. Thisapproximates numpy 1.21 print output of complex structured dtypesby not inserting spaces after commas that separate fields and aftercolons.

If set to'1.25' approximates printing of 1.25 which mainly meansthat numeric scalars are printed without their type information, e.g.as3.0 rather thannp.float64(3.0).

If set to'2.1', shape information is not given when arrays aresummarized (i.e., multiple elements replaced with...).

If set to'2.2', the transition to use scientific notation forprintingnp.float16 andnp.float32 types may happen later ornot at all for larger values.

If set toFalse, disables legacy mode.

Unrecognized strings will be ignored with a warning for forwardcompatibility.

Changed in version 1.22.0.

Changed in version 2.2.

override_repr: callable, optional

If set a passed function will be used for generating arrays’ repr.Other options will be ignored.

Notes

formatter is always reset with a call toset_printoptions.

Useprintoptions as a context manager to set the values temporarily.

Examples

Floating point precision can be set:

>>>importnumpyasnp>>>np.set_printoptions(precision=4)>>>np.array([1.123456789])[1.1235]

Long arrays can be summarised:

>>>np.set_printoptions(threshold=5)>>>np.arange(10)array([0, 1, 2, ..., 7, 8, 9], shape=(10,))

Small results can be suppressed:

>>>eps=np.finfo(float).eps>>>x=np.arange(4.)>>>x**2-(x+eps)**2array([-4.9304e-32, -4.4409e-16,  0.0000e+00,  0.0000e+00])>>>np.set_printoptions(suppress=True)>>>x**2-(x+eps)**2array([-0., -0.,  0.,  0.])

A custom formatter can be used to display array elements as desired:

>>>np.set_printoptions(formatter={'all':lambdax:'int: '+str(-x)})>>>x=np.arange(3)>>>xarray([int: 0, int: -1, int: -2])>>>np.set_printoptions()# formatter gets reset>>>xarray([0, 1, 2])

To put back the default options, you can use:

>>>np.set_printoptions(edgeitems=3,infstr='inf',...linewidth=75,nanstr='nan',precision=8,...suppress=False,threshold=1000,formatter=None)

Also to temporarily override options, useprintoptionsas a context manager:

>>>withnp.printoptions(precision=2,suppress=True,threshold=5):...np.linspace(0,10,10)array([ 0.  ,  1.11,  2.22, ...,  7.78,  8.89, 10.  ], shape=(10,))
On this page