Print options#
Numpy outputs can be configured through set_printoptions
function. Here is described with examples some options.
For more information, see the corresponding page in the official documentation.
import numpy as np
np.random.seed(1)
arr = np.random.uniform(-10, 10, 20)
precision
#
Allows you to select the number of characters to be displayed after the decimal separator. By default it’s 8.
The following cell shows outputs for the different values of the parameter.
print("precision=3")
np.set_printoptions(
precision=3
)
print(arr)
print("precision=5")
np.set_printoptions(
precision=5
)
print(arr)
precision=3
[-1.66 4.406 -9.998 -3.953 -7.065 -8.153 -6.275 -3.089 -2.065 0.776
-1.616 3.704 -5.911 7.562 -9.452 3.409 -1.654 1.174 -7.192 -6.038]
precision=5
[-1.65956 4.40649 -9.99771 -3.95335 -7.06488 -8.15323 -6.2748 -3.08879
-2.06465 0.77633 -1.61611 3.70439 -5.91096 7.56235 -9.45225 3.40935
-1.6539 1.1738 -7.19226 -6.03797]
threshold
#
With the threshold
parameter you can specify the maximum size of the array that can be represented without any summarisation. Summarisation is a way to represent an array shorter by hiding some of the elements.
The following cell shows in the contrast representation of the 20 elements array with values threshold=20
and threshold=19
values.
arr = np.random.rand(20)
print("threshold=20; lead to full display of the array")
np.set_printoptions(threshold=20)
print(arr)
print("threshold=19; lead ot display just summary of the array")
np.set_printoptions(threshold=19)
print(arr)
threshold=20; lead to full display of the array
[0.51998728 0.85432652 0.73038509 0.44367867 0.84823692 0.2926463
0.06968334 0.65043151 0.92419416 0.12764245 0.0786205 0.23087711
0.74445412 0.61933252 0.55952937 0.44320053 0.34593265 0.001056
0.18968612 0.75649162]
threshold=19; lead ot display just summary of the array
[0.51998728 0.85432652 0.73038509 ... 0.001056 0.18968612 0.75649162]
edgeitems
#
Defines how many elements are displayed on both sides of ...
. By default it’s 3.
The following cell shows in the contrast representation of the array with edgeitems=3
and edgeitems=5
.
arr = np.random.rand(20)
np.set_printoptions(
edgeitems=3,
threshold=0
)
print("edgeitems=3")
print(arr)
np.set_printoptions(
edgeitems=5
)
print("edgeitems=5")
print(arr)
edgeitems=3
[0.17201879 0.00447939 0.45801459 ... 0.94304821 0.91343646 0.49323007]
edgeitems=5
[0.17201879 0.00447939 0.45801459 0.90427001 0.15933161 ... 0.19034866
0.49419617 0.94304821 0.91343646 0.49323007]
linewidth
#
How many characters will be displayed on each line, the default is 75. [
and \n
are also counted. That’s why you always get 3 characters less from the array. The result seems to be approximate, because the specified linewidth
may not be a multiple of the number of matrix symbols. Moreover, strings are different - minuses, and the value of the precision
argument will affect the length of strings.
The follwing cell sets the parameter under consideration to the 20. And prints the array.
np.set_printoptions(
linewidth=20,
threshold=1000,
precision=1
)
print(arr)
[ -1.7 4.4 -10.
-4. -7.1 -8.2
-6.3 -3.1 -2.1
0.8 -1.6 3.7
-5.9 7.6 -9.5
3.4 -1.7 1.2
-7.2 -6. ]
Array is printed thinner than the default numpy representation. To be sure that numpy has followed to the specified instructions, the following cell counts the number of symbols for each row.
matr_strs = str(arr).split("\n")
for line in matr_strs:
print(f"- '{line}' - {len(line)} symbols")
- '[ -1.7 4.4 -10.' - 17 symbols
- ' -4. -7.1 -8.2' - 18 symbols
- ' -6.3 -3.1 -2.1' - 18 symbols
- ' 0.8 -1.6 3.7' - 18 symbols
- ' -5.9 7.6 -9.5' - 18 symbols
- ' 3.4 -1.7 1.2' - 18 symbols
- ' -7.2 -6. ]' - 13 symbols
All lines have less than 20 symbols - the only option to fit the array into lines that don’t take more than 20 symbols.
Fixed point notation#
Fixed point notation (e.g. exponential notation, scientific notation) is a way of writting numbers like this 1e-4
. By default, numpy will print any array using this type of notation only if \(m < 1e-4\) or \(M/m > 1e3\), where \(m\) minimum value in the array under consideration \(M\) maximum value of the array under consideration. You can change the behaviour by using the supress=True
parameter of the set_printoptions
, this forces numpy to always use the regular form of the number.
The following cell creates a numpy array where one of the numbers is extremely small, and displays it with different values of the suppress
parameter.
example_array = np.array([1e-6, 10])
np.set_printoptions(suppress=True)
print(example_array)
np.set_printoptions(suppress=False)
print(example_array)
[ 0.000001 10. ]
[1.e-06 1.e+01]
The same example, but using a different rule of printing value in the scientific form - ratio of minimum and maximum numbers.
example_array = np.array([1, 1e+5])
np.set_printoptions(suppress=True)
print(example_array)
np.set_printoptions(suppress=False)
print(example_array)
[ 1. 100000.]
[1.e+00 1.e+05]
Note: Scientific notation doesn’t work for the integer datatype. The following cell show examples with the same arrays, but one stored in the integer datatype and the other in the float datatype.
np.set_printoptions(suppress = False)
print("int")
ints_arr = np.array([1, 1001])
print(ints_arr, end = "\n\n\n")
floats_arr = np.array([1.0, 1001])
print("float")
print(floats_arr)
int
[ 1 1001]
float
[1.000e+00 1.001e+03]
nanstr
#
nanstr
parameter allows to set what string to use for pringing NaN
values.
The following cell prints an array with nanstr="No data"
.
np.set_printoptions(nanstr="No data")
nan_arr = np.fromfunction(
lambda i, j: np.where(
(i+j)%2, 0, np.nan
),
(4,4)
)
nan_arr
array([[No data, 0., No data, 0.],
[ 0., No data, 0., No data],
[No data, 0., No data, 0.],
[ 0., No data, 0., No data]])
infstr
#
The infstr
parameter allows you to specify how the way infinity is displayed on your numpy arrays.
Following code shows how Custom infinity
line can be used for defining how infinity values have to be displayed in numpy arrays.
np.set_printoptions(infstr="Custom infinity")
nan_arr = np.fromfunction(
lambda i, j: np.where(
(i+j)%2, -np.inf, np.inf
),
(4,4)
)
nan_arr
array([[ Custom infinity, -Custom infinity, Custom infinity,
-Custom infinity],
[-Custom infinity, Custom infinity, -Custom infinity,
Custom infinity],
[ Custom infinity, -Custom infinity, Custom infinity,
-Custom infinity],
[-Custom infinity, Custom infinity, -Custom infinity,
Custom infinity]])
sign
#
With sign parameter allows you specify how numpy handles the displaycement of the +
sign. Generally it can take 3 values “ “, “+” and “-“:
In case of “-”, nothing is printed.
In the case “+”, each positive number is preceded by a “+”.
In the case “ “, each positive number is preceded by a space.
The next cell shows the differences in display for all possible cases.
np.set_printoptions(
sign='-',
linewidth=10
)
print(np.array([1.0]))
np.set_printoptions(sign='+')
print(np.array([1.0]))
np.set_printoptions(sign=' ')
print(np.array([1.0]))
[1.]
[+1.]
[ 1.]
Note: if you’re using sign='-'
, numpy still may can add some space to the negative numbers just to make the result to look like a matrix.
arr = np.random.normal(0, 1, [3, 3])
np.set_printoptions(sign='-', linewidth=50)
print(arr)
[[-0.27320818 -1.730814 -0.25073122]
[ 0.62554647 1.92942427 0.4122529 ]
[ 0.42870972 0.18284921 -0.80934173]]
[[-0.27320818 -1.730814 -0.25073122]
[ 0.62554647 1.92942427 0.4122529 ]
[ 0.42870972 0.18284921 -0.80934173]]
Fromatter#
With the formatter
argument, you can specify for each type a fucntion that will process that type before displaying it. Just pass the dictionary that with the type/function correspondance. Also key all
can be used to apply the function to all datatypes.
The following cell shows application of use of the formatter
for float
datatype.
bool_exprimental = np.array([True, False])
def bool_formatter(val):
return "✓" if val else "×"
np.set_printoptions(
formatter={'bool':bool_formatter}
)
print("bool dtype")
print(bool_exprimental)
bool dtype
[✓ ×]
To remove all formatters, simply pass an empty dictionary to the formatter
argument.
np.set_printoptions(formatter={})
print(bool_exprimental)
[ True False]
The following code applies rules to the all
key - so int
will use the same rule as float
.
arr = np.random.rand(3, 3)
np.set_printoptions(
formatter = {'all': lambda val: str(type(val))}
)
print(arr)
print(np.array([0, 1, 2, 3]))
[[<class 'numpy.float64'> <class 'numpy.float64'> <class 'numpy.float64'>]
[<class 'numpy.float64'> <class 'numpy.float64'> <class 'numpy.float64'>]
[<class 'numpy.float64'> <class 'numpy.float64'> <class 'numpy.float64'>]]
[<class 'numpy.int64'> <class 'numpy.int64'> <class 'numpy.int64'>
<class 'numpy.int64'>]
Note: The key of the specific datatype has higher priority than the all
key, the following cell proves this.
np.set_printoptions(
formatter = {
'all': lambda x: "key for all",
'bool': lambda x: "key for bool",
}
)
bool_array = np.random.choice([True, False], 20)
bool_array
array([key for bool, key for bool, key for bool, key for bool,
key for bool, key for bool, key for bool, key for bool,
key for bool, key for bool, key for bool, key for bool,
key for bool, key for bool, key for bool, key for bool,
key for bool, key for bool, key for bool, key for bool])
floatmode
#
Allows you to specify more specific rules associated with the display of the float point numbers in numpy. It can take one of the following values:
fixed
: uses exactly the number of symbols as specified by theprecision
parameter.unique
: is the number of characters required to uniquely define a number.maxprec
: if number can be defined in less thanprecision
digits, it will be displayed shorter, but number can’t take more thanprecision
digits.maxprec_equal
: all numbers use the same number of symbols, determined by the digits required to uniquely display the longest number, but not exceedingprecision
.
The following cell creates an array that will be used as example. The array is created so the first element can be idtentified by 4 digits and others by 2 digits.
np.set_printoptions(precision=3)
test_arr = np.array([0.4839, 0.7])
The floatmode='fixed'
for the given example will hide last sign of the 4-digit value, one digit value will be filled with zeros to have 3 digits.
np.set_printoptions(floatmode='fixed')
test_arr
array([0.484, 0.700], shape=(2,))
The floatmode='unique'
literally tells the system to ignore the precision=3
- all values use this and only this number of digits for complete definition.
np.set_printoptions(floatmode='unique')
test_arr
array([0.4839, 0.7 ], shape=(2,))
The floatmode='maxprec'
for a 4 digit number truncates it to 3 digits, but for a 1 digit float it just leaves as it is.
np.set_printoptions(floatmode="maxprec")
test_arr
array([0.484, 0.7 ], shape=(2,))
The floatmode="maxprec_equl"
for the basic example works just like floatmode=fixed
as the 4-digit value is measured with 3 digits, the 1-digit value is completed with zeros corresponding to the 3 digits. The main idea of this type of the floatmode
is in the second example it uses only 2 digits for both numbers, as values with maximum number of digits have exactly 2 digits.
np.set_printoptions(precision=3, floatmode="maxprec_equal")
print(test_arr)
print(np.array([0.55, 0.7]))
[0.484 0.700]
[0.55 0.70]