Skip to content
Snippets Groups Projects
Commit 19e8a321 authored by Vermaat's avatar Vermaat
Browse files

Add some exercises for NumPy

parent bb67e923
No related branches found
No related tags found
No related merge requests found
......@@ -694,6 +694,31 @@
],
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can set the seed with `np.random.seed`."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"$\\S$ Exercise: Simple arrays\n",
"===\n",
"\n",
"* Create simple one and two dimensional arrays. First redo the examples, then create your own.\n",
"* Use the functions `len`, `shape` and `ndim` on some of those arrays and observe their output.\n",
"* Experiment with `arange`, `linspace`, `ones`, `zeros`, `eye` and `diag`.\n",
"* Create different kinds of arrays with random numbers.\n",
"* Try setting the seed before creating an array with random values."
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -1179,6 +1204,93 @@
],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"$\\S$ Exercise: Indexing and slicing\n",
"===\n",
"\n",
"* Try the different flavours of slicing, using `start`, `end` and `step`.\n",
"* Verify that the slices in the previous diagram are indeed correct. You may use the following expression to create the array:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"np.arange(6) + np.transpose([np.arange(0, 51, 10)])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"text": [
"array([[ 0, 1, 2, 3, 4, 5],\n",
" [10, 11, 12, 13, 14, 15],\n",
" [20, 21, 22, 23, 24, 25],\n",
" [30, 31, 32, 33, 34, 35],\n",
" [40, 41, 42, 43, 44, 45],\n",
" [50, 51, 52, 53, 54, 55]])"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"$\\S$ Exercise: Array creation\n",
"===\n",
"\n",
"Create the following arrays (with correct data types):\n",
"\n",
" [[1, 1, 1, 1],\n",
" [1, 1, 1, 1],\n",
" [1, 1, 1, 2],\n",
" [1, 6, 1, 1]]\n",
"\n",
" [[0., 0., 0., 0., 0.],\n",
" [2., 0., 0., 0., 0.],\n",
" [0., 3., 0., 0., 0.],\n",
" [0., 0., 4., 0., 0.],\n",
" [0., 0., 0., 5., 0.],\n",
" [0., 0., 0., 0., 6.]]\n",
"\n",
"Par on course: 3 statements for each (hint: `help(np.diag)`)."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"$\\S$ Exercise: Tiling for array creation\n",
"===\n",
"\n",
"Skim through the documentation for np.tile, and use this function to construct the array:\n",
"\n",
" [[4, 3, 4, 3, 4, 3],\n",
" [2, 1, 2, 1, 2, 1],\n",
" [4, 3, 4, 3, 4, 3],\n",
" [2, 1, 2, 1, 2, 1]]"
]
},
{
"cell_type": "markdown",
"metadata": {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment