NumPy Input and Output: array2string() function
numpy.array2string() function
The array2string() function is used to get a string representation of an array.
Syntax:
numpy.array2string(a, max_line_width=None, precision=None, suppress_small=None, separator=' ',
prefix='', style=<no value>, formatter=None, threshold=None, edgeitems=None, sign=None,
floatmode=None, suffix='', **kwarg)
Version: 1.15.0
Parameter:
Name | Description | Required / Optional |
---|---|---|
a | Input array. array_like |
Required |
max_line_width | The maximum number of columns the string should span. Newline characters splits the string appropriately after array elements. int |
Optional |
precision | Floating point precision. Default is the current printing precision (usually 8), which can be altered using set_printoptions. int or None |
Optional |
suppress_small | Represent very small numbers as zero. A number is “very small” if it is smaller than the current printing precision. bool |
Optional |
separator | Inserted between elements. str |
Optional |
prefix | str | Optional |
suffix | The 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) + suffix The output is left-padded by the length of the prefix string, and wrapping is forced at the column max_line_width - len(suffix) str |
Optional |
formatter | If 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:
dict of callables |
Optional |
threshold | Total number of array elements which trigger summarization rather than full repr. int |
Optional |
edgeitems | Number of array items in summary at beginning and end of each dimension. int |
Optional |
sign | Controls 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. string, either '-', '+', or ' ' |
Optional |
floatmode | Controls the interpretation of the precision option for floating-point types. Can take the following values:
str |
Optional |
legacy | If 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 to False, disables legacy mode. Unrecognized strings will be ignored with a warning for forward compatibility. string or False |
Optional |
Returns: array_str : str
String representation of the array.
Raises:
TypeError : if a callable in formatter does 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.
NumPy.array2string() method Example-1:
>>> import numpy as np
>>> x = np.array([1e-25,2,4,6])
>>> print(np.array2string(x, precision=2, separator=',', suppress_small=True))
Output:
[0.,2.,4.,6.]
NumPy.array2string() method Example-2:
>>> import numpy as np
>>> x = np.arange(4.)
>>> np.array2string(x, formatter={'float_kind':lambda x: "%.2f" % x})
Output:
'[0.00 1.00 2.00 3.00]'
NumPy.array2string() method Example-3:
>>> import numpy as np
>>> x = np.arange(4)
>>> np.array2string(x, formatter={'int':lambda x: hex(x)})
Output:
'[0x0 0x1 0x2 0x3]'
Python - NumPy Code Editor:
Previous: fromfile() function
Next: array_repr() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/numpy/input-and-output/array2string.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics