/
printing_floating.Rmd
88 lines (74 loc) · 1.9 KB
/
printing_floating.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
---
jupyter:
jupytext:
text_representation:
extension: .Rmd
format_name: rmarkdown
format_version: '1.2'
jupytext_version: 1.11.5
---
$\newcommand{L}[1]{\| #1 \|}\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]{\operatorname{Im}\, (#1)}$
## Making floating points numbers print nicely
By default, when numpy prints an array, it looks for very small or very large
numbers. If it finds either, it uses exponents to show the numbers. This can
be annoying:
```{python}
import numpy as np
np.pi
```
```{python}
np.array([np.pi, 0.000001])
```
In order to avoid this, you can tell numpy not to use exponential notation for
small numbers:
```{python}
np.set_printoptions(suppress=True)
np.array([np.pi, 0.000001])
```
This setting stays in place until you change it:
```{python}
np.array([np.pi, 0.000001])
```
It can also be annoying to see many digits after the decimal point, if
you know that these are not important. You can set the number of digits
after the decimal point for numpy printing like this:
```{python}
np.set_printoptions(precision=4)
a = np.array([np.pi, 0.000001])
a
```
This only affects printing, not calculations:
```{python}
b = a * 2
b
# change the printoptions again, we see more decimal places
np.set_printoptions(precision=8)
b
```
<!-- vim:ft=rst -->
<!-- Course -->
<!-- BIC -->
<!-- Python distributions -->
<!-- Version control -->
<!-- Editors -->
<!-- Python and common libraries -->
<!-- IPython -->
<!-- Virtualenv and helpers -->
<!-- Pypi and packaging -->
<!-- Mac development -->
<!-- Windows development -->
<!-- Nipy and friends -->
<!-- FMRI datasets -->
<!-- Languages -->
<!-- Imaging software -->
<!-- Installation -->
<!-- Tutorials -->
<!-- MB tutorials -->
<!-- Ideas -->
<!-- Psych-214 -->
<!-- People -->
<!-- Licenses -->
<!-- Neuroimaging stuff -->
<!-- OpenFMRI projects -->
<!-- Unix -->
<!-- Substitutions -->