{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![](../docs/banner.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy\n", "\n", "**Tomas Beuzen, September 2020**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These exercises complement [Chapter 5](../chapters/chapter5-numpy.ipynb) and [Chapter 6](../chapters/chapter6-numpy-addendum.ipynb)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import numpy under the alias `np`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the following arrays:\n", "\n", "1. Create an array of 5 zeros.\n", "2. Create an array of 10 ones.\n", "3. Create an array of 5 3.141s.\n", "4. Create an array of the integers 1 to 20.\n", "5. Create a 5 x 5 matrix of ones with a dtype `int`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use numpy to:\n", "1. Create an 3D matrix of 3 x 3 x 3 full of random numbers drawn from a standard normal distribution (hint: `np.random.randn()`)\n", "2. Reshape the above array into shape (27,)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an array of 20 linearly spaced numbers between 1 and 10." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the following code to create an array of shape 4 x 4 and then use indexing to produce the outputs shown below." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "a = np.arange(1, 26).reshape(5, -1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "20\n", "```" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([[ 9, 10],\n", " [14, 15],\n", " [19, 20],\n", " [24, 25]])\n", "```" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([ 6, 7, 8, 9, 10])\n", "```" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([[11, 12, 13, 14, 15],\n", " [16, 17, 18, 19, 20]])\n", "```" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([[ 8, 9],\n", " [13, 14]])\n", "```" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the sum of all the numbers in `a`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the sum of each row in `a`." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extract all values of `a` greater than the mean of `a` (hint: use a boolean mask)." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the location of the minimum value in the following array `b`:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1.0856306 , 0.99734545, 0.2829785 , -1.50629471, -0.57860025,\n", " 1.65143654, -2.42667924, -0.42891263, 1.26593626, -0.8667404 ])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(123)\n", "b = np.random.randn(10)\n", "b" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 10." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the location of the maximum value in the following 2D array `c` (hint: there are many ways to do this question, but a quick search on stackoverflow.com will typically help you find the optimum solution for a problem, for example see [post](https://stackoverflow.com/questions/3584243/get-the-position-of-the-biggest-item-in-a-multi-dimensional-numpy-array)):" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1.0856306 , 0.99734545],\n", " [ 0.2829785 , -1.50629471],\n", " [-0.57860025, 1.65143654]])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(123)\n", "c = np.random.randn(3, 2)\n", "c" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import numpy under the alias `np`." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the following arrays:\n", "\n", "1. Create an array of 5 zeros.\n", "2. Create an array of 10 ones.\n", "3. Create an array of 5 3.141s.\n", "4. Create an array of the integers 1 to 20.\n", "5. Create a 5 x 5 matrix of ones with a dtype `int`." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0. 0. 0.]\n", "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\n", "[3.141 3.141 3.141 3.141 3.141]\n", "[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]\n", "[[1 1 1 1 1]\n", " [1 1 1 1 1]\n", " [1 1 1 1 1]\n", " [1 1 1 1 1]\n", " [1 1 1 1 1]]\n" ] } ], "source": [ "print(np.zeros(5))\n", "print(np.ones(10))\n", "print(np.full(5, 3.141))\n", "print(np.array(range(21)))\n", "print(np.ones((5, 5), dtype=int))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use numpy to:\n", "1. Create an 3D matrix of 3 x 3 x 3 full of random numbers drawn from a standard normal distribution (hint: `np.random.randn()`)\n", "2. Reshape the above array into shape (27,)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[-2.42667924, -0.42891263, 1.26593626],\n", " [-0.8667404 , -0.67888615, -0.09470897],\n", " [ 1.49138963, -0.638902 , -0.44398196]],\n", "\n", " [[-0.43435128, 2.20593008, 2.18678609],\n", " [ 1.0040539 , 0.3861864 , 0.73736858],\n", " [ 1.49073203, -0.93583387, 1.17582904]],\n", "\n", " [[-1.25388067, -0.6377515 , 0.9071052 ],\n", " [-1.4286807 , -0.14006872, -0.8617549 ],\n", " [-0.25561937, -2.79858911, -1.7715331 ]]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.random.randn(3, 3, 3)\n", "x" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-2.42667924, -0.42891263, 1.26593626, -0.8667404 , -0.67888615,\n", " -0.09470897, 1.49138963, -0.638902 , -0.44398196, -0.43435128,\n", " 2.20593008, 2.18678609, 1.0040539 , 0.3861864 , 0.73736858,\n", " 1.49073203, -0.93583387, 1.17582904, -1.25388067, -0.6377515 ,\n", " 0.9071052 , -1.4286807 , -0.14006872, -0.8617549 , -0.25561937,\n", " -2.79858911, -1.7715331 ])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.reshape(-1) # or x.reshape(27)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an array of 20 linearly spaced numbers between 1 and 10." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1. , 1.47368421, 1.94736842, 2.42105263, 2.89473684,\n", " 3.36842105, 3.84210526, 4.31578947, 4.78947368, 5.26315789,\n", " 5.73684211, 6.21052632, 6.68421053, 7.15789474, 7.63157895,\n", " 8.10526316, 8.57894737, 9.05263158, 9.52631579, 10. ])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linspace(1, 10, 20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Below I've defined an array of shape 4 x 4. Use indexing to procude the given outputs." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 4, 5],\n", " [ 6, 7, 8, 9, 10],\n", " [11, 12, 13, 14, 15],\n", " [16, 17, 18, 19, 20],\n", " [21, 22, 23, 24, 25]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(1, 26).reshape(5, -1)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "20\n", "```" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[3,4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([[ 9, 10],\n", " [14, 15],\n", " [19, 20],\n", " [24, 25]])\n", "```" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 9, 10],\n", " [14, 15],\n", " [19, 20],\n", " [24, 25]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1:,3:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([ 6, 7, 8, 9, 10])\n", "```" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 6, 7, 8, 9, 10])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1,:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([[11, 12, 13, 14, 15],\n", " [16, 17, 18, 19, 20]])\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", "array([[ 8, 9],\n", " [13, 14]])\n", "```" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 8, 9],\n", " [13, 14]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[1:3,2:4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the sum of all the numbers in `a`." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "325" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sum()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate the sum of each row in `a`." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 15, 40, 65, 90, 115])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sum(axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extract all values of `a` greater than the mean of `a` (hint: use a boolean mask)." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a[a > a.mean()]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the location of the minimum value in the following array `b`:" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1.0856306 , 0.99734545, 0.2829785 , -1.50629471, -0.57860025,\n", " 1.65143654, -2.42667924, -0.42891263, 1.26593626, -0.8667404 ])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(123)\n", "b = np.random.randn(10)\n", "b" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.argmin()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 10." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find the location of the maximum value in the following 2D array `c` (hint: there are many ways to do this question, but a quick search on stackoverflow.com will typically help you find the optimum solution for a problem, for example see [post](https://stackoverflow.com/questions/3584243/get-the-position-of-the-biggest-item-in-a-multi-dimensional-numpy-array)):" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1.0856306 , 0.99734545],\n", " [ 0.2829785 , -1.50629471],\n", " [-0.57860025, 1.65143654]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.seed(123)\n", "c = np.random.randn(3, 2)\n", "c" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Location of maximum: (2, 1)\n", " Value of maximum: 1.65\n" ] } ], "source": [ "print(f\"Location of maximum: {np.unravel_index(c.argmax(), c.shape)}\")\n", "print(f\" Value of maximum: {c.max():.2f}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.8" } }, "nbformat": 4, "nbformat_minor": 4 }