Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 01-numpy.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<h1 class="title">Programming with Python</h1>
<h2 class="subtitle">Analyzing Patient Data</h2>
<div id="learning-objectives" class="objectives objectives">
<h2>Learning Objectives</h2>
<h2 id="learning-objectives" class="objectives objectives">Learning Objectives</h2>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to change with this PR? I know it is happening automatically, but could you move it to a different commit?

<ul>
<li>Explain what a library is, and what libraries are used for.</li>
<li>Load a Python library and use the things it contains.</li>
Expand All @@ -43,7 +43,7 @@ <h2>Learning Objectives</h2>
<p>In order to load our inflammation data, we need to <strong>import</strong> a library called NumPy. In general you should use this library if you want to do fancy things with numbers, especially if you have matrices. We can load NumPy using:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="ch">import</span> numpy</code></pre>
<p>Importing a library is like getting a piece of lab equipment out of a storage locker and setting it up on the bench. Once it's done, we can ask the library to read our data file for us:</p>
<pre class="sourceCode python"><code class="sourceCode python">numpy.loadtxt(fname=&amp;<span class="co">#39;inflammation-01.csv&amp;#39;, delimiter=&amp;#39;,&amp;#39;)</span></code></pre>
<pre class="sourceCode python"><code class="sourceCode python">numpy.loadtxt(fname=<span class="st">&#39;inflammation-01.csv&#39;</span>, delimiter=<span class="st">&#39;,&#39;</span>)</code></pre>
<pre class="output"><code>array([[ 0., 0., 1., ..., 3., 0., 0.],
[ 0., 1., 2., ..., 1., 0., 1.],
[ 0., 1., 1., ..., 2., 1., 1.],
Expand Down Expand Up @@ -93,7 +93,7 @@ <h2>Learning Objectives</h2>
[ 0. 0. 1. ..., 1. 1. 0.]]</code></pre>
<p>Now that our data is in memory, we can start doing things with it. First, let's ask what <strong>type</strong> of thing <code>data</code> refers to:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="dt">print</span> <span class="dt">type</span>(data)</code></pre>
<pre class="output"><code>&amp;lt;type &#39;numpy.ndarray&#39;&amp;gt;</code></pre>
<pre class="output"><code>&lt;type &#39;numpy.ndarray&#39;&gt;</code></pre>
<p>The output tells us that <code>data</code> currently refers to an N-dimensional array created by the NumPy library. We can see what its <strong>shape</strong> is like this:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="dt">print</span> data.shape</code></pre>
<pre class="output"><code>(60, 40)</code></pre>
Expand Down
4 changes: 2 additions & 2 deletions 01-numpy.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Once it's done,
we can ask the library to read our data file for us:

~~~ {.python}
numpy.loadtxt(fname=&#39;inflammation-01.csv&#39;, delimiter=&#39;,&#39;)
numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
~~~
~~~ {.output}
array([[ 0., 0., 1., ..., 3., 0., 0.],
Expand Down Expand Up @@ -189,7 +189,7 @@ let's ask what **type** of thing `data` refers to:
print type(data)
~~~
~~~ {.output}
&lt;type 'numpy.ndarray'&gt;
<type 'numpy.ndarray'>
~~~

The output tells us that `data` currently refers to an N-dimensional array created by the NumPy library.
Expand Down
38 changes: 19 additions & 19 deletions 02-func.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ <h2 id="the-call-stack">The Call Stack</h2>
<pre class="sourceCode python"><code class="sourceCode python"><span class="dt">print</span> <span class="st">&#39;final value of temp after all function calls:&#39;</span>, temp</code></pre>
<pre class="error"><code>---------------------------------------------------------------------------
NameError Traceback (most recent call last)
&amp;lt;ipython-input-12-ffd9b4dbd5f1&amp;gt; in &amp;lt;module&amp;gt;()
----&amp;gt; 1 print &#39;final value of temp after all function calls:&#39;, temp
&lt;ipython-input-12-ffd9b4dbd5f1&gt; in &lt;module&gt;()
----&gt; 1 print &#39;final value of temp after all function calls:&#39;, temp

NameError: name &#39;temp&#39; is not defined</code></pre>
<pre class="output"><code>final value of temp after all function calls:</code></pre>
Expand Down Expand Up @@ -195,15 +195,15 @@ <h2 id="testing-and-documenting">Testing and Documenting</h2>
<p>A string like this is called a <strong>docstring</strong>. We don't need to use triple quotes when we write one, but if we do, we can break the string across multiple lines:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> center(data, desired):
<span class="co">&#39;&#39;&#39;Return a new array containing the original data centered around the desired value.</span>
<span class="co"> Example: center([1, 2, 3], 0) =&amp;gt; [-1, 0, 1]&#39;&#39;&#39;</span>
<span class="co"> Example: center([1, 2, 3], 0) =&gt; [-1, 0, 1]&#39;&#39;&#39;</span>
<span class="kw">return</span> (data - data.mean()) + desired

<span class="dt">help</span>(center)</code></pre>
<pre class="output"><code>Help on function center in module __main__:

center(data, desired)
Return a new array containing the original data centered around the desired value.
Example: center([1, 2, 3], 0) =&amp;gt; [-1, 0, 1]
Example: center([1, 2, 3], 0) =&gt; [-1, 0, 1]
</code></pre>
<h2 id="defining-defaults">Defining Defaults</h2>
<p>We have passed parameters to functions in two ways: directly, as in <code>span(data)</code>, and by name, as in <code>numpy.loadtxt(fname='something.csv', delimiter=',')</code>. In fact, we can pass the filename to <code>loadtxt</code> without the <code>fname=</code>:</p>
Expand All @@ -213,21 +213,21 @@ <h2 id="defining-defaults">Defining Defaults</h2>
<pre class="sourceCode python"><code class="sourceCode python">numpy.loadtxt(<span class="st">&#39;inflammation-01.csv&#39;</span>, <span class="st">&#39;,&#39;</span>)</code></pre>
<pre class="error"><code>---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
&amp;lt;ipython-input-26-e3bc6cf4fd6a&amp;gt; in &amp;lt;module&amp;gt;()
----&amp;gt; 1 numpy.loadtxt(&#39;inflammation-01.csv&#39;, &#39;,&#39;)
&lt;ipython-input-26-e3bc6cf4fd6a&gt; in &lt;module&gt;()
----&gt; 1 numpy.loadtxt(&#39;inflammation-01.csv&#39;, &#39;,&#39;)

/Users/gwilson/anaconda/lib/python2.7/site-packages/numpy/lib/npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
775 try:
776 # Make sure we&#39;re dealing with a proper dtype
--&amp;gt; 777 dtype = np.dtype(dtype)
--&gt; 777 dtype = np.dtype(dtype)
778 defconv = _getconv(dtype)
779

TypeError: data type &quot;,&quot; not understood</code></pre>
<p>To understand what's going on, and make our own functions easier to use, let's re-define our <code>center</code> function like this:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> center(data, desired=<span class="fl">0.0</span>):
<span class="co">&#39;&#39;&#39;Return a new array containing the original data centered around the desired value (0 by default).</span>
<span class="co"> Example: center([1, 2, 3], 0) =&amp;gt; [-1, 0, 1]&#39;&#39;&#39;</span>
<span class="co"> Example: center([1, 2, 3], 0) =&gt; [-1, 0, 1]&#39;&#39;&#39;</span>
<span class="kw">return</span> (data - data.mean()) + desired</code></pre>
<p>The key change is that the second parameter is now written <code>desired=0.0</code> instead of just <code>desired</code>. If we call the function with two arguments, it works as it did before:</p>
<pre class="sourceCode python"><code class="sourceCode python">test_data = numpy.zeros((<span class="dv">2</span>, <span class="dv">2</span>))
Expand Down Expand Up @@ -267,7 +267,7 @@ <h2 id="defining-defaults">Defining Defaults</h2>
<pre class="sourceCode python"><code class="sourceCode python"><span class="dt">help</span>(numpy.loadtxt)</code></pre>
<pre class="output"><code>Help on function loadtxt in module numpy.lib.npyio:

loadtxt(fname, dtype=&amp;lt;type &#39;float&#39;&amp;gt;, comments=&#39;#&#39;, delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
loadtxt(fname, dtype=&lt;type &#39;float&#39;&gt;, comments=&#39;#&#39;, delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
Load data from a text file.

Each row in the text file must have the same number of values.
Expand Down Expand Up @@ -331,23 +331,23 @@ <h2 id="defining-defaults">Defining Defaults</h2>

Examples
--------
&amp;gt;&amp;gt;&amp;gt; from StringIO import StringIO # StringIO behaves like a file object
&amp;gt;&amp;gt;&amp;gt; c = StringIO(&quot;0 1\n2 3&quot;)
&amp;gt;&amp;gt;&amp;gt; np.loadtxt(c)
&gt;&gt;&gt; from StringIO import StringIO # StringIO behaves like a file object
&gt;&gt;&gt; c = StringIO(&quot;0 1\n2 3&quot;)
&gt;&gt;&gt; np.loadtxt(c)
array([[ 0., 1.],
[ 2., 3.]])

&amp;gt;&amp;gt;&amp;gt; d = StringIO(&quot;M 21 72\nF 35 58&quot;)
&amp;gt;&amp;gt;&amp;gt; np.loadtxt(d, dtype={&#39;names&#39;: (&#39;gender&#39;, &#39;age&#39;, &#39;weight&#39;),
&gt;&gt;&gt; d = StringIO(&quot;M 21 72\nF 35 58&quot;)
&gt;&gt;&gt; np.loadtxt(d, dtype={&#39;names&#39;: (&#39;gender&#39;, &#39;age&#39;, &#39;weight&#39;),
... &#39;formats&#39;: (&#39;S1&#39;, &#39;i4&#39;, &#39;f4&#39;)})
array([(&#39;M&#39;, 21, 72.0), (&#39;F&#39;, 35, 58.0)],
dtype=[(&#39;gender&#39;, &#39;|S1&#39;), (&#39;age&#39;, &#39;&amp;lt;i4&#39;), (&#39;weight&#39;, &#39;&amp;lt;f4&#39;)])
dtype=[(&#39;gender&#39;, &#39;|S1&#39;), (&#39;age&#39;, &#39;&lt;i4&#39;), (&#39;weight&#39;, &#39;&lt;f4&#39;)])

&amp;gt;&amp;gt;&amp;gt; c = StringIO(&quot;1,0,2\n3,0,4&quot;)
&amp;gt;&amp;gt;&amp;gt; x, y = np.loadtxt(c, delimiter=&#39;,&#39;, usecols=(0, 2), unpack=True)
&amp;gt;&amp;gt;&amp;gt; x
&gt;&gt;&gt; c = StringIO(&quot;1,0,2\n3,0,4&quot;)
&gt;&gt;&gt; x, y = np.loadtxt(c, delimiter=&#39;,&#39;, usecols=(0, 2), unpack=True)
&gt;&gt;&gt; x
array([ 1., 3.])
&amp;gt;&amp;gt;&amp;gt; y
&gt;&gt;&gt; y
array([ 2., 4.])
</code></pre>
<p>There's a lot of information here, but the most important part is the first couple of lines:</p>
Expand Down
38 changes: 19 additions & 19 deletions 02-func.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ print 'final value of temp after all function calls:', temp
~~~ {.error}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
&lt;ipython-input-12-ffd9b4dbd5f1&gt; in &lt;module&gt;()
----&gt; 1 print 'final value of temp after all function calls:', temp
<ipython-input-12-ffd9b4dbd5f1> in <module>()
----> 1 print 'final value of temp after all function calls:', temp

NameError: name 'temp' is not defined
~~~
Expand Down Expand Up @@ -478,7 +478,7 @@ we can break the string across multiple lines:
~~~ {.python}
def center(data, desired):
'''Return a new array containing the original data centered around the desired value.
Example: center([1, 2, 3], 0) =&gt; [-1, 0, 1]'''
Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
return (data - data.mean()) + desired

help(center)
Expand All @@ -488,7 +488,7 @@ Help on function center in module __main__:

center(data, desired)
Return a new array containing the original data centered around the desired value.
Example: center([1, 2, 3], 0) =&gt; [-1, 0, 1]
Example: center([1, 2, 3], 0) => [-1, 0, 1]

~~~

Expand Down Expand Up @@ -520,13 +520,13 @@ numpy.loadtxt('inflammation-01.csv', ',')
~~~ {.error}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
&lt;ipython-input-26-e3bc6cf4fd6a&gt; in &lt;module&gt;()
----&gt; 1 numpy.loadtxt('inflammation-01.csv', ',')
<ipython-input-26-e3bc6cf4fd6a> in <module>()
----> 1 numpy.loadtxt('inflammation-01.csv', ',')

/Users/gwilson/anaconda/lib/python2.7/site-packages/numpy/lib/npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
775 try:
776 # Make sure we're dealing with a proper dtype
--&gt; 777 dtype = np.dtype(dtype)
--> 777 dtype = np.dtype(dtype)
778 defconv = _getconv(dtype)
779

Expand All @@ -540,7 +540,7 @@ let's re-define our `center` function like this:
~~~ {.python}
def center(data, desired=0.0):
'''Return a new array containing the original data centered around the desired value (0 by default).
Example: center([1, 2, 3], 0) =&gt; [-1, 0, 1]'''
Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
return (data - data.mean()) + desired
~~~

Expand Down Expand Up @@ -622,7 +622,7 @@ help(numpy.loadtxt)
~~~ {.output}
Help on function loadtxt in module numpy.lib.npyio:

loadtxt(fname, dtype=&lt;type 'float'&gt;, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
Load data from a text file.

Each row in the text file must have the same number of values.
Expand Down Expand Up @@ -686,23 +686,23 @@ loadtxt(fname, dtype=&lt;type 'float'&gt;, comments='#', delimiter=None, convert

Examples
--------
&gt;&gt;&gt; from StringIO import StringIO # StringIO behaves like a file object
&gt;&gt;&gt; c = StringIO("0 1\n2 3")
&gt;&gt;&gt; np.loadtxt(c)
>>> from StringIO import StringIO # StringIO behaves like a file object
>>> c = StringIO("0 1\n2 3")
>>> np.loadtxt(c)
array([[ 0., 1.],
[ 2., 3.]])

&gt;&gt;&gt; d = StringIO("M 21 72\nF 35 58")
&gt;&gt;&gt; np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
>>> d = StringIO("M 21 72\nF 35 58")
>>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
... 'formats': ('S1', 'i4', 'f4')})
array([('M', 21, 72.0), ('F', 35, 58.0)],
dtype=[('gender', '|S1'), ('age', '&lt;i4'), ('weight', '&lt;f4')])
dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')])

&gt;&gt;&gt; c = StringIO("1,0,2\n3,0,4")
&gt;&gt;&gt; x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
&gt;&gt;&gt; x
>>> c = StringIO("1,0,2\n3,0,4")
>>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
>>> x
array([ 1., 3.])
&gt;&gt;&gt; y
>>> y
array([ 2., 4.])

~~~
Expand Down
4 changes: 2 additions & 2 deletions 03-loop.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ <h2 id="for-loops">For Loops</h2>
</ol>
<pre class="sourceCode python"><code class="sourceCode python">print_characters(<span class="st">&#39;tin&#39;</span>)</code></pre>
<h2 id="section" class="error"><sub>~</sub></h2>
<p>IndexError Traceback (most recent call last) &lt;ipython-input-13-5bc7311e0bf3&gt; in &lt;module&gt;() ----&gt; 1 print_characters('tin')</p>
<p>&lt;ipython-input-12-11460561ea56&gt; in print_characters(element) 3 print element[1] 4 print element[2] ----&gt; 5 print element[3] 6 7 print_characters('lead')</p>
<p>IndexError Traceback (most recent call last) <ipython-input-13-5bc7311e0bf3> in <module>() ----&gt; 1 print_characters('tin')</p>
<p><ipython-input-12-11460561ea56> in print_characters(element) 3 print element[1] 4 print element[2] ----&gt; 5 print element[3] 6 7 print_characters('lead')</p>
<p>IndexError: string index out of range <sub>~</sub> {.output} t i n <sub>~</sub></p>
<p>Here's a better approach:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> print_characters(element):
Expand Down
8 changes: 4 additions & 4 deletions 03-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ print_characters('tin')
~~~ {.error}
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
&lt;ipython-input-13-5bc7311e0bf3&gt; in &lt;module&gt;()
----&gt; 1 print_characters('tin')
<ipython-input-13-5bc7311e0bf3> in <module>()
----> 1 print_characters('tin')

&lt;ipython-input-12-11460561ea56&gt; in print_characters(element)
<ipython-input-12-11460561ea56> in print_characters(element)
3 print element[1]
4 print element[2]
----&gt; 5 print element[3]
----> 5 print element[3]
6
7 print_characters('lead')

Expand Down
Loading