You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>As the example above shows, we can print several things at once by separating them with commas.</p>
75
75
<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>
76
76
<divclass="figure">
77
-
<imgsrc="fig/python-sticky-note-variables-01.svg" alt="Variables as Sticky Notes" />
78
-
<pclass="caption">Variables as Sticky Notes</p>
77
+
<imgsrc="fig/python-sticky-note-variables-01.svg" alt="Variables as Sticky Notes" /><pclass="caption">Variables as Sticky Notes</p>
79
78
</div>
80
79
<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>
<spanclass="dt">print</span><spanclass="st">'weight in kilograms:'</span>, weight_kg, <spanclass="st">'and in pounds:'</span>, weight_lb</code></pre>
83
82
<preclass="output"><code>weight in kilograms: 57.5 and in pounds: 126.5</code></pre>
84
83
<divclass="figure">
85
-
<imgsrc="fig/python-sticky-note-variables-02.svg" alt="Creating Another Variable" />
86
-
<pclass="caption">Creating Another Variable</p>
84
+
<imgsrc="fig/python-sticky-note-variables-02.svg" alt="Creating Another Variable" /><pclass="caption">Creating Another Variable</p>
<spanclass="dt">print</span><spanclass="st">'weight in kilograms is now:'</span>, weight_kg, <spanclass="st">'and weight in pounds is still:'</span>, weight_lb</code></pre>
91
89
<preclass="output"><code>weight in kilograms is now: 100.0 and weight in pounds is still: 126.5</code></pre>
92
90
<divclass="figure">
93
-
<imgsrc="fig/python-sticky-note-variables-03.svg" alt="Updating a Variable" />
94
-
<pclass="caption">Updating a Variable</p>
91
+
<imgsrc="fig/python-sticky-note-variables-03.svg" alt="Updating a Variable" /><pclass="caption">Updating a Variable</p>
95
92
</div>
96
93
<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
-
<sectionclass="challenge panel panel-success">
98
-
<divclass="panel-heading">
99
-
<h2id="whats-inside-the-box"><spanclass="glyphicon glyphicon-pencil"></span>What’s inside the box?</h2>
100
-
</div>
101
-
<divclass="panel-body">
102
-
<p>Draw diagrams showing what variables refer to what values after each statement in the following program:</p>
<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>
<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>
<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>
145
118
<asideclass="callout panel panel-info">
146
119
<divclass="panel-heading">
147
-
<h2id="in-the-corner"><spanclass="glyphicon glyphicon-pushpin"></span>In the Corner</h2>
120
+
<h2><spanclass="glyphicon glyphicon-pushpin"></span>In the Corner</h2>
148
121
</div>
149
122
<divclass="panel-body">
150
123
<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
<spanclass="dt">print</span><spanclass="st">'first three characters:'</span>, element[<spanclass="dv">0</span>:<spanclass="dv">3</span>]
183
-
<spanclass="dt">print</span><spanclass="st">'last three characters:'</span>, element[<spanclass="dv">3</span>:<spanclass="dv">6</span>]</code></pre>
184
-
<preclass="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>
<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>
198
148
<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>
199
149
<preclass="sourceCode python"><codeclass="sourceCode python">doubledata = data * <spanclass="fl">2.0</span></code></pre>
200
150
<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>
<preclass="output"><code>maximum inflammation for patient 2: 19.0</code></pre>
240
190
<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>
241
191
<divclass="figure">
242
-
<imgsrc="fig/python-operations-across-axes.svg" alt="Operations Across Axes" />
243
-
<pclass="caption">Operations Across Axes</p>
192
+
<imgsrc="fig/python-operations-across-axes.svg" alt="Operations Across Axes" /><pclass="caption">Operations Across Axes</p>
244
193
</div>
245
194
<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>
<imgsrc="fig/01-numpy_71_0.png" alt="Heatmap of the Data" />
273
-
<pclass="caption">Heatmap of the Data</p>
221
+
<imgsrc="fig/01-numpy_71_0.png" alt="Heatmap of the Data" /><pclass="caption">Heatmap of the Data</p>
274
222
</div>
275
223
<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>
<imgsrc="fig/01-numpy_73_0.png" alt="Average Inflammation Over Time" />
281
-
<pclass="caption">Average Inflammation Over Time</p>
228
+
<imgsrc="fig/01-numpy_73_0.png" alt="Average Inflammation Over Time" /><pclass="caption">Average Inflammation Over Time</p>
282
229
</div>
283
230
<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>
<imgsrc="fig/01-numpy_75_3.png" alt="Minimum Value Along The First Axis" />
294
-
<pclass="caption">Minimum Value Along The First Axis</p>
239
+
<imgsrc="fig/01-numpy_75_3.png" alt="Minimum Value Along The First Axis" /><pclass="caption">Minimum Value Along The First Axis</p>
295
240
</div>
296
241
<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>
297
242
<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>
<imgsrc="fig/01-numpy_80_0.png" alt="The Previous Plots as Subplots" />
323
-
<pclass="caption">The Previous Plots as Subplots</p>
267
+
<imgsrc="fig/01-numpy_80_0.png" alt="The Previous Plots as Subplots" /><pclass="caption">The Previous Plots as Subplots</p>
324
268
</div>
325
269
<p>The <ahref="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>
<p>We will always use the syntax <code>import numpy</code> to import NumPy. However, in order to save typing, it is <ahref="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>
332
276
</div>
333
277
</aside>
334
278
<sectionclass="challenge panel panel-success">
335
279
<divclass="panel-heading">
336
-
<h2id="check-your-understanding"><spanclass="glyphicon glyphicon-pencil"></span>Check your understanding</h2>
280
+
<h2><spanclass="glyphicon glyphicon-pencil"></span>Check your understanding</h2>
337
281
</div>
338
282
<divclass="panel-body">
339
283
<p>Draw diagrams showing what variables refer to what values after each statement in the following program:</p>
<p>The expression <code>element[3:3]</code> produces an <ahref="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>
378
322
</div>
379
323
</section>
380
324
<sectionclass="challenge panel panel-success">
381
325
<divclass="panel-heading">
382
-
<h2id="check-your-understanding-plot-scaling"><spanclass="glyphicon glyphicon-pencil"></span>Check your understanding: plot scaling</h2>
326
+
<h2><spanclass="glyphicon glyphicon-pencil"></span>Check your understanding: plot scaling</h2>
383
327
</div>
384
328
<divclass="panel-body">
385
329
<p>Why do all of our plots stop just short of the upper end of our graph?</p>
386
330
</div>
387
331
</section>
388
332
<sectionclass="challenge panel panel-success">
389
333
<divclass="panel-heading">
390
-
<h2id="check-your-understanding-drawing-straight-lines"><spanclass="glyphicon glyphicon-pencil"></span>Check your understanding: drawing straight lines</h2>
334
+
<h2><spanclass="glyphicon glyphicon-pencil"></span>Check your understanding: drawing straight lines</h2>
391
335
</div>
392
336
<divclass="panel-body">
393
337
<p>Why are the vertical lines in our plot of the minimum inflammation per day not perfectly vertical?</p>
394
338
</div>
395
339
</section>
396
340
<sectionclass="challenge panel panel-success">
397
341
<divclass="panel-heading">
398
-
<h2id="make-your-own-plot"><spanclass="glyphicon glyphicon-pencil"></span>Make your own plot</h2>
342
+
<h2><spanclass="glyphicon glyphicon-pencil"></span>Make your own plot</h2>
399
343
</div>
400
344
<divclass="panel-body">
401
345
<p>Create a plot showing the standard deviation (<code>numpy.std</code>) of the inflammation data for each day across all patients.</p>
0 commit comments