bundles / numpy 2.4.3 / numpy / _core / arrayprint / array2string
function
numpy._core.arrayprint:array2string
source: /numpy/_core/arrayprint.py :635
Signature
def array2string ( a , max_line_width = None , precision = None , suppress_small = None , separator = , prefix = '' , * , formatter = None , threshold = None , edgeitems = None , sign = None , floatmode = None , suffix = '' , legacy = None ) Summary
Return a string representation of an array.
Parameters
a: ndarrayInput array.
max_line_width: int, optionalInserts newlines if text is longer than
max_line_width. Defaults tonumpy.get_printoptions()['linewidth'].precision: int or None, optionalFloating point precision. Defaults to
numpy.get_printoptions()['precision'].suppress_small: bool, optionalRepresent numbers "very close" to zero as zero; default is False. Very close is defined by precision: if the precision is 8, e.g., numbers smaller (in absolute value) than 5e-9 are represented as zero. Defaults to
numpy.get_printoptions()['suppress'].separator: str, optionalInserted between elements.
prefix: str, optionalsuffix: str, optionalThe length of the prefix and suffix strings are used to respectively align and wrap the output. An array is typically printed as
prefix + array2string(a) + suffixThe output is left-padded by the length of the prefix string, and wrapping is forced at the column
max_line_width - len(suffix). It should be noted that the content of prefix and suffix strings are not included in the output.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
'void'type numpy.void
'numpystr'types numpy.bytes_ and numpy.str_
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'
threshold: int, optionalTotal number of array elements which trigger summarization rather than full repr. Defaults to
numpy.get_printoptions()['threshold'].edgeitems: int, optionalNumber of array items in summary at beginning and end of each dimension. Defaults to
numpy.get_printoptions()['edgeitems'].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. Defaults to
numpy.get_printoptions()['sign'].floatmode: str, optionalControls the interpretation of the
precisionoption for floating-point types. Defaults tonumpy.get_printoptions()['floatmode']. Can take the following values:'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 if an 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. If set toFalse, disables legacy mode. Unrecognized strings will be ignored with a warning for forward compatibility.
Returns
array_str: strString representation of the array.
Raises
: TypeErrorif a callable in
formatterdoes not return a string.
Notes
If a formatter is specified for a certain type, the precision keyword is ignored for that type.
This is a very flexible function; array_repr and array_str are using array2string internally so keywords with the same name should work identically in all three functions.
Examples
import numpy as np x = np.array([1e-16,1,2,3]) np.array2string(x, precision=2, separator=',', suppress_small=True)
x = np.arange(3.) np.array2string(x, formatter={'float_kind':lambda x: "%.2f" % x})
x = np.arange(3) np.array2string(x, formatter={'int':lambda x: hex(x)})
See also
Aliases
-
numpy._core.arrayprint._array2string_impl