{ "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": [ "