Added exercises
This commit is contained in:
42
README.html
42
README.html
@@ -34,7 +34,8 @@ is:</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>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
|
<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>
|
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>
|
<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>
|
||||||
<div class="section" id="neophyte">
|
<div class="section" id="neophyte">
|
||||||
<h1>Neophyte</h1>
|
<h1>Neophyte</h1>
|
||||||
<ol class="arabic">
|
<ol class="arabic">
|
||||||
@@ -257,16 +258,12 @@ point by point distances</p>
|
|||||||
<span class="keyword">print</span> <span class="name">G</span>
|
<span class="keyword">print</span> <span class="name">G</span>
|
||||||
</pre>
|
</pre>
|
||||||
</li>
|
</li>
|
||||||
<li><p class="first">Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3
|
<li><p class="first">How to tell if a given 2D array has null columns ?</p>
|
||||||
consecutive zeros interleaved between each value ?</p>
|
|
||||||
<pre class="code python literal-block">
|
<pre class="code python literal-block">
|
||||||
<span class="comment"># Author: Warren Weckesser</span>
|
<span class="comment"># Author: Warren Weckesser</span>
|
||||||
|
|
||||||
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">([</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">])</span>
|
<span class="name">Z</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">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,(</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">))</span>
|
||||||
<span class="name">nz</span> <span class="operator">=</span> <span class="literal number integer">3</span>
|
<span class="keyword">print</span> <span class="punctuation">(</span><span class="operator">~</span><span class="name">Z</span><span class="operator">.</span><span class="name">any</span><span class="punctuation">(</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">0</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">any</span><span class="punctuation">()</span>
|
||||||
<span class="name">Z0</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">)</span> <span class="operator">+</span> <span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">)</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">)</span><span class="operator">*</span><span class="punctuation">(</span><span class="name">nz</span><span class="punctuation">))</span>
|
|
||||||
<span class="name">Z0</span><span class="punctuation">[::</span><span class="name">nz</span><span class="operator">+</span><span class="literal number integer">1</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">Z</span>
|
|
||||||
<span class="keyword">print</span> <span class="name">Z0</span>
|
|
||||||
</pre>
|
</pre>
|
||||||
</li>
|
</li>
|
||||||
<li><p class="first">Find the nearest value from a given value in an array</p>
|
<li><p class="first">Find the nearest value from a given value in an array</p>
|
||||||
@@ -357,6 +354,35 @@ using a vector S of same size describing subset indices ?</p>
|
|||||||
<span class="keyword">print</span> <span class="name">D_means</span>
|
<span class="keyword">print</span> <span class="name">D_means</span>
|
||||||
</pre>
|
</pre>
|
||||||
</li>
|
</li>
|
||||||
|
<li><p class="first">Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3
|
||||||
|
consecutive zeros interleaved between each value ?</p>
|
||||||
|
<pre class="code python literal-block">
|
||||||
|
<span class="comment"># Author: Warren Weckesser</span>
|
||||||
|
|
||||||
|
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">([</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">])</span>
|
||||||
|
<span class="name">nz</span> <span class="operator">=</span> <span class="literal number integer">3</span>
|
||||||
|
<span class="name">Z0</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">)</span> <span class="operator">+</span> <span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">)</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">)</span><span class="operator">*</span><span class="punctuation">(</span><span class="name">nz</span><span class="punctuation">))</span>
|
||||||
|
<span class="name">Z0</span><span class="punctuation">[::</span><span class="name">nz</span><span class="operator">+</span><span class="literal number integer">1</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">Z</span>
|
||||||
|
<span class="keyword">print</span> <span class="name">Z0</span>
|
||||||
|
</pre>
|
||||||
|
</li>
|
||||||
|
<li><p class="first">Consider an array of dimension (5,5,3), how to mulitply it by an array with
|
||||||
|
dimensions (5,5) ?</p>
|
||||||
|
<pre class="code python literal-block">
|
||||||
|
<span class="name">A</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">((</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">))</span>
|
||||||
|
<span class="name">B</span> <span class="operator">=</span> <span class="literal number integer">2</span><span class="operator">*</span><span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">((</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">))</span>
|
||||||
|
<span class="keyword">print</span> <span class="name">A</span> <span class="operator">*</span> <span class="name">B</span><span class="punctuation">[:,:,</span><span class="name builtin pseudo">None</span><span class="punctuation">]</span>
|
||||||
|
</pre>
|
||||||
|
</li>
|
||||||
|
<li><p class="first">How to swap two rows of an array ?</p>
|
||||||
|
<pre class="code python literal-block">
|
||||||
|
<span class="comment"># Author: Eelco Hoogendoorn</span>
|
||||||
|
|
||||||
|
<span class="name">A</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">25</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">reshape</span><span class="punctuation">(</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">)</span>
|
||||||
|
<span class="name">A</span><span class="punctuation">[[</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">]]</span> <span class="operator">=</span> <span class="name">A</span><span class="punctuation">[[</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">]]</span>
|
||||||
|
<span class="keyword">print</span> <span class="name">A</span>
|
||||||
|
</pre>
|
||||||
|
</li>
|
||||||
</ol>
|
</ol>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="craftsman">
|
<div class="section" id="craftsman">
|
||||||
|
|||||||
75
README.ipynb
75
README.ipynb
@@ -662,8 +662,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3\n",
|
"How to tell if a given 2D array has null columns ?\n"
|
||||||
"consecutive zeros interleaved between each value ?\n"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -672,11 +671,8 @@
|
|||||||
"input": [
|
"input": [
|
||||||
"# Author: Warren Weckesser\n",
|
"# Author: Warren Weckesser\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Z = np.array([1,2,3,4,5])\n",
|
"Z = np.random.randint(0,3,(3,10))\n",
|
||||||
"nz = 3\n",
|
"print (~Z.any(axis=0)).any()"
|
||||||
"Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))\n",
|
|
||||||
"Z0[::nz+1] = Z\n",
|
|
||||||
"print Z0"
|
|
||||||
],
|
],
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@@ -880,6 +876,71 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": []
|
"outputs": []
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3\n",
|
||||||
|
"consecutive zeros interleaved between each value ?\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"# Author: Warren Weckesser\n",
|
||||||
|
"\n",
|
||||||
|
"Z = np.array([1,2,3,4,5])\n",
|
||||||
|
"nz = 3\n",
|
||||||
|
"Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))\n",
|
||||||
|
"Z0[::nz+1] = Z\n",
|
||||||
|
"print Z0"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Consider an array of dimension (5,5,3), how to mulitply it by an array\n",
|
||||||
|
"with dimensions (5,5) ?\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"A = np.ones((5,5,3))\n",
|
||||||
|
"B = 2*np.ones((5,5))\n",
|
||||||
|
"print A * B[:,:,None]"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"How to swap two rows of an array ?\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [
|
||||||
|
"# Author: Eelco Hoogendoorn\n",
|
||||||
|
"\n",
|
||||||
|
"A = np.arange(25).reshape(5,5)\n",
|
||||||
|
"A[[0,1]] = A[[1,0]]\n",
|
||||||
|
"print A"
|
||||||
|
],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "heading",
|
"cell_type": "heading",
|
||||||
"level": 2,
|
"level": 2,
|
||||||
|
|||||||
50
README.rst
50
README.rst
@@ -34,7 +34,8 @@ from the github repo, thanks to the `rst2ipynb
|
|||||||
<https://github.com/esc/rst2ipynb>`_ conversion tool by `Valentin Haenel
|
<https://github.com/esc/rst2ipynb>`_ conversion tool by `Valentin Haenel
|
||||||
<http://haenel.co>`_
|
<http://haenel.co>`_
|
||||||
|
|
||||||
Thanks to Michiaki Ariga, there is now a `Julia version <https://github.com/chezou/julia-100-exercises>`_.
|
Thanks to Michiaki Ariga, there is now a
|
||||||
|
`Julia version <https://github.com/chezou/julia-100-exercises>`_.
|
||||||
|
|
||||||
|
|
||||||
.. **Contents**
|
.. **Contents**
|
||||||
@@ -319,19 +320,14 @@ Apprentice
|
|||||||
print G
|
print G
|
||||||
|
|
||||||
|
|
||||||
9. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3
|
9. How to tell if a given 2D array has null columns ?
|
||||||
consecutive zeros interleaved between each value ?
|
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
# Author: Warren Weckesser
|
# Author: Warren Weckesser
|
||||||
|
|
||||||
Z = np.array([1,2,3,4,5])
|
Z = np.random.randint(0,3,(3,10))
|
||||||
nz = 3
|
print (~Z.any(axis=0)).any()
|
||||||
Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
|
|
||||||
Z0[::nz+1] = Z
|
|
||||||
print Z0
|
|
||||||
|
|
||||||
|
|
||||||
10. Find the nearest value from a given value in an array
|
10. Find the nearest value from a given value in an array
|
||||||
|
|
||||||
@@ -410,6 +406,7 @@ Journeyman
|
|||||||
n = len(np.unique(F))
|
n = len(np.unique(F))
|
||||||
print np.unique(I)
|
print np.unique(I)
|
||||||
|
|
||||||
|
|
||||||
6. Considering a four dimensions array, how to get sum over the last two axis at once ?
|
6. Considering a four dimensions array, how to get sum over the last two axis at once ?
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
@@ -435,6 +432,41 @@ Journeyman
|
|||||||
print D_means
|
print D_means
|
||||||
|
|
||||||
|
|
||||||
|
8. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3
|
||||||
|
consecutive zeros interleaved between each value ?
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
# Author: Warren Weckesser
|
||||||
|
|
||||||
|
Z = np.array([1,2,3,4,5])
|
||||||
|
nz = 3
|
||||||
|
Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
|
||||||
|
Z0[::nz+1] = Z
|
||||||
|
print Z0
|
||||||
|
|
||||||
|
|
||||||
|
9. Consider an array of dimension (5,5,3), how to mulitply it by an array with
|
||||||
|
dimensions (5,5) ?
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
A = np.ones((5,5,3))
|
||||||
|
B = 2*np.ones((5,5))
|
||||||
|
print A * B[:,:,None]
|
||||||
|
|
||||||
|
|
||||||
|
10. How to swap two rows of an array ?
|
||||||
|
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
# Author: Eelco Hoogendoorn
|
||||||
|
|
||||||
|
A = np.arange(25).reshape(5,5)
|
||||||
|
A[[0,1]] = A[[1,0]]
|
||||||
|
print A
|
||||||
|
|
||||||
|
|
||||||
Craftsman
|
Craftsman
|
||||||
=========
|
=========
|
||||||
|
|||||||
Reference in New Issue
Block a user