Added new exercices (Subtract the mean of each row of a matrix)

This commit is contained in:
Nicolas Rougier
2015-08-16 20:07:45 +02:00
parent 1e222512d5
commit 61f0bcc00b
2 changed files with 28 additions and 2 deletions

View File

@@ -28,8 +28,6 @@ is:</p>
<p>Here is what the page looks like so far:
<a class="reference external" href="http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html">http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html</a></p>
<p>Repository is at: <a class="reference external" href="https://github.com/rougier/numpy-100">https://github.com/rougier/numpy-100</a></p>
<p>The corresponding <a class="reference external" href="https://github.com/rougier/numpy-100/blob/master/README.ipynb">IPython notebook</a> is available
from the github repo, thanks to the <a class="reference external" href="https://github.com/esc/rst2ipynb">rst2ipynb</a> conversion tool by <a class="reference external" href="http://haenel.co">Valentin Haenel</a></p>
<p>Thanks to Michiaki Ariga, there is now a
<a class="reference external" href="https://github.com/chezou/julia-100-exercises">Julia version</a>.</p>
<ol class="arabic">
@@ -246,6 +244,19 @@ point by point distances</p>
<span class="keyword">print</span> <span class="name">G</span>
</pre>
</li>
<li><p class="first">Subtract the mean of each row of a matrix</p>
<pre class="code python literal-block">
<span class="comment"># Author: Warren Weckesser</span>
<span class="name">X</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">rand</span><span class="punctuation">(</span><span class="literal number integer">5</span><span class="punctuation">,</span> <span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="comment"># Recent versions of numpy</span>
<span class="name">Y</span> <span class="operator">=</span> <span class="name">X</span> <span class="operator">-</span> <span class="name">X</span><span class="operator">.</span><span class="name">mean</span><span class="punctuation">(</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">,</span> <span class="name">keepdims</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">)</span>
<span class="comment"># Older versions of numpy</span>
<span class="name">Y</span> <span class="operator">=</span> <span class="name">X</span> <span class="operator">-</span> <span class="name">X</span><span class="operator">.</span><span class="name">mean</span><span class="punctuation">(</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">reshape</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">)</span>
</pre>
</li>
<li><p class="first">How to tell if a given 2D array has null columns ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Warren Weckesser</span>

View File

@@ -293,6 +293,21 @@ Thanks to Michiaki Ariga, there is now a
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )
print G
#. Subtract the mean of each row of a matrix
.. code-block:: python
# Author: Warren Weckesser
X = np.random.rand(5, 10)
# Recent versions of numpy
Y = X - X.mean(axis=1, keepdims=True)
# Older versions of numpy
Y = X - X.mean(axis=1).reshape(-1, 1)
#. How to tell if a given 2D array has null columns ?