Corrected recipes and added notebook

This commit is contained in:
Nicolas Rougier
2014-05-29 21:48:26 +02:00
parent 6d55b8bcac
commit 4cef439df0
3 changed files with 2411 additions and 137 deletions

View File

@@ -32,23 +32,8 @@ is:</p>
<p class="last">The level names came from an old-game (Dungeon Master)</p> <p class="last">The level names came from an old-game (Dungeon Master)</p>
</div> </div>
<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><strong>Contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#neophyte" id="id1">Neophyte</a></li>
<li><a class="reference internal" href="#novice" id="id2">Novice</a></li>
<li><a class="reference internal" href="#apprentice" id="id3">Apprentice</a></li>
<li><a class="reference internal" href="#journeyman" id="id4">Journeyman</a></li>
<li><a class="reference internal" href="#craftsman" id="id5">Craftsman</a></li>
<li><a class="reference internal" href="#artisan" id="id6">Artisan</a></li>
<li><a class="reference internal" href="#adept" id="id7">Adept</a></li>
<li><a class="reference internal" href="#expert" id="id8">Expert</a></li>
<li><a class="reference internal" href="#master" id="id9">Master</a></li>
<li><a class="reference internal" href="#archmaster" id="id10">Archmaster</a></li>
</ul>
</div>
<div class="section" id="neophyte"> <div class="section" id="neophyte">
<h1><a class="toc-backref" href="#id1">Neophyte</a></h1> <h1>Neophyte</h1>
<ol class="arabic"> <ol class="arabic">
<li><p class="first">Import the numpy package under the name <tt class="docutils literal">np</tt></p> <li><p class="first">Import the numpy package under the name <tt class="docutils literal">np</tt></p>
<pre class="code python literal-block"> <pre class="code python literal-block">
@@ -105,7 +90,7 @@ is:</p>
</ol> </ol>
</div> </div>
<div class="section" id="novice"> <div class="section" id="novice">
<h1><a class="toc-backref" href="#id2">Novice</a></h1> <h1>Novice</h1>
<ol class="arabic"> <ol class="arabic">
<li><p class="first">Create a 8x8 matrix and fill it with a checkerboard pattern</p> <li><p class="first">Create a 8x8 matrix and fill it with a checkerboard pattern</p>
<pre class="code python literal-block"> <pre class="code python literal-block">
@@ -170,7 +155,7 @@ is:</p>
</ol> </ol>
</div> </div>
<div class="section" id="apprentice"> <div class="section" id="apprentice">
<h1><a class="toc-backref" href="#id3">Apprentice</a></h1> <h1>Apprentice</h1>
<ol class="arabic"> <ol class="arabic">
<li><p class="first">Make an array immutable (read-only)</p> <li><p class="first">Make an array immutable (read-only)</p>
<pre class="code python literal-block"> <pre class="code python literal-block">
@@ -260,7 +245,7 @@ consecutive zeros interleaved between each value ?</p>
</ol> </ol>
</div> </div>
<div class="section" id="journeyman"> <div class="section" id="journeyman">
<h1><a class="toc-backref" href="#id4">Journeyman</a></h1> <h1>Journeyman</h1>
<ol class="arabic"> <ol class="arabic">
<li><p class="first">Consider the following file:</p> <li><p class="first">Consider the following file:</p>
<pre class="literal-block"> <pre class="literal-block">
@@ -336,7 +321,7 @@ using a vector S of same size describing subset indices ?</p>
</ol> </ol>
</div> </div>
<div class="section" id="craftsman"> <div class="section" id="craftsman">
<h1><a class="toc-backref" href="#id5">Craftsman</a></h1> <h1>Craftsman</h1>
<ol class="arabic"> <ol class="arabic">
<li><p class="first">Consider a one-dimensional array Z, build a two-dimensional array whose <li><p class="first">Consider a one-dimensional array Z, build a two-dimensional array whose
first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last
@@ -378,7 +363,7 @@ np.bincount(A) == C ?</p>
</ol> </ol>
</div> </div>
<div class="section" id="artisan"> <div class="section" id="artisan">
<h1><a class="toc-backref" href="#id6">Artisan</a></h1> <h1>Artisan</h1>
<ol class="arabic"> <ol class="arabic">
<li><p class="first">Considering a 100x3 matrix, extract rows with unequal values (e.g. [2,2,3])</p> <li><p class="first">Considering a 100x3 matrix, extract rows with unequal values (e.g. [2,2,3])</p>
<pre class="code python literal-block"> <pre class="code python literal-block">
@@ -406,7 +391,7 @@ np.bincount(A) == C ?</p>
</ol> </ol>
</div> </div>
<div class="section" id="adept"> <div class="section" id="adept">
<h1><a class="toc-backref" href="#id7">Adept</a></h1> <h1>Adept</h1>
<ol class="arabic"> <ol class="arabic">
<li><p class="first">Consider an arbitrary array, write a function that extract a subpart with a <li><p class="first">Consider an arbitrary array, write a function that extract a subpart with a
fixed shape and centered on a given element (pad with a <tt class="docutils literal">fill</tt> value when fixed shape and centered on a given element (pad with a <tt class="docutils literal">fill</tt> value when
@@ -451,7 +436,7 @@ array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]] ?</p>
</ol> </ol>
</div> </div>
<div class="section" id="expert"> <div class="section" id="expert">
<h1><a class="toc-backref" href="#id8">Expert</a></h1> <h1>Expert</h1>
<ol class="arabic"> <ol class="arabic">
<li><p class="first">Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A <li><p class="first">Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A
that contain elements of each row of B regardless of the order of the elements that contain elements of each row of B regardless of the order of the elements
@@ -498,7 +483,7 @@ in B ?</p>
</ol> </ol>
</div> </div>
<div class="section" id="master"> <div class="section" id="master">
<h1><a class="toc-backref" href="#id9">Master</a></h1> <h1>Master</h1>
<ol class="arabic"> <ol class="arabic">
<li><p class="first">Given a two dimensional array, how to extract unique rows ?</p> <li><p class="first">Given a two dimensional array, how to extract unique rows ?</p>
<div class="note"> <div class="note">
@@ -517,7 +502,7 @@ in B ?</p>
</ol> </ol>
</div> </div>
<div class="section" id="archmaster"> <div class="section" id="archmaster">
<h1><a class="toc-backref" href="#id10">Archmaster</a></h1> <h1>Archmaster</h1>
</div> </div>
</div> </div>
</body> </body>

2233
README.ipynb Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -8,7 +8,9 @@ A joint effort of the numpy community
The goal is both to offer a quick reference for new and old users and to The goal is both to offer a quick reference for new and old users and to
provide also a set of exercices for those who teach. If you remember having provide also a set of exercices for those who teach. If you remember having
asked or answered a (short) problem, you can send a pull request. The format asked or answered a (short) problem, you can send a pull request. The format
is:: is:
::
#. Find indices of non-zero elements from [1,2,0,0,4,0] #. Find indices of non-zero elements from [1,2,0,0,4,0]
@@ -22,18 +24,17 @@ is::
Here is what the page looks like so far: Here is what the page looks like so far:
http://www.loria.fr/~rougier/teaching/numpy.100/index.html http://www.loria.fr/~rougier/teaching/numpy.100/index.html
.. Note:: The level names came from an old-game (Dungeon Master) .. Note:: The level names came from an old-game (Dungeon Master)
Repository is at: https://github.com/rougier/numpy-100 The IPython notebook is
available from the github repo, thanks to the `rst2ipynb
<https://github.com/esc/rst2ipynb>`_ conversion tool by `Valentin Haenel
<http://haenel.co>`_
Repository is at: https://github.com/rougier/numpy-100 .. **Contents**
.. .. contents::
.. :local:
**Contents** .. :depth: 1
.. contents::
:local:
:depth: 1
Neophyte Neophyte
@@ -41,14 +42,14 @@ Neophyte
1. Import the numpy package under the name ``np`` 1. Import the numpy package under the name ``np``
.. code:: python .. code-block:: python
import numpy as np import numpy as np
2. Print the numpy version and the configuration. 2. Print the numpy version and the configuration.
.. code:: python .. code-block:: python
print np.__version__ print np.__version__
np.__config__.show() np.__config__.show()
@@ -56,129 +57,160 @@ Neophyte
3. Create a null vector of size 10 3. Create a null vector of size 10
.. code:: python .. code-block:: python
Z = np.zeros(10) Z = np.zeros(10)
print Z
4. Create a null vector of size 10 but the fifth value which is 1 4. Create a null vector of size 10 but the fifth value which is 1
.. code:: python .. code-block:: python
Z = np.zeros(10) Z = np.zeros(10)
Z[4] = 1 Z[4] = 1
print Z
5. Create a vector with values ranging from 10 to 99
.. code:: python 5. Create a vector with values ranging from 10 to 49
.. code-block:: python
Z = np.arange(10,50)
print Z
Z = np.arange(10,100)
6. Create a 3x3 matrix with values ranging from 0 to 8 6. Create a 3x3 matrix with values ranging from 0 to 8
.. code:: python .. code-block:: python
Z = np.arange(9).reshape(3,3) Z = np.arange(9).reshape(3,3)
print Z
7. Find indices of non-zero elements from [1,2,0,0,4,0] 7. Find indices of non-zero elements from [1,2,0,0,4,0]
.. code:: python .. code-block:: python
nz = np.nonzero([1,2,0,0,4,0]) nz = np.nonzero([1,2,0,0,4,0])
print nz
8. Create a 3x3 identity matrix 8. Create a 3x3 identity matrix
.. code:: python .. code-block:: python
Z = np.eye(3) Z = np.eye(3)
print Z
9. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal 9. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal
.. code:: python .. code-block:: python
Z = np.diag(1+np.arange(4),k=-1) Z = np.diag(1+np.arange(4),k=-1)
print Z
10. Create a 10x10x10 array with random values 10. Create a 3x3x3 array with random values
.. code:: python .. code-block:: python
Z = np.random.random((3,3,3))
print Z
Z = np.random.random((10,10,10))
Novice Novice
====== ======
1. Create a 8x8 matrix and fill it with a checkerboard pattern 1. Create a 8x8 matrix and fill it with a checkerboard pattern
.. code:: python .. code-block:: python
Z = np.zeros((8,8)) Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1 Z[1::2,::2] = 1
Z[::2,1::2] = 1 Z[::2,1::2] = 1
print Z
2. Create a 10x10 array with random values and find the minimum and maximum values 2. Create a 10x10 array with random values and find the minimum and maximum values
.. code:: python .. code-block:: python
Z = np.random.random((10,10)) Z = np.random.random((10,10))
Zmin, Zmax = Z.min(), Z.max() Zmin, Zmax = Z.min(), Z.max()
print Zmin, Zmax
3. Create a checkerboard 8x8 matrix using the tile function 3. Create a checkerboard 8x8 matrix using the tile function
.. code:: python .. code-block:: python
Z = np.tile( np.array([[0,1],[1,0]]), (4,4)) Z = np.tile( np.array([[0,1],[1,0]]), (4,4))
print Z
4. Normalize a 5x5 random matrix (between 0 and 1) 4. Normalize a 5x5 random matrix (between 0 and 1)
.. code:: python .. code-block:: python
Z = np.random.random((5,5)) Z = np.random.random((5,5))
Zmax,Zmin = Z.max(), Z.min() Zmax,Zmin = Z.max(), Z.min()
Z = (Z - Zmin)/(Zmax - Zmin) Z = (Z - Zmin)/(Zmax - Zmin)
print Z
5. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) 5. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)
.. code:: python .. code-block:: python
Z = np.dot(np.ones((5,3)), np.ones((3,2))) Z = np.dot(np.ones((5,3)), np.ones((3,2)))
print Z
6. Create a 10x10 matrix with row values ranging from 0 to 9 6. Create a 5x5 matrix with row values ranging from 0 to 4
.. code:: python .. code-block:: python
Z = np.zeros((10,10)) Z = np.zeros((5,5))
Z += np.arange(10) Z += np.arange(5)
print Z
7. Create a vector of size 1000 with values ranging from 0 to 1, both excluded
.. code:: python 7. Create a vector of size 10 with values ranging from 0 to 1, both excluded
Z = np.random.linspace(0,1,1002,endpoint=True)[1:-1] .. code-block:: python
8. Create a random vector of size 100 and sort it Z = np.linspace(0,1,12,endpoint=True)[1:-1]
print Z
.. code:: python
Z = np.random.random(100) 8. Create a random vector of size 10 and sort it
.. code-block:: python
Z = np.random.random(10)
Z.sort() Z.sort()
print Z
9. Consider two random matrices A anb B, check if they are equal.
.. code:: python 9. Consider two random array A anb B, check if they are equal.
A = np.random.randint(0,2,(2,2)) .. code-block:: python
B = np.random.randint(0,2,(2,2))
A = np.random.randint(0,2,5)
B = np.random.randint(0,2,5)
equal = np.allclose(A,B) equal = np.allclose(A,B)
print equal
10. Create a random vector of size 1000 and find the mean value
.. code:: python 10. Create a random vector of size 30 and find the mean value
Z = np.random.random(1000) .. code-block:: python
Z = np.random.random(30)
m = Z.mean() m = Z.mean()
print m
@@ -188,43 +220,49 @@ Apprentice
1. Make an array immutable (read-only) 1. Make an array immutable (read-only)
.. code:: python .. code-block:: python
Z = np.zeros(10) Z = np.zeros(10)
Z.flags.writeable = False Z.flags.writeable = False
Z[0] = 1
2. Consider a random 100x2 matrix representing cartesian coordinates, convert 2. Consider a random 10x2 matrix representing cartesian coordinates, convert
them to polar coordinates them to polar coordinates
.. code:: python .. code-block:: python
Z = np.random.random((100,2)) Z = np.random.random((10,2))
X,Y = Z[:,0], Z[:,1] X,Y = Z[:,0], Z[:,1]
R = np.sqrt(X**2+Y**2) R = np.sqrt(X**2+Y**2)
T = np.arctan2(Y,X) T = np.arctan2(Y,X)
print R
print T
3. Create random vector of size 100 and replace the maximum value by 0 3. Create random vector of size 10 and replace the maximum value by 0
.. code:: python .. code-block:: python
Z = np.random.random(100) Z = np.random.random(10)
Z[Z.argmax()] = 0 Z[Z.argmax()] = 0
print Z
4. Create a structured array with ``x`` and ``y`` coordinates covering the 4. Create a structured array with ``x`` and ``y`` coordinates covering the
[0,1]x[0,1] area. [0,1]x[0,1] area.
.. code:: python .. code-block:: python
Z = np.zeros((10,10), [('x',float),('y',float)]) Z = np.zeros((10,10), [('x',float),('y',float)])
Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,10), Z['x'], Z['y'] = np.meshgrid(np.linspace(0,1,10),
np.linspace(0,1,10)) np.linspace(0,1,10))
print Z
5. Print the minimum and maximum representable value for each numpy scalar type 5. Print the minimum and maximum representable value for each numpy scalar type
.. code:: python .. code-block:: python
for dtype in [np.int8, np.int32, np.int64]: for dtype in [np.int8, np.int32, np.int64]:
print np.iinfo(dtype).min print np.iinfo(dtype).min
@@ -237,43 +275,48 @@ Apprentice
6. Create a structured array representing a position (x,y) and a color (r,g,b) 6. Create a structured array representing a position (x,y) and a color (r,g,b)
.. code:: python .. code-block:: python
Z = np.zeros(10, [ ('position', [ ('x', float, 1), Z = np.zeros(10, [ ('position', [ ('x', float, 1),
('y', float, 1)]), ('y', float, 1)]),
('color', [ ('r', float, 1), ('color', [ ('r', float, 1),
('g', float, 1), ('g', float, 1),
('b', float, 1)])]) ('b', float, 1)])])
print Z
7. Consider a random vector with shape (100,2) representing coordinates, find 7. Consider a random vector with shape (100,2) representing coordinates, find
point by point distances point by point distances
.. code:: python .. code-block:: python
Z = np.random.random((10,2)) Z = np.random.random((10,2))
X,Y = np.atleast_2d(Z[:,0]), np.atleast_2d(Z[:,1]) X,Y = np.atleast_2d(Z[:,0]), np.atleast_2d(Z[:,1])
D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2) D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2)
print D
# Much faster with scipy # Much faster with scipy
import scipy
Z = np.random.random((10,2)) Z = np.random.random((10,2))
D = scipy.spatial.distance.cdist(Z,Z) D = scipy.spatial.distance.cdist(Z,Z)
print D
8. Generate a generic 2D Gaussian-like array 8. Generate a generic 2D Gaussian-like array
.. code:: python .. code-block:: python
X, Y = np.meshgrid(np.linspace(-1,1,100), np.linspace(-1,1,100)) X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10))
D = np.sqrt(X*X+Y*Y) D = np.sqrt(X*X+Y*Y)
sigma, mu = 1.0, 0.0 sigma, mu = 1.0, 0.0
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) )
print G
9. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 9. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3
consecutive zeros interleaved between each value ? consecutive zeros interleaved between each value ?
.. code:: python .. code-block:: python
# Author: Warren Weckesser # Author: Warren Weckesser
@@ -281,14 +324,17 @@ Apprentice
nz = 3 nz = 3
Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz)) Z0 = np.zeros(len(Z) + (len(Z)-1)*(nz))
Z0[::nz+1] = Z 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
.. code:: python .. code-block:: python
Z.flat[np.abs(Z - z).argmin()]
Z = np.random.uniform(0,1,10)
z = 0.5
m = Z.flat[np.abs(Z - z).argmin()]
print m
Journeyman Journeyman
@@ -302,49 +348,53 @@ Journeyman
How to read it ? How to read it ?
.. code:: python .. code-block:: python
Z = genfromtxt("missing.dat", delimiter=",") Z = np.genfromtxt("missing.dat", delimiter=",")
2. Consider a generator function that generates 10 integers and use it to build an 2. Consider a generator function that generates 10 integers and use it to build an
array array
.. code:: python .. code-block:: python
def generate(): def generate():
for x in xrange(10): for x in xrange(10):
yield x yield x
Z = np.fromiter(generate(),dtype=float,count=-1) Z = np.fromiter(generate(),dtype=float,count=-1)
print Z
3. Consider a given vector, how to add 1 to each element indexed by a second 3. Consider a given vector, how to add 1 to each element indexed by a second
vector (be careful with repeated indices) ? vector (be careful with repeated indices) ?
.. code:: python .. code-block:: python
# Author: Brett Olsen # Author: Brett Olsen
Z = np.ones(10) Z = np.ones(10)
I = np.random.randint(0,len(Z),20) I = np.random.randint(0,len(Z),20)
Z += np.bincount(I, minlength=len(Z)) Z += np.bincount(I, minlength=len(Z))
print Z
4. How to accumulate elements of a vector (X) to an array (F) based on an index 4. How to accumulate elements of a vector (X) to an array (F) based on an index
list (I) ? list (I) ?
.. code:: python .. code-block:: python
# Author: Alan G Isaac # Author: Alan G Isaac
X = [1,2,3,4,5,6] X = [1,2,3,4,5,6]
I = [1,3,9,3,4,1] I = [1,3,9,3,4,1]
F = np.bincount(I,X) F = np.bincount(I,X)
print F
5. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique 5. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique
colors colors
.. code:: python .. code-block:: python
# Author: Nadav Horesh # Author: Nadav Horesh
@@ -352,23 +402,22 @@ Journeyman
I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)
F = I[...,0]*256*256 + I[...,1]*256 +I[...,2] F = I[...,0]*256*256 + I[...,1]*256 +I[...,2]
n = len(np.unique(F)) n = len(np.unique(F))
print np.unique(I)
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:: python
A = np.random.randint(0,10,(3,4,3,4)) A = np.random.randint(0,10,(3,4,3,4))
sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1) sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)
print
7. Considering a one-dimensional vector D, how to compute means of subsets of D 7. Considering a one-dimensional vector D, how to compute means of subsets of D
using a vector S of same size describing subset indices ? using a vector S of same size describing subset indices ?
.. code:: python .. code-block:: python
# Jaime Fernández del Río # Jaime Fernández del Río
@@ -377,8 +426,7 @@ Journeyman
D_sums = np.bincount(S, weights=D) D_sums = np.bincount(S, weights=D)
D_counts = np.bincount(S) D_counts = np.bincount(S)
D_means = D_sums / D_counts D_means = D_sums / D_counts
print D_means
@@ -389,72 +437,77 @@ Craftsman
first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last
row should be (Z[-3],Z[-2],Z[-1]) row should be (Z[-3],Z[-2],Z[-1])
.. code:: python .. code-block:: python
# Author: Joe Kington / Erik Rigtorp # Author: Joe Kington / Erik Rigtorp
from numpy.lib import stride_tricks
def rolling(a, window): def rolling(a, window):
shape = (a.size - window + 1, window) shape = (a.size - window + 1, window)
strides = (a.itemsize, a.itemsize) strides = (a.itemsize, a.itemsize)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides) return stride_tricks.as_strided(a, shape=shape, strides=strides)
Z = rolling(np.arange(10), 3)
print Z
Z = rolling(np.arange(100), 3)
2. Consider a set of 100 triplets describing 100 triangles (with shared 2. Consider a set of 10 triplets describing 10 triangles (with shared
vertices), find the set of unique line segments composing all the triangles. vertices), find the set of unique line segments composing all the triangles.
.. code:: python .. code-block:: python
# Author: Nicolas Rougier # Author: Nicolas Rougier
faces = np.random.randint(0,100,(100,3)) faces = np.random.randint(0,100,(10,3))
F = np.roll(faces.repeat(2,axis=1),-1,axis=1) F = np.roll(faces.repeat(2,axis=1),-1,axis=1)
F = F.reshape(len(F)*3,2) F = F.reshape(len(F)*3,2)
F = np.sort(F,axis=1) F = np.sort(F,axis=1)
G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] ) G = F.view( dtype=[('p0',F.dtype),('p1',F.dtype)] )
G = np.unique(G) G = np.unique(G)
print G
3. Given an array C that is a bincount, how to produce an array A such that 3. Given an array C that is a bincount, how to produce an array A such that
np.bincount(A) == C ? np.bincount(A) == C ?
.. code:: python .. code-block:: python
# Jaime Fernández del Río # Jaime Fernández del Río
C = np.bincount([1,1,2,3,4,4,6]) C = np.bincount([1,1,2,3,4,4,6])
A = np.repeat(np.arange(len(C)), C) A = np.repeat(np.arange(len(C)), C)
print A
Artisan Artisan
======= =======
1. Considering a 100x3 matrix, extract rows with unequal values (e.g. [2,2,3]) 1. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3])
.. code:: python .. code-block:: python
# Author: Robert Kern # Author: Robert Kern
Z = np.random.randint(0,5,(100,3)) Z = np.random.randint(0,5,(10,3))
E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1) E = np.logical_and.reduce(Z[:,1:] == Z[:,:-1], axis=1)
U = Z[~E] U = Z[~E]
print Z
print U
2. Convert a vector of ints into a matrix binary representation. 2. Convert a vector of ints into a matrix binary representation.
.. code:: python .. code-block:: python
# Author: Warren Weckesser # Author: Warren Weckesser
I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128]) I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128])
B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int) B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)
B = B[:,::-1] print B[:,::-1]
# Author: Daniel T. McDonald # Author: Daniel T. McDonald
I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=np.uint8) I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=np.uint8)
np.unpackbits(I[:, np.newaxis], axis=1) print np.unpackbits(I[:, np.newaxis], axis=1)
@@ -465,14 +518,14 @@ Adept
fixed shape and centered on a given element (pad with a ``fill`` value when fixed shape and centered on a given element (pad with a ``fill`` value when
necessary) necessary)
.. code :: python .. code:: python
# Author: Nicolas Rougier # Author: Nicolas Rougier
Z = np.random.random((25,25)) Z = np.random.randint(0,10,(10,10))
shape = (3,3) shape = (5,5)
fill = 0 fill = 0
position = (0,0) position = (1,1)
R = np.ones(shape, dtype=Z.dtype)*fill R = np.ones(shape, dtype=Z.dtype)*fill
P = np.array(list(position)).astype(int) P = np.array(list(position)).astype(int)
@@ -492,20 +545,20 @@ Adept
r = [slice(start,stop) for start,stop in zip(R_start,R_stop)] r = [slice(start,stop) for start,stop in zip(R_start,R_stop)]
z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)] z = [slice(start,stop) for start,stop in zip(Z_start,Z_stop)]
R[r] = Z[z] R[r] = Z[z]
print Z
print R
2. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an 2. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an
array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]] ? array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]] ?
.. code:: python .. code-block:: python
# Stéfan van der Walt # Stéfan van der Walt
Z = np.arange(1,15) Z = np.arange(1,15,dtype=uint32)
R = as_strided(Z,(11,4),(4,4)) R = stride_tricks.as_strided(Z,(11,4),(4,4))
print R
Expert Expert
@@ -515,7 +568,7 @@ Expert
that contain elements of each row of B regardless of the order of the elements that contain elements of each row of B regardless of the order of the elements
in B ? in B ?
.. code:: python .. code-block:: python
# Author: Gabe Schwartz # Author: Gabe Schwartz
@@ -524,11 +577,12 @@ Expert
C = (A[..., np.newaxis, np.newaxis] == B) C = (A[..., np.newaxis, np.newaxis] == B)
rows = (C.sum(axis=(1,2,3)) >= B.shape[1]).nonzero()[0] rows = (C.sum(axis=(1,2,3)) >= B.shape[1]).nonzero()[0]
print rows
2. Extract all the contiguous 3x3 blocks from a random 10x10 matrix. 2. Extract all the contiguous 3x3 blocks from a random 10x10 matrix.
.. code:: python .. code-block:: python
# Chris Barker # Chris Barker
@@ -537,11 +591,12 @@ Expert
i = 1 + (Z.shape[0]-3) i = 1 + (Z.shape[0]-3)
j = 1 + (Z.shape[1]-3) j = 1 + (Z.shape[1]-3)
C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides) C = stride_tricks.as_strided(Z, shape=(i, j, n, n), strides=Z.strides + Z.strides)
print C
3. Create a 2D array subclass such that Z[i,j] == Z[j,i] 3. Create a 2D array subclass such that Z[i,j] == Z[j,i]
.. code:: python .. code-block:: python
# Eric O. Lebigot # Eric O. Lebigot
# Note: only works for 2d array and value setting using indices # Note: only works for 2d array and value setting using indices
@@ -554,7 +609,7 @@ Expert
def symetric(Z): def symetric(Z):
return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric) return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric)
S = symetric(np.random.randint(0,10,(5,5)) S = symetric(np.random.randint(0,10,(5,5)))
S[2,3] = 42 S[2,3] = 42
print S print S
@@ -566,14 +621,15 @@ Master
.. note:: See `stackoverflow <http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array/>`_ for explanations. .. note:: See `stackoverflow <http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array/>`_ for explanations.
.. code:: python .. code-block:: python
# Jaime Fernández del Río # Jaime Fernández del Río
Z = np.random.randint(0,2,(6,6)) Z = np.random.randint(0,2,(6,3))
T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1]))) T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))
_, idx = np.unique(T, return_index=True) _, idx = np.unique(T, return_index=True)
uZ = Z[idx] uZ = Z[idx]
print uZ