Skip to content

Commit e9289c9

Browse files
committed
added several applications to the presentation
1 parent 7839bf3 commit e9289c9

File tree

2 files changed

+267
-30
lines changed

2 files changed

+267
-30
lines changed

numpy-tutorial-exercises.ipynb

Lines changed: 164 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,33 @@
321321
"outputs": [],
322322
"source": []
323323
},
324+
{
325+
"cell_type": "code",
326+
"execution_count": null,
327+
"metadata": {
328+
"collapsed": true
329+
},
330+
"outputs": [],
331+
"source": []
332+
},
333+
{
334+
"cell_type": "code",
335+
"execution_count": null,
336+
"metadata": {
337+
"collapsed": true
338+
},
339+
"outputs": [],
340+
"source": []
341+
},
342+
{
343+
"cell_type": "code",
344+
"execution_count": null,
345+
"metadata": {
346+
"collapsed": true
347+
},
348+
"outputs": [],
349+
"source": []
350+
},
324351
{
325352
"cell_type": "markdown",
326353
"metadata": {},
@@ -968,7 +995,47 @@
968995
"cell_type": "markdown",
969996
"metadata": {},
970997
"source": [
971-
"## An application: sieve of Eratosthenes"
998+
"## Fancy indexing ‒ Boolean mask"
999+
]
1000+
},
1001+
{
1002+
"cell_type": "code",
1003+
"execution_count": null,
1004+
"metadata": {
1005+
"collapsed": true
1006+
},
1007+
"outputs": [],
1008+
"source": [
1009+
"a = np.arange(40).reshape(5, 8)"
1010+
]
1011+
},
1012+
{
1013+
"cell_type": "code",
1014+
"execution_count": null,
1015+
"metadata": {
1016+
"collapsed": false
1017+
},
1018+
"outputs": [],
1019+
"source": [
1020+
"a %3 == 0"
1021+
]
1022+
},
1023+
{
1024+
"cell_type": "code",
1025+
"execution_count": null,
1026+
"metadata": {
1027+
"collapsed": false
1028+
},
1029+
"outputs": [],
1030+
"source": [
1031+
"a[a %3 == 0]"
1032+
]
1033+
},
1034+
{
1035+
"cell_type": "markdown",
1036+
"metadata": {},
1037+
"source": [
1038+
"## Application: sieve of Eratosthenes"
9721039
]
9731040
},
9741041
{
@@ -1289,6 +1356,22 @@
12891356
"plt.axis('scaled')"
12901357
]
12911358
},
1359+
{
1360+
"cell_type": "markdown",
1361+
"metadata": {},
1362+
"source": [
1363+
"## Application: π from random numbers"
1364+
]
1365+
},
1366+
{
1367+
"cell_type": "code",
1368+
"execution_count": null,
1369+
"metadata": {
1370+
"collapsed": true
1371+
},
1372+
"outputs": [],
1373+
"source": []
1374+
},
12921375
{
12931376
"cell_type": "markdown",
12941377
"metadata": {},
@@ -1718,6 +1801,29 @@
17181801
"plt.imshow(imdata, cmap='gray')"
17191802
]
17201803
},
1804+
{
1805+
"cell_type": "markdown",
1806+
"metadata": {},
1807+
"source": [
1808+
"## Application: π from random numbers"
1809+
]
1810+
},
1811+
{
1812+
"cell_type": "markdown",
1813+
"metadata": {},
1814+
"source": [
1815+
"Create an array of random numbers and determine the fraction of points with distance from the origin smaller than one. Determine an approximation for π."
1816+
]
1817+
},
1818+
{
1819+
"cell_type": "code",
1820+
"execution_count": null,
1821+
"metadata": {
1822+
"collapsed": true
1823+
},
1824+
"outputs": [],
1825+
"source": []
1826+
},
17211827
{
17221828
"cell_type": "markdown",
17231829
"metadata": {
@@ -1729,7 +1835,7 @@
17291835
},
17301836
{
17311837
"cell_type": "code",
1732-
"execution_count": 1,
1838+
"execution_count": null,
17331839
"metadata": {
17341840
"collapsed": true
17351841
},
@@ -1740,22 +1846,11 @@
17401846
},
17411847
{
17421848
"cell_type": "code",
1743-
"execution_count": 9,
1849+
"execution_count": null,
17441850
"metadata": {
17451851
"collapsed": false
17461852
},
1747-
"outputs": [
1748-
{
1749-
"data": {
1750-
"text/plain": [
1751-
"array([-0.56155281, 3.56155281])"
1752-
]
1753-
},
1754-
"execution_count": 9,
1755-
"metadata": {},
1756-
"output_type": "execute_result"
1757-
}
1758-
],
1853+
"outputs": [],
17591854
"source": [
17601855
"a = np.arange(4).reshape(2, 2)\n",
17611856
"eigenvalues, eigenvectors = LA.eig(a)\n",
@@ -1764,23 +1859,11 @@
17641859
},
17651860
{
17661861
"cell_type": "code",
1767-
"execution_count": 10,
1862+
"execution_count": null,
17681863
"metadata": {
17691864
"collapsed": false
17701865
},
1771-
"outputs": [
1772-
{
1773-
"data": {
1774-
"text/plain": [
1775-
"array([[-0.87192821, -0.27032301],\n",
1776-
" [ 0.48963374, -0.96276969]])"
1777-
]
1778-
},
1779-
"execution_count": 10,
1780-
"metadata": {},
1781-
"output_type": "execute_result"
1782-
}
1783-
],
1866+
"outputs": [],
17841867
"source": [
17851868
"eigenvectors"
17861869
]
@@ -1842,6 +1925,58 @@
18421925
"Determine the eigenvalue larger than one appearing in the Fibonacci problem. Verify the result by calculating the ratio of successive Fibonacci numbers. Do you recognize the result?"
18431926
]
18441927
},
1928+
{
1929+
"cell_type": "code",
1930+
"execution_count": null,
1931+
"metadata": {
1932+
"collapsed": true
1933+
},
1934+
"outputs": [],
1935+
"source": []
1936+
},
1937+
{
1938+
"cell_type": "markdown",
1939+
"metadata": {},
1940+
"source": [
1941+
"## Application: Brownian motion"
1942+
]
1943+
},
1944+
{
1945+
"cell_type": "markdown",
1946+
"metadata": {},
1947+
"source": [
1948+
"1. Simulate several trajectories for a one-dimensional\n",
1949+
"Brownian motion \n",
1950+
"Hint: np.random.choice\n",
1951+
"2. Plot the mean distance from the origin as a function\n",
1952+
"of time\n",
1953+
"3. Plot the variance of the trajectories as a function of\n",
1954+
"time\n"
1955+
]
1956+
},
1957+
{
1958+
"cell_type": "code",
1959+
"execution_count": null,
1960+
"metadata": {
1961+
"collapsed": true
1962+
},
1963+
"outputs": [],
1964+
"source": []
1965+
},
1966+
{
1967+
"cell_type": "markdown",
1968+
"metadata": {},
1969+
"source": [
1970+
"## Application: identify entry closest to ½"
1971+
]
1972+
},
1973+
{
1974+
"cell_type": "markdown",
1975+
"metadata": {},
1976+
"source": [
1977+
"Create a 2d array containing random numbers and generate a vector containing for each row the entry closest to one-half."
1978+
]
1979+
},
18451980
{
18461981
"cell_type": "code",
18471982
"execution_count": null,

presentation.tex

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
\usepackage{beramono}
88
\usepackage{fontawesome}
99
\usepackage{pifont}
10+
\usepackage{amsmath}
1011
\usepackage{rotating}
1112
\usepackage{listings}
13+
\usepackage{nicefrac}
1214
\usepackage{ulem}
1315
\usepackage{hyperref}
1416

@@ -453,6 +455,15 @@
453455
\end{center}
454456
\end{frame}
455457

458+
\begin{frame}{Application: sieve of Eratosthenes}
459+
\includegraphics[width=\textwidth]{eratosthenes_1}\\[0.3truecm]
460+
\includegraphics[width=\textwidth]{eratosthenes_2}\\[0.3truecm]
461+
\includegraphics[width=\textwidth]{eratosthenes_3}\\[0.3truecm]
462+
\includegraphics[width=\textwidth]{eratosthenes_4}\\[0.3truecm]
463+
\includegraphics[width=\textwidth]{eratosthenes_5}\\[0.3truecm]
464+
\includegraphics[width=\textwidth]{eratosthenes_6}
465+
\end{frame}
466+
456467
\begin{frame}{Axes}
457468
\begin{Large}
458469
\begin{displaymath}
@@ -627,6 +638,32 @@
627638
\end{columns}
628639
\end{frame}
629640

641+
\begin{frame}{Application: $\pi$ from random numbers}
642+
\begin{columns}
643+
\begin{column}{0.4\textwidth}
644+
\begin{center}
645+
\includegraphics[width=0.8\textwidth]{random_pi}
646+
\end{center}
647+
\end{column}%
648+
\begin{column}{0.6\textwidth}
649+
\begin{enumerate}
650+
\item Create pairs of random numbers and determine the fraction of pairs which
651+
has a distance from the origin less than one.
652+
\item Multiply the result by four to obtain an approximation of $\pi$.
653+
\end{enumerate}
654+
\end{column}
655+
\end{columns}
656+
657+
\vspace{0.5truecm}
658+
hint: \texttt{count\_nonzero(a)} counts the number of non-zero values in the array \texttt{a} and
659+
also works for Boolean arrays. Remember that \texttt{np.info(\dots)} can be helpful.
660+
661+
\vspace{0.3truecm}
662+
\begin{center}
663+
\includegraphics[width=3truecm]{yourturn}
664+
\end{center}
665+
\end{frame}
666+
630667
\begin{frame}{Fibonacci series and linear algebra}
631668
\begin{center}
632669
\includegraphics[width=0.7\textwidth]{fibonacci}
@@ -686,7 +723,7 @@
686723
We are looking for the eigenvalue larger than one.
687724
\end{frame}
688725

689-
\begin{frame}[fragile]{Linear algebra in NumPy}
726+
\begin{frame}{Linear algebra in NumPy}
690727
\texttt{import numpy.linalg as LA}
691728

692729
\vspace{0.3truecm}
@@ -708,4 +745,69 @@
708745
hint: see also the methods for linear algebra in SciPy
709746
\end{frame}
710747

748+
\begin{frame}{Statistics in NumPy}
749+
\structure{Order statistics}\\
750+
\small{amin, amax, nanmin, nanmax, ptp, percentile, nanpercentile}\\[0.2truecm]
751+
\structure{Averages and variances}\\
752+
\small{median, average, mean, std, var, nanmedian, nanmean, nanstd, nanvar}\\[0.2truecm]
753+
\structure{Correlating}\\
754+
\small{corrcoef, correlate, cov}\\[0.2truecm]
755+
\structure{Histograms}\\
756+
\small{histogram, histogram2d, histogramdd, bincount, digitize}
757+
\end{frame}
758+
759+
\begin{frame}{Application: Brownian motion}
760+
\begin{center}
761+
\includegraphics[width=0.8\textwidth]{diffusion}
762+
\end{center}
763+
764+
\begin{enumerate}
765+
\item Simulate several trajectories for a one-dimensional Brownian motion\\
766+
hint: \texttt{np.random.choice}
767+
\item Plot the mean distance from the origin as a function of time\\
768+
\item Plot the variance of the trajectories as a function of time
769+
\end{enumerate}
770+
771+
\vspace{0.3truecm}
772+
\begin{center}
773+
\includegraphics[width=3truecm]{yourturn}
774+
\end{center}
775+
\end{frame}
776+
777+
\begin{frame}{Sorting, searching, and counting in NumPy}
778+
779+
\structure{Sorting}\\
780+
\small{sort, lexsort, argsort, ndarray.sort, msort, sort\_complex, partition,
781+
argpartition}\\[0.2truecm]
782+
\structure{Searching}\\
783+
\small{argmax, nanargmax, argmin, nanargmin, argwhere, nonzero, flatnonzero,
784+
where, searchsorted, extract}\\[0.2truecm]
785+
\structure{Counting}\\
786+
\small{count\_nonzero}
787+
\end{frame}
788+
789+
\begin{frame}{Application: identify entry closest to $\nicefrac{1}{2}$}
790+
\begin{displaymath}
791+
\begin{pmatrix}
792+
0.05344164 & \alert{0.37648768} & 0.80691163 & 0.71400815\\
793+
\alert{0.60825034} & 0.35778938 & 0.37393356 & 0.32615374\\
794+
0.83118547 & 0.33178711 & 0.21548027 & \alert{0.42209291}
795+
\end{pmatrix}
796+
\end{displaymath}
797+
798+
\begin{displaymath}
799+
\Downarrow
800+
\end{displaymath}
801+
802+
\begin{displaymath}
803+
\begin{pmatrix}
804+
\alert{0.37648768}\\ \alert{0.60825034}\\ \alert{0.42209291}
805+
\end{pmatrix}
806+
\end{displaymath}
807+
808+
\vspace{0.3truecm}
809+
\includegraphics[width=3truecm]{yourturn}\qquad
810+
\raisebox{0.7truecm}{hint: use \texttt{np.argsort}}
811+
\end{frame}
812+
711813
\end{document}

0 commit comments

Comments
 (0)