|
1 | | -# -*- coding: utf-8 -*- |
2 | | -# --- |
3 | | -# jupyter: |
4 | | -# jupytext: |
5 | | -# cell_metadata_filter: all,-hidden,-heading_collapsed,-run_control,-trusted,-editable |
6 | | -# formats: py:percent |
7 | | -# notebook_metadata_filter: all, -jupytext.text_representation.jupytext_version, |
8 | | -# -jupytext.text_representation.format_version,-language_info.version, -language_info.codemirror_mode.version, |
9 | | -# -language_info.codemirror_mode,-language_info.file_extension, -language_info.mimetype, |
10 | | -# -toc, -rise, -version |
11 | | -# text_representation: |
12 | | -# extension: .py |
13 | | -# format_name: percent |
14 | | -# kernelspec: |
15 | | -# display_name: Python 3 (ipykernel) |
16 | | -# language: python |
17 | | -# name: python3 |
18 | | -# language_info: |
19 | | -# name: python |
20 | | -# nbconvert_exporter: python |
21 | | -# pygments_lexer: ipython3 |
22 | | -# nbhosting: |
23 | | -# title: basic numpy |
24 | | -# --- |
25 | | - |
26 | | -# %% |
| 1 | +--- |
| 2 | +jupytext: |
| 3 | + text_representation: |
| 4 | + extension: .md |
| 5 | + format_name: myst |
| 6 | + encoding: '# -*- coding: utf-8 -*-' |
| 7 | +kernelspec: |
| 8 | + display_name: Python 3 (ipykernel) |
| 9 | + language: python |
| 10 | + name: python3 |
| 11 | +language_info: |
| 12 | + name: python |
| 13 | + nbconvert_exporter: python |
| 14 | + pygments_lexer: ipython3 |
| 15 | +nbhosting: |
| 16 | + title: basic numpy |
| 17 | +--- |
| 18 | + |
| 19 | +```{code-cell} ipython3 |
27 | 20 | import numpy as np |
28 | 21 | import pandas as pd |
29 | 22 | import matplotlib.pyplot as plt |
30 | 23 | import seaborn as sns |
| 24 | +``` |
31 | 25 |
|
32 | | -# %% [markdown] |
33 | | -# # data loading |
| 26 | +# data loading |
34 | 27 |
|
35 | | -# %% [markdown] |
36 | | -# Let's load a dataset on rain precipitations on Seattle on 2014 |
| 28 | ++++ |
| 29 | + |
| 30 | +Let's load a dataset on rain precipitations on Seattle on 2014 |
| 31 | + |
| 32 | +```{code-cell} ipython3 |
| 33 | +:lines_to_next_cell: 2 |
37 | 34 |
|
38 | | -# %% |
39 | 35 | # we download the file from Internet and save it |
40 | 36 | # easiest way, we can pass a URL to read_csv (or a local file) |
41 | 37 | URL = "http://www-sop.inria.fr/members/Arnaud.Legout/formationPython/Exos/Seattle2014.csv" |
|
54 | 50 | # rainfall is an array of precipitation per day |
55 | 51 | # for each day of 2014 |
56 | 52 | # rainfall = pd.read_csv('Seattle2014.csv')['PRCP'].to_numpy() |
| 53 | +``` |
57 | 54 |
|
| 55 | +## Let's visualize |
58 | 56 |
|
59 | | -# %% [markdown] |
60 | | -# ## Let's visualize |
| 57 | ++++ |
61 | 58 |
|
62 | | -# %% [markdown] |
63 | | -# **[assignement]**: plot the amount of rain (in mm) over time; make sure you put a proper label on both axes, and on the global figure |
64 | | -# |
| 59 | +**[assignement]**: plot the amount of rain (in mm) over time; make sure you put a proper label on both axes, and on the global figure |
65 | 60 |
|
66 | | -# %% |
| 61 | +```{code-cell} ipython3 |
67 | 62 | # your code here |
| 63 | +``` |
| 64 | + |
| 65 | +```{code-cell} ipython3 |
| 66 | +:tags: [] |
68 | 67 |
|
69 | | -# %% tags=[] |
70 | 68 | # prune-cell |
71 | 69 |
|
72 | 70 | plt.plot(rainfall) |
73 | 71 | plt.xlabel("days") |
74 | 72 | plt.ylabel("mm") |
75 | 73 | plt.title("Rainy days in 2014 at Seattle") |
| 74 | +``` |
| 75 | + |
| 76 | +## Let's answer the following questions |
76 | 77 |
|
77 | | -# %% [markdown] |
78 | | -# ## Let's answer the following questions |
| 78 | ++++ |
79 | 79 |
|
80 | | -# %% [markdown] |
81 | | -# **What is the shape and dype of the ndarray?** |
| 80 | +**What is the shape and dype of the ndarray?** |
82 | 81 |
|
83 | | -# %% |
| 82 | +```{code-cell} ipython3 |
84 | 83 | # your code here |
| 84 | +``` |
85 | 85 |
|
86 | | -# %% |
| 86 | +```{code-cell} ipython3 |
87 | 87 | # prune-cell |
88 | 88 | print(f"{rainfall.shape=}\n{rainfall.dtype=}") |
| 89 | +``` |
89 | 90 |
|
90 | | -# %% [markdown] |
91 | | -# **How many rainy days?** |
| 91 | +**How many rainy days?** |
92 | 92 |
|
93 | | -# %% |
| 93 | +```{code-cell} ipython3 |
94 | 94 | # your code here |
| 95 | +``` |
95 | 96 |
|
96 | | -# %% |
| 97 | +```{code-cell} ipython3 |
97 | 98 | # prune-cell |
98 | 99 | rainy_days = np.sum(rainfall > 0) |
99 | 100 | print(f"{rainy_days=} days") |
| 101 | +``` |
100 | 102 |
|
101 | | -# %% [markdown] |
102 | | -# **Average precipitation on the year?** |
| 103 | +**Average precipitation on the year?** |
103 | 104 |
|
104 | | -# %% |
| 105 | +```{code-cell} ipython3 |
105 | 106 | # your code here |
| 107 | +``` |
106 | 108 |
|
107 | | -# %% |
| 109 | +```{code-cell} ipython3 |
108 | 110 | # prune-cell |
109 | 111 | ave_precip_year = np.mean(rainfall) |
110 | 112 | print(f"{ave_precip_year=:.2f}mm") |
| 113 | +``` |
111 | 114 |
|
112 | | -# %% [markdown] |
113 | | -# **Average precipitation on the rainy days?** |
| 115 | +**Average precipitation on the rainy days?** |
114 | 116 |
|
115 | | -# %% |
| 117 | +```{code-cell} ipython3 |
116 | 118 | # your code here |
| 119 | +``` |
117 | 120 |
|
118 | | -# %% |
| 121 | +```{code-cell} ipython3 |
119 | 122 | # prune-cell |
120 | 123 | ave_precip_rainy = np.mean(rainfall[rainfall > 0]) |
121 | 124 | print(f"{ave_precip_rainy=:.2f}mm") |
| 125 | +``` |
122 | 126 |
|
123 | | -# %% [markdown] |
124 | | -# **Mean precipitation on January?** |
| 127 | +**Mean precipitation on January?** |
125 | 128 |
|
126 | | -# %% |
| 129 | +```{code-cell} ipython3 |
127 | 130 | # your code here |
| 131 | +``` |
128 | 132 |
|
129 | | -# %% |
| 133 | +```{code-cell} ipython3 |
130 | 134 | # prune-cell |
131 | 135 |
|
132 | 136 | # let's build expressive masks |
|
137 | 141 | # mean precipitation on January |
138 | 142 | mean_jan = np.mean(rainfall[january]) |
139 | 143 | print(f"{mean_jan=:.2f}mm") |
| 144 | +``` |
140 | 145 |
|
141 | | -# %% [markdown] |
142 | | -# **Mean precipitation on January on the rainy days?** |
| 146 | +**Mean precipitation on January on the rainy days?** |
143 | 147 |
|
144 | | -# %% |
| 148 | +```{code-cell} ipython3 |
145 | 149 | # your code here |
| 150 | +``` |
146 | 151 |
|
147 | | -# %% |
| 152 | +```{code-cell} ipython3 |
148 | 153 | # prune-cell |
149 | 154 |
|
150 | 155 | mean_jan_rainy = np.mean(rainfall[january & rainy]) |
151 | 156 | print(f"{mean_jan_rainy=:.2f}mm") |
| 157 | +``` |
152 | 158 |
|
153 | | -# %% [markdown] |
154 | | -# # A transition to pandas |
| 159 | +# A transition to pandas |
155 | 160 |
|
156 | | -# %% |
| 161 | +```{code-cell} ipython3 |
157 | 162 | # But in practice we don’t do that. Here is what we do… |
158 | 163 | # We start to convert to a pandas Series |
159 | 164 | s = pd.Series(rainfall) |
|
164 | 169 |
|
165 | 170 | # possibly resample per month to get the total monthly rain |
166 | 171 | s = s.resample('m').max() |
| 172 | +``` |
167 | 173 |
|
168 | | -# %% |
| 174 | +```{code-cell} ipython3 |
169 | 175 | # then plot |
170 | 176 |
|
171 | | -# %matplotlib ipympl |
| 177 | +%matplotlib ipympl |
172 | 178 |
|
173 | 179 | s.plot.bar() |
174 | 180 | plt.xlabel('month') |
|
177 | 183 | fig = plt.gcf() |
178 | 184 | fig.autofmt_xdate() |
179 | 185 | # plt.show() # if in a terminal |
| 186 | +``` |
180 | 187 |
|
181 | | -# %% [markdown] |
182 | | -# *** |
| 188 | +*** |
0 commit comments