Skip to content

Commit e6b5c8b

Browse files
committed
Removing redundant challenges and regenerating HTML
1 parent 9e01603 commit e6b5c8b

File tree

6 files changed

+49
-105
lines changed

6 files changed

+49
-105
lines changed

01-numpy.html

Lines changed: 20 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h1 class="title">Programming with Python</h1>
3030
<h2 class="subtitle">Analyzing Patient Data</h2>
3131
<section class="objectives panel panel-warning">
3232
<div class="panel-heading">
33-
<h2 id="learning-objectives"><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
33+
<h2><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
3434
</div>
3535
<div class="panel-body">
3636
<ul>
@@ -74,50 +74,23 @@ <h2 id="learning-objectives"><span class="glyphicon glyphicon-certificate"></spa
7474
<p>As the example above shows, we can print several things at once by separating them with commas.</p>
7575
<p>If we imagine the variable as a sticky note with a name written on it, assignment is like putting the sticky note on a particular value:</p>
7676
<div class="figure">
77-
<img src="fig/python-sticky-note-variables-01.svg" alt="Variables as Sticky Notes" />
78-
<p class="caption">Variables as Sticky Notes</p>
77+
<img src="fig/python-sticky-note-variables-01.svg" alt="Variables as Sticky Notes" /><p class="caption">Variables as Sticky Notes</p>
7978
</div>
8079
<p>This means that assigning a value to one variable does <em>not</em> change the values of other variables. For example, let’s store the subject’s weight in pounds in a variable:</p>
8180
<pre class="sourceCode python"><code class="sourceCode python">weight_lb = <span class="fl">2.2</span> * weight_kg
8281
<span class="dt">print</span> <span class="st">&#39;weight in kilograms:&#39;</span>, weight_kg, <span class="st">&#39;and in pounds:&#39;</span>, weight_lb</code></pre>
8382
<pre class="output"><code>weight in kilograms: 57.5 and in pounds: 126.5</code></pre>
8483
<div class="figure">
85-
<img src="fig/python-sticky-note-variables-02.svg" alt="Creating Another Variable" />
86-
<p class="caption">Creating Another Variable</p>
84+
<img src="fig/python-sticky-note-variables-02.svg" alt="Creating Another Variable" /><p class="caption">Creating Another Variable</p>
8785
</div>
8886
<p>and then change <code>weight_kg</code>:</p>
8987
<pre class="sourceCode python"><code class="sourceCode python">weight_kg = <span class="fl">100.0</span>
9088
<span class="dt">print</span> <span class="st">&#39;weight in kilograms is now:&#39;</span>, weight_kg, <span class="st">&#39;and weight in pounds is still:&#39;</span>, weight_lb</code></pre>
9189
<pre class="output"><code>weight in kilograms is now: 100.0 and weight in pounds is still: 126.5</code></pre>
9290
<div class="figure">
93-
<img src="fig/python-sticky-note-variables-03.svg" alt="Updating a Variable" />
94-
<p class="caption">Updating a Variable</p>
91+
<img src="fig/python-sticky-note-variables-03.svg" alt="Updating a Variable" /><p class="caption">Updating a Variable</p>
9592
</div>
9693
<p>Since <code>weight_lb</code> doesn’t “remember” where its value came from, it isn’t automatically updated when <code>weight_kg</code> changes. This is different from the way spreadsheets work.</p>
97-
<section class="challenge panel panel-success">
98-
<div class="panel-heading">
99-
<h2 id="whats-inside-the-box"><span class="glyphicon glyphicon-pencil"></span>What’s inside the box?</h2>
100-
</div>
101-
<div class="panel-body">
102-
<p>Draw diagrams showing what variables refer to what values after each statement in the following program:</p>
103-
<pre class="sourceCode python"><code class="sourceCode python">weight = <span class="fl">70.5</span>
104-
age = <span class="dv">35</span>
105-
<span class="co"># Take a trip to the planet Neptune</span>
106-
weight = weight * <span class="fl">1.14</span>
107-
age = age + <span class="dv">20</span></code></pre>
108-
</div>
109-
</section>
110-
<section class="challenge panel panel-success">
111-
<div class="panel-heading">
112-
<h2 id="sorting-out-references"><span class="glyphicon glyphicon-pencil"></span>Sorting out references</h2>
113-
</div>
114-
<div class="panel-body">
115-
<p>What does the following program print out?</p>
116-
<pre class="sourceCode python"><code class="sourceCode python">first, second = <span class="st">&#39;Grace&#39;</span>, <span class="st">&#39;Hopper&#39;</span>
117-
third, fourth = second, first
118-
<span class="dt">print</span> third, fourth</code></pre>
119-
</div>
120-
</section>
12194
<p>Just as we can assign a single value to a variable, we can also assign an array of values to a variable using the same syntax. Let’s re-run <code>numpy.loadtxt</code> and save its result:</p>
12295
<pre class="sourceCode python"><code class="sourceCode python">data = numpy.loadtxt(fname=<span class="st">&#39;inflammation-01.csv&#39;</span>, delimiter=<span class="st">&#39;,&#39;</span>)</code></pre>
12396
<p>This statement doesn’t produce any output because assignment doesn’t display anything. If we want to check that our data has been loaded, we can print the variable’s value:</p>
@@ -144,7 +117,7 @@ <h2 id="sorting-out-references"><span class="glyphicon glyphicon-pencil"></span>
144117
<p>The expression <code>data[30, 20]</code> may not surprise you, but <code>data[0, 0]</code> might. Programming languages like Fortran and MATLAB start counting at 1, because that’s what human beings have done for thousands of years. Languages in the C family (including C++, Java, Perl, and Python) count from 0 because that’s simpler for computers to do. As a result, if we have an M×N array in Python, its indices go from 0 to M-1 on the first axis and 0 to N-1 on the second. It takes a bit of getting used to, but one way to remember the rule is that the index is how many steps we have to take from the start to get the item we want.</p>
145118
<aside class="callout panel panel-info">
146119
<div class="panel-heading">
147-
<h2 id="in-the-corner"><span class="glyphicon glyphicon-pushpin"></span>In the Corner</h2>
120+
<h2><span class="glyphicon glyphicon-pushpin"></span>In the Corner</h2>
148121
</div>
149122
<div class="panel-body">
150123
<p>What may also surprise you is that when Python displays an array, it shows the element with index <code>[0, 0]</code> in the upper left corner rather than the lower left. This is consistent with the way mathematicians draw matrices, but different from the Cartesian coordinates. The indices are (row, column) instead of (column, row) for the same reason, which can be confusing when plotting data.</p>
@@ -172,29 +145,6 @@ <h2 id="in-the-corner"><span class="glyphicon glyphicon-pushpin"></span>In the C
172145
[[ 2. 3. 0. 0.]
173146
[ 1. 1. 0. 1.]
174147
[ 2. 2. 1. 1.]]</code></pre>
175-
<section class="challenge panel panel-success">
176-
<div class="panel-heading">
177-
<h2 id="slicing-strings"><span class="glyphicon glyphicon-pencil"></span>Slicing strings</h2>
178-
</div>
179-
<div class="panel-body">
180-
<p>A section of an array is called a <strong>slice</strong>. We can take slices of character strings as well:</p>
181-
<pre class="sourceCode python"><code class="sourceCode python">element = <span class="st">&#39;oxygen&#39;</span>
182-
<span class="dt">print</span> <span class="st">&#39;first three characters:&#39;</span>, element[<span class="dv">0</span>:<span class="dv">3</span>]
183-
<span class="dt">print</span> <span class="st">&#39;last three characters:&#39;</span>, element[<span class="dv">3</span>:<span class="dv">6</span>]</code></pre>
184-
<pre class="output"><code>first three characters: oxy
185-
last three characters: gen</code></pre>
186-
<p>What is the value of <code>element[:4]</code>? What about <code>element[4:]</code>? Or <code>element[:]</code>?</p>
187-
<p>What is <code>element[-1]</code>? What is <code>element[-2]</code>? Given those answers, explain what <code>element[1:-1]</code> does.</p>
188-
</div>
189-
</section>
190-
<section class="challenge panel panel-success">
191-
<div class="panel-heading">
192-
<h2 id="thin-slices"><span class="glyphicon glyphicon-pencil"></span>Thin slices</h2>
193-
</div>
194-
<div class="panel-body">
195-
<p>The expression <code>element[3:3]</code> produces an <strong>empty string</strong>, i.e., a string that contains no characters. If <code>data</code> holds our array of patient data, what does <code>data[3:3, 4:4]</code> produce? What about <code>data[3:3, :]</code>?</p>
196-
</div>
197-
</section>
198148
<p>Arrays also know how to perform common mathematical operations on their values. The simplest operations with data are arithmetic: add, subtract, multiply, and divide. When you do such operations on arrays, the operation is done on each individual element of the array. Thus:</p>
199149
<pre class="sourceCode python"><code class="sourceCode python">doubledata = data * <span class="fl">2.0</span></code></pre>
200150
<p>will create a new array <code>doubledata</code> whose elements have the value of two times the value of the corresponding elements in <code>data</code>:</p>
@@ -239,8 +189,7 @@ <h2 id="thin-slices"><span class="glyphicon glyphicon-pencil"></span>Thin slices
239189
<pre class="output"><code>maximum inflammation for patient 2: 19.0</code></pre>
240190
<p>What if we need the maximum inflammation for <em>all</em> patients (as in the next diagram on the left), or the average for each day (as in the diagram on the right)? As the diagram below shows, we want to perform the operation across an axis:</p>
241191
<div class="figure">
242-
<img src="fig/python-operations-across-axes.svg" alt="Operations Across Axes" />
243-
<p class="caption">Operations Across Axes</p>
192+
<img src="fig/python-operations-across-axes.svg" alt="Operations Across Axes" /><p class="caption">Operations Across Axes</p>
244193
</div>
245194
<p>To support this, most array methods allow us to specify the axis we want to work on. If we ask for the average across axis 0 (rows in our 2D example), we get:</p>
246195
<pre class="sourceCode python"><code class="sourceCode python"><span class="dt">print</span> data.mean(axis=<span class="dv">0</span>)</code></pre>
@@ -269,29 +218,25 @@ <h2 id="thin-slices"><span class="glyphicon glyphicon-pencil"></span>Thin slices
269218
image = matplotlib.pyplot.imshow(data)
270219
matplotlib.pyplot.show(image)</code></pre>
271220
<div class="figure">
272-
<img src="fig/01-numpy_71_0.png" alt="Heatmap of the Data" />
273-
<p class="caption">Heatmap of the Data</p>
221+
<img src="fig/01-numpy_71_0.png" alt="Heatmap of the Data" /><p class="caption">Heatmap of the Data</p>
274222
</div>
275223
<p>Blue regions in this heat map are low values, while red shows high values. As we can see, inflammation rises and falls over a 40-day period. Let’s take a look at the average inflammation over time:</p>
276224
<pre class="sourceCode python"><code class="sourceCode python">ave_inflammation = data.mean(axis=<span class="dv">0</span>)
277225
ave_plot = pyplot.plot(ave_inflammation)
278226
pyplot.show(ave_plot)</code></pre>
279227
<div class="figure">
280-
<img src="fig/01-numpy_73_0.png" alt="Average Inflammation Over Time" />
281-
<p class="caption">Average Inflammation Over Time</p>
228+
<img src="fig/01-numpy_73_0.png" alt="Average Inflammation Over Time" /><p class="caption">Average Inflammation Over Time</p>
282229
</div>
283230
<p>Here, we have put the average per day across all patients in the variable <code>ave_inflammation</code>, then asked <code>pyplot</code> to create and display a line graph of those values. The result is roughly a linear rise and fall, which is suspicious: based on other studies, we expect a sharper rise and slower fall. Let’s have a look at two other statistics:</p>
284231
<pre class="sourceCode python"><code class="sourceCode python">max_plot = pyplot.plot(data.<span class="dt">max</span>(axis=<span class="dv">0</span>))
285232
pyplot.show(max_plot)</code></pre>
286233
<div class="figure">
287-
<img src="fig/01-numpy_75_1.png" alt="Maximum Value Along The First Axis" />
288-
<p class="caption">Maximum Value Along The First Axis</p>
234+
<img src="fig/01-numpy_75_1.png" alt="Maximum Value Along The First Axis" /><p class="caption">Maximum Value Along The First Axis</p>
289235
</div>
290236
<pre class="sourceCode python"><code class="sourceCode python">min_plot = pyplot.plot(data.<span class="dt">min</span>(axis=<span class="dv">0</span>))
291237
pyplot.show(min_plot)</code></pre>
292238
<div class="figure">
293-
<img src="fig/01-numpy_75_3.png" alt="Minimum Value Along The First Axis" />
294-
<p class="caption">Minimum Value Along The First Axis</p>
239+
<img src="fig/01-numpy_75_3.png" alt="Minimum Value Along The First Axis" /><p class="caption">Minimum Value Along The First Axis</p>
295240
</div>
296241
<p>The maximum value rises and falls perfectly smoothly, while the minimum seems to be a step function. Neither result seems particularly likely, so either there’s a mistake in our calculations or something is wrong with our data.</p>
297242
<p>You can group similar plots in a single figure using subplots. This script below uses a number of new commands. The function <code>matplotlib.pyplot.figure()</code> creates a space into which we will place all of our plots. The parameter <code>figsize</code> tells Python how big to make this space. Each subplot is placed into the figure using the <code>subplot</code> command. The <code>subplot</code> command takes 3 parameters. The first denotes how many total rows of subplots there are, the second parameter refers to the total number of subplot columns, and the final parameters denotes which subplot your variable is referencing. Each subplot is stored in a different variable (axes1, axes2, axes3). Once a subplot is created, the axes are can be titled using the <code>set_xlabel()</code> command (or <code>set_ylabel()</code>). Here are our three plots side by side:</p>
@@ -319,21 +264,20 @@ <h2 id="thin-slices"><span class="glyphicon glyphicon-pencil"></span>Thin slices
319264

320265
matplotlib.pyplot.show(fig)</code></pre>
321266
<div class="figure">
322-
<img src="fig/01-numpy_80_0.png" alt="The Previous Plots as Subplots" />
323-
<p class="caption">The Previous Plots as Subplots</p>
267+
<img src="fig/01-numpy_80_0.png" alt="The Previous Plots as Subplots" /><p class="caption">The Previous Plots as Subplots</p>
324268
</div>
325269
<p>The <a href="reference.html#function-call">call</a> to <code>loadtxt</code> reads our data, and the rest of the program tells the plotting library how large we want the figure to be, that we’re creating three sub-plots, what to draw for each one, and that we want a tight layout. (Perversely, if we leave out that call to <code>fig.tight_layout()</code>, the graphs will actually be squeezed together more closely.)</p>
326270
<aside class="callout panel panel-info">
327271
<div class="panel-heading">
328-
<h2 id="scientists-dislike-typing"><span class="glyphicon glyphicon-pushpin"></span>Scientists dislike typing</h2>
272+
<h2><span class="glyphicon glyphicon-pushpin"></span>Scientists dislike typing</h2>
329273
</div>
330274
<div class="panel-body">
331275
<p>We will always use the syntax <code>import numpy</code> to import NumPy. However, in order to save typing, it is <a href="http://www.scipy.org/getting-started.html#an-example-script">often suggested</a> to make a shortcut like so: <code>import numpy as np</code>. If you ever see Python code online using a NumPy function with <code>np</code> (for example, <code>np.loadtxt(...)</code>), it’s because they’ve used this shortcut.</p>
332276
</div>
333277
</aside>
334278
<section class="challenge panel panel-success">
335279
<div class="panel-heading">
336-
<h2 id="check-your-understanding"><span class="glyphicon glyphicon-pencil"></span>Check your understanding</h2>
280+
<h2><span class="glyphicon glyphicon-pencil"></span>Check your understanding</h2>
337281
</div>
338282
<div class="panel-body">
339283
<p>Draw diagrams showing what variables refer to what values after each statement in the following program:</p>
@@ -345,7 +289,7 @@ <h2 id="check-your-understanding"><span class="glyphicon glyphicon-pencil"></spa
345289
</section>
346290
<section class="challenge panel panel-success">
347291
<div class="panel-heading">
348-
<h2 id="sorting-out-references-1"><span class="glyphicon glyphicon-pencil"></span>Sorting out references</h2>
292+
<h2><span class="glyphicon glyphicon-pencil"></span>Sorting out references</h2>
349293
</div>
350294
<div class="panel-body">
351295
<p>What does the following program print out?</p>
@@ -356,7 +300,7 @@ <h2 id="sorting-out-references-1"><span class="glyphicon glyphicon-pencil"></spa
356300
</section>
357301
<section class="challenge panel panel-success">
358302
<div class="panel-heading">
359-
<h2 id="slicing-strings-1"><span class="glyphicon glyphicon-pencil"></span>Slicing strings</h2>
303+
<h2><span class="glyphicon glyphicon-pencil"></span>Slicing strings</h2>
360304
</div>
361305
<div class="panel-body">
362306
<p>A section of an array is called a <a href="reference.html#slice">slice</a>. We can take slices of character strings as well:</p>
@@ -371,39 +315,39 @@ <h2 id="slicing-strings-1"><span class="glyphicon glyphicon-pencil"></span>Slici
371315
</section>
372316
<section class="challenge panel panel-success">
373317
<div class="panel-heading">
374-
<h2 id="thin-slices-1"><span class="glyphicon glyphicon-pencil"></span>Thin slices</h2>
318+
<h2><span class="glyphicon glyphicon-pencil"></span>Thin slices</h2>
375319
</div>
376320
<div class="panel-body">
377321
<p>The expression <code>element[3:3]</code> produces an <a href="reference.html#empty-string">empty string</a>, i.e., a string that contains no characters. If <code>data</code> holds our array of patient data, what does <code>data[3:3, 4:4]</code> produce? What about <code>data[3:3, :]</code>?</p>
378322
</div>
379323
</section>
380324
<section class="challenge panel panel-success">
381325
<div class="panel-heading">
382-
<h2 id="check-your-understanding-plot-scaling"><span class="glyphicon glyphicon-pencil"></span>Check your understanding: plot scaling</h2>
326+
<h2><span class="glyphicon glyphicon-pencil"></span>Check your understanding: plot scaling</h2>
383327
</div>
384328
<div class="panel-body">
385329
<p>Why do all of our plots stop just short of the upper end of our graph?</p>
386330
</div>
387331
</section>
388332
<section class="challenge panel panel-success">
389333
<div class="panel-heading">
390-
<h2 id="check-your-understanding-drawing-straight-lines"><span class="glyphicon glyphicon-pencil"></span>Check your understanding: drawing straight lines</h2>
334+
<h2><span class="glyphicon glyphicon-pencil"></span>Check your understanding: drawing straight lines</h2>
391335
</div>
392336
<div class="panel-body">
393337
<p>Why are the vertical lines in our plot of the minimum inflammation per day not perfectly vertical?</p>
394338
</div>
395339
</section>
396340
<section class="challenge panel panel-success">
397341
<div class="panel-heading">
398-
<h2 id="make-your-own-plot"><span class="glyphicon glyphicon-pencil"></span>Make your own plot</h2>
342+
<h2><span class="glyphicon glyphicon-pencil"></span>Make your own plot</h2>
399343
</div>
400344
<div class="panel-body">
401345
<p>Create a plot showing the standard deviation (<code>numpy.std</code>) of the inflammation data for each day across all patients.</p>
402346
</div>
403347
</section>
404348
<section class="challenge panel panel-success">
405349
<div class="panel-heading">
406-
<h2 id="moving-plots-around"><span class="glyphicon glyphicon-pencil"></span>Moving plots around</h2>
350+
<h2><span class="glyphicon glyphicon-pencil"></span>Moving plots around</h2>
407351
</div>
408352
<div class="panel-body">
409353
<p>Modify the program to display the three plots on top of one another instead of side by side.</p>

0 commit comments

Comments
 (0)