np_preferences

pyhelpers.settings.np_preferences(reset=False, precision=4, head_tail=5, line_char=120, formatter=None, **kwargs)[source]

Alter some default parameters for displaying NumPy arrays.

This function allows customising the display options for NumPy arrays, including decimal precision, summary at the beginning and end of each dimension, line width for inserting line breaks and optional custom formatting.

Parameters:
  • reset (bool) – Whether to reset to the default print options set by the function numpy.set_printoptions(); defaults to False.

  • precision (int) – Number of decimal points to display, corresponding to precision of the function numpy.set_printoptions(); defaults to 4.

  • line_char (int) – Number of characters per line for inserting line breaks, corresponding to linewidth of the function numpy.set_printoptions(); defaults to 120.

  • head_tail (int) – Number of array items to summarise at the beginning (head) and end (tail) of each dimension, corresponding to edgeitems of the function numpy.set_printoptions(); defaults to 5.

  • formatter (dict | None) – Custom format specification, corresponding to formatter of the function numpy.set_printoptions(); if formatter=None (default), empty decimal places are filled with zeros for the specified precision.

  • kwargs – [Optional] Additional parameters for the function numpy.set_printoptions().

Examples:

>>> import numpy as np
>>> np.random.seed(0)
>>> random_array = np.random.rand(100, 100)
>>> random_array
array([[0.5488135 , 0.71518937, 0.60276338, ..., 0.02010755, 0.82894003,
        0.00469548],
       [0.67781654, 0.27000797, 0.73519402, ..., 0.25435648, 0.05802916,
        0.43441663],
       [0.31179588, 0.69634349, 0.37775184, ..., 0.86219152, 0.97291949,
        0.96083466],
       ...,
       [0.89111234, 0.26867428, 0.84028499, ..., 0.5736796 , 0.73729114,
        0.22519844],
       [0.26969792, 0.73882539, 0.80714479, ..., 0.94836806, 0.88130699,
        0.1419334 ],
       [0.88498232, 0.19701397, 0.56861333, ..., 0.75842952, 0.02378743,
        0.81357508]])
>>> from pyhelpers.settings import np_preferences
>>> np_preferences(precision=2)
>>> random_array
array([[0.55, 0.72, 0.60, 0.54, 0.42, ..., 0.18, 0.59, 0.02, 0.83, 0.00],
       [0.68, 0.27, 0.74, 0.96, 0.25, ..., 0.49, 0.23, 0.25, 0.06, 0.43],
       [0.31, 0.70, 0.38, 0.18, 0.02, ..., 0.22, 0.10, 0.86, 0.97, 0.96],
       [0.91, 0.77, 0.33, 0.08, 0.41, ..., 0.96, 0.36, 0.36, 0.02, 0.19],
       [0.40, 0.93, 0.10, 0.95, 0.87, ..., 0.27, 0.46, 0.40, 0.25, 0.51],
       ...,
       [0.03, 0.99, 0.09, 0.45, 0.84, ..., 0.12, 0.29, 0.37, 0.91, 0.14],
       [0.62, 0.20, 0.29, 0.45, 0.55, ..., 0.48, 0.87, 0.22, 0.14, 0.93],
       [0.89, 0.27, 0.84, 0.76, 1.00, ..., 0.98, 0.41, 0.57, 0.74, 0.23],
       [0.27, 0.74, 0.81, 0.20, 0.31, ..., 0.51, 0.23, 0.95, 0.88, 0.14],
       [0.88, 0.20, 0.57, 0.93, 0.56, ..., 0.55, 0.40, 0.76, 0.02, 0.81]])

Reset to default settings:

>>> np_preferences(reset=True)
>>> random_array
array([[0.54881350, 0.71518937, 0.60276338, ..., 0.02010755, 0.82894003,
        0.00469548],
       [0.67781654, 0.27000797, 0.73519402, ..., 0.25435648, 0.05802916,
        0.43441663],
       [0.31179588, 0.69634349, 0.37775184, ..., 0.86219152, 0.97291949,
        0.96083466],
       ...,
       [0.89111234, 0.26867428, 0.84028499, ..., 0.57367960, 0.73729114,
        0.22519844],
       [0.26969792, 0.73882539, 0.80714479, ..., 0.94836806, 0.88130699,
        0.14193340],
       [0.88498232, 0.19701397, 0.56861333, ..., 0.75842952, 0.02378743,
        0.81357508]])