From 25ac81b95b2ebbfa6df50eb411d15b33ee33c4ba Mon Sep 17 00:00:00 2001
From: Nicolas Rougier
Here is what the page looks like so far: http://www.labri.fr/perso/nrougier/teaching/numpy.100/index.html
-Note
-The level names came from an old-game (Dungeon Master)
-Repository is at: https://github.com/rougier/numpy-100
The corresponding IPython notebook is available from the github repo, thanks to the rst2ipynb conversion tool by Valentin Haenel
Thanks to Michiaki Ariga, there is now a Julia version.
-Import the numpy package under the name np
@@ -94,16 +88,13 @@ from the github repo, thanks to the Create a 3x3x3 array with random values +Z = np.random.random((3,3,3)) print Z+
Create a 8x8 matrix and fill it with a checkerboard pattern
Z = np.zeros((8,8),dtype=int) @@ -168,17 +159,14 @@ from the github repo, thanks to the Create a random vector of size 30 and find the mean value +Z = np.random.random(30) m = Z.mean() print m+
Make an array immutable (read-only)
Z = np.zeros(10) @@ -267,18 +255,15 @@ point by point distances
Find the nearest value from a given value in an array
+Z = np.random.uniform(0,1,10) z = 0.5 m = Z.flat[np.abs(Z - z).argmin()] print m+
Consider the following file:
1,2,3,4,5 @@ -375,6 +360,7 @@ dimensions (5,5) ?
How to swap two rows of an array ?
+# Author: Eelco Hoogendoorn @@ -382,12 +368,8 @@ dimensions (5,5) ? A[[0,1]] = A[[1,0]] print A+
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 row should be (Z[-3],Z[-2],Z[-1])
@@ -445,9 +427,6 @@ python -c "import numpy; numpy.info(numHow to negate a boolean, or to change the sign of a float inplace ?
--# Author: Nathaniel J. Smith @@ -457,14 +436,7 @@ python -c "import numpy; numpy.info(num Z = np.random.uniform(-1.0,1.0,100) np.negative(arr, out=arr)-
Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3])
# Author: Robert Kern @@ -513,11 +485,6 @@ how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i]) ?< Answer needed actually
Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a fill value when necessary)
@@ -561,11 +528,6 @@ array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]] ? print RConsider 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 in B ?
@@ -628,11 +590,6 @@ How to compute the sum of of the p matrix products at once ? (result has shape ( # and 2 and 1, to remain with a (n,1) vector.Given a two dimensional array, how to extract unique rows ?
Note
@@ -671,9 +628,5 @@ How to compute the sum of of the p matrix products at once ? (result has shape (