From 89bb4a9939c62c2291c99ec88e3d165177c81ca0 Mon Sep 17 00:00:00 2001 From: webdnd <76805904+webdnd@users.noreply.github.com> Date: Tue, 2 Feb 2021 19:13:44 -0800 Subject: [PATCH 1/2] Separate Answer Sections --- 3. NumPy exercises.ipynb | 4297 +++++++++++++++++++++++--------------- 1 file changed, 2555 insertions(+), 1742 deletions(-) diff --git a/3. NumPy exercises.ipynb b/3. NumPy exercises.ipynb index deff2a9..d115510 100644 --- a/3. NumPy exercises.ipynb +++ b/3. NumPy exercises.ipynb @@ -1,1743 +1,2556 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", - "
\n", - "\n", - "# NumPy exercises\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Import the numpy package under the name np\n", - "import numpy as np\n", - "\n", - "# Print the numpy version and the configuration\n", - "print(np.__version__)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Array creation" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Create a numpy array of size 10, filled with zeros." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#np.array([0] * 10)\n", - "np.zeros(10)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a numpy array with values ranging from 10 to 49" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "np.arange(10,50)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a numpy matrix of 2*2 integers, filled with ones." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "np.ones([2,2], dtype=np.int)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a numpy matrix of 3*2 float numbers, filled with ones." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "np.ones([3,2], dtype=np.float)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, create a new numpy array with the same shape and type as X, filled with ones." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.arange(4, dtype=np.int)\n", - "\n", - "np.ones_like(X)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, create a new numpy matrix with the same shape and type as X, filled with zeros." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([[1,2,3], [4,5,6]], dtype=np.int)\n", - "\n", - "np.zeros_like(X)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a numpy matrix of 4*4 integers, filled with fives." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "np.ones([4,4], dtype=np.int) * 5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, create a new numpy matrix with the same shape and type as X, filled with sevens." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([[2,3], [6,2]], dtype=np.int)\n", - "\n", - "np.ones_like(X) * 7" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a 3*3 identity numpy matrix with ones on the diagonal and zeros elsewhere." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#np.eye(3)\n", - "np.identity(3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a numpy array, filled with 3 random integer values between 1 and 10." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "np.random.randint(10, size=3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a 3\\*3\\*3 numpy matrix, filled with random float values." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "#np.random.random((3,3,3)) \n", - "np.random.randn(3,3,3) # 0 to 1 floats" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X python list convert it to an Y numpy array" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = [1, 2, 3]\n", - "print(X, type(X))\n", - "\n", - "Y = np.array(X)\n", - "print(Y, type(Y)) # different type" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, make a copy and store it on Y." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([5,2,3], dtype=np.int)\n", - "print(X, id(X))\n", - "\n", - "Y = np.copy(X)\n", - "print(Y, id(Y)) # different id" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a numpy array with numbers from 1 to 10" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "np.arange(1, 11)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a numpy array with the odd numbers between 1 to 10" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "np.arange(1, 11, 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a numpy array with numbers from 1 to 10, in descending order." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "np.arange(1, 11)[::-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Create a 3*3 numpy matrix, filled with values ranging from 0 to 8" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "np.arange(9).reshape(3,3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Show the memory size of the given Z numpy matrix" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution", - "scrolled": true - }, - "outputs": [], - "source": [ - "Z = np.zeros((10,10))\n", - "\n", - "print(\"%d bytes\" % (Z.size * Z.itemsize))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Array indexation\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Given the X numpy array, show it's first element" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array(['A','B','C','D','E'])\n", - "\n", - "X[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, show it's last element" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array(['A','B','C','D','E'])\n", - "\n", - "#X[len(X)-1]\n", - "X[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, show it's first three elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array(['A','B','C','D','E'])\n", - "\n", - "X[0:3] # remember! elements start at zero index" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, show all middle elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array(['A','B','C','D','E'])\n", - "\n", - "X[1:-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, show the elements in reverse position" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array(['A','B','C','D','E'])\n", - "\n", - "X[::-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, show the elements in an odd position" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array(['A','B','C','D','E'])\n", - "\n", - "#X[[0, 2, -1]]\n", - "X[::2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, show the first row elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "X[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, show the last row elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "X[-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, show the first element on first row" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "#X[0][0]\n", - "X[0, 0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, show the last element on last row" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "#X[-1][-1]\n", - "X[-1, -1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, show the middle row elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "#X[1:-1][1:-1] wrong!\n", - "X[1:-1, 1:-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, show the first two elements on the first two rows" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "#X[:2][:2] wrong!\n", - "#X[0:2, 0:2]\n", - "X[:2, :2]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, show the last two elements on the last two rows" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "X[2:, 2:]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Array manipulation\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Convert the given integer numpy array to float" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = [-5, -3, 0, 10, 40]\n", - "\n", - "np.array(X, np.float)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Reverse the given numpy array (first element becomes last)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = [-5, -3, 0, 10, 40]\n", - "\n", - "X[::-1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Order (sort) the given numpy array" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = [0, 10, -5, 40, -3]\n", - "\n", - "X.sort()\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, set the fifth element equal to 1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.zeros(10)\n", - "\n", - "X[4] = 1\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, change the 50 with a 40" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([10, 20, 30, 50])\n", - "\n", - "X[3] = 40\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, change the last row with all 1" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "X[-1] = np.array([1, 1, 1, 1])\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, change the last item on the last row with a 0" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "X[-1, -1] = 0\n", - "X" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, add 5 to every element" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "X + 5" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Boolean arrays _(also called masks)_\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Given the X numpy array, make a mask showing negative elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([-1,2,0,-4,5,6,0,0,-9,10])\n", - "\n", - "mask = X <= 0\n", - "mask" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, get the negative elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", - "\n", - "mask = X <= 0\n", - "X[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, get numbers higher than 5" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", - "\n", - "mask = X > 5\n", - "X[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, get numbers higher than the elements mean" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", - "\n", - "mask = X > X.mean()\n", - "X[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, get numbers equal to 2 or 10" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution", - "scrolled": true - }, - "outputs": [], - "source": [ - "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", - "\n", - "mask = (X == 2) | (X == 10)\n", - "X[mask]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Logic functions\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Given the X numpy array, return True if none of its elements is zero" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", - "\n", - "X.all()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, return True if any of its elements is zero" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\n", - "\n", - "X.any()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", - "\n", - "## Summary statistics" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Given the X numpy array, show the sum of its elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([3, 5, 6, 7, 2, 3, 4, 9, 4])\n", - "\n", - "#np.sum(X)\n", - "X.sum()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, show the mean value of its elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", - "\n", - "#np.mean(X)\n", - "X.mean()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, show the sum of its columns" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "X.sum(axis=0) # remember: axis=0 columns; axis=1 rows" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy matrix, show the mean value of its rows" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([\n", - " [1, 2, 3, 4],\n", - " [5, 6, 7, 8],\n", - " [9, 10, 11, 12],\n", - " [13, 14, 15, 16]\n", - "])\n", - "\n", - "X.mean(axis=1) # remember: axis=0 columns; axis=1 rows" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", - "\n", - "### Given the X numpy array, show the max value of its elements" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cell_type": "solution" - }, - "outputs": [], - "source": [ - "X = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", - "\n", - "#np.max(X)\n", - "X.max()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" - ] - } - ], - "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.8.1" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} + "nbformat": 4, + "nbformat_minor": 0, + "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.8.1" + }, + "colab": { + "name": "3. NumPy exercises.ipynb", + "provenance": [] + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "ot0MpMcYcFeW" + }, + "source": [ + "![rmotr](https://user-images.githubusercontent.com/7065401/52071918-bda15380-2562-11e9-828c-7f95297e4a82.png)\n", + "
\n", + "\n", + "# NumPy exercises\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "X9DeZ6dvcFec" + }, + "source": [ + "# Import the numpy package under the name np\n", + "import numpy as np\n", + "\n", + "# Print the numpy version and the configuration\n", + "print(np.__version__)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2qksBi9mcFed" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Array creation" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vu0YgD6dcFee" + }, + "source": [ + "### Create a numpy array of size 10, filled with zeros." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2yjDrB72cFee" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_fsdUxw8dFMJ" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "RBA6jgdpcFef" + }, + "source": [ + "#np.array([0] * 10)\n", + "np.zeros(10)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "liFpO5MacFef" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a numpy array with values ranging from 10 to 49" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9ll7g0L9cFeg" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8k4a0V88dY4j" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "EJ16m5JTcFeg" + }, + "source": [ + "np.arange(10,50)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Q1CxIJd5cFeg" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a numpy matrix of 2*2 integers, filled with ones." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "eJoD7CXucFeh" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MmLVn008gdzz" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "Vd4bAGKQcFeh" + }, + "source": [ + "np.ones([2,2], dtype=np.int)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pt9TKByFcFeh" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a numpy matrix of 3*2 float numbers, filled with ones." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mwX2TDTIcFeh" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HtQnbOIzghYZ" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "Tib_aU4XcFei" + }, + "source": [ + "np.ones([3,2], dtype=np.float)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kgWjZgkBcFei" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, create a new numpy array with the same shape and type as X, filled with ones." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bQgkvqdScFei" + }, + "source": [ + "x = np.arange(4, dtype=np.int)\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oRsEDrIpgkax" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "EfnXfcXRcFei" + }, + "source": [ + "X = np.arange(4, dtype=np.int)\n", + "\n", + "np.ones_like(X)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9xDg454ccFej" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, create a new numpy matrix with the same shape and type as X, filled with zeros." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "l91Zibj2cFek" + }, + "source": [ + "x = np.array([[1,2,3], [4,5,6]], dtype=np.int)\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OLeeRcSLgtVP" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "13ByZ8e1cFek" + }, + "source": [ + "X = np.array([[1,2,3], [4,5,6]], dtype=np.int)\n", + "\n", + "np.zeros_like(X)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Z_m--yilcFek" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a numpy matrix of 4*4 integers, filled with fives." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "I4aMQ5HscFel" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uKjhT3TAhae4" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "sKuctZwtcFel" + }, + "source": [ + "np.ones([4,4], dtype=np.int) * 5" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yv6ma7jTcFel" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, create a new numpy matrix with the same shape and type as X, filled with sevens." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5zlDqcDFcFel" + }, + "source": [ + "x = np.array([[2,3], [6,2]], dtype=np.int)\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "o9uVNaemhk2y" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "0Dx9i0JJcFem" + }, + "source": [ + "X = np.array([[2,3], [6,2]], dtype=np.int)\n", + "\n", + "np.ones_like(X) * 7" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jLWfHq-WcFem" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a 3*3 identity numpy matrix with ones on the diagonal and zeros elsewhere." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-OatrR8ocFen" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uecqC3rnhzmH" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "NnGpEvSncFen" + }, + "source": [ + "#np.eye(3)\n", + "np.identity(3)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bKhY2BcLcFeo" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a numpy array, filled with 3 random integer values between 1 and 10." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "aei1tYupcFeo" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qeUKLcz8h7GW" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "YUS7ACMOcFeo" + }, + "source": [ + "np.random.randint(10, size=3)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zbtVfEeMcFeo" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a 3\\*3\\*3 numpy matrix, filled with random float values." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mlISbQ6HcFep" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fbQUeoKqiCLC" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "XirwrcKocFep" + }, + "source": [ + "#np.random.random((3,3,3)) \n", + "np.random.randn(3,3,3) # 0 to 1 floats" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E8sL_R4dcFep" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X python list convert it to an Y numpy array" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "o8a0_trAcFep" + }, + "source": [ + "x = [1, 2, 3]\n", + "print(x, type(x))\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Diul9xsbiGwn" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "PGwtSOpncFeq" + }, + "source": [ + "X = [1, 2, 3]\n", + "print(X, type(X))\n", + "\n", + "Y = np.array(X)\n", + "print(Y, type(Y)) # different type" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GLGtgwygcFeq" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, make a copy and store it on Y." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SiNfrwracFeq" + }, + "source": [ + "x = np.array([5,2,3], dtype=np.int)\n", + "print(X, id(X))\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jz_xJasPiyqJ" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "80gpjFAtcFeq" + }, + "source": [ + "X = np.array([5,2,3], dtype=np.int)\n", + "print(X, id(X))\n", + "\n", + "Y = np.copy(X)\n", + "print(Y, id(Y)) # different id" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iq-0eOzxcFeq" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a numpy array with numbers from 1 to 10" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-uuc6XXbcFer" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0HYPN92Fi1_m" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "iybfDCjhcFer" + }, + "source": [ + "np.arange(1, 11)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "H2n9gq0bcFes" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a numpy array with the odd numbers between 1 to 10" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Blxa88I3cFes" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5t-aK7FPi7FS" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "YKUuX2pMcFes" + }, + "source": [ + "np.arange(1, 11, 2)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WR2SKPJocFes" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a numpy array with numbers from 1 to 10, in descending order." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "m1beUTnFcFet" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "puFIApavi-iV" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "fUSh40OCcFet" + }, + "source": [ + "np.arange(1, 11)[::-1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AtK4WTClcFet" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Create a 3*3 numpy matrix, filled with values ranging from 0 to 8" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9Z4sd5IjcFeu" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xJqM4hF6jCS_" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "_kQ-jByScFeu" + }, + "source": [ + "np.arange(9).reshape(3,3)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-lVZVQvgcFeu" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Show the memory size of the given Z numpy matrix" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bdLBU0oGcFeu" + }, + "source": [ + "z = np.zeros((10,10))\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jEHQv_hejH8D" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "scrolled": true, + "id": "8yv01ECdcFev" + }, + "source": [ + "Z = np.zeros((10,10))\n", + "\n", + "print(\"%d bytes\" % (Z.size * Z.itemsize))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JET2qIztcFev" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Array indexation\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "zCRo-yOolVOv" + }, + "source": [ + "X = np.array(['A','B','C','D','E'])\r\n", + "\r\n", + "# use 'x' for solving problems\r\n", + "x = X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Kxo-Ar9GcFev" + }, + "source": [ + "### Given the X numpy array, show it's first element" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "wIUBL4dTcFev" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VZ5arj24k0Ay" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "LCmz44kkcFew" + }, + "source": [ + "X[0]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2vn8KpdEcFew" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, show it's last element" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Zskkzl3AcFew" + }, + "source": [ + "# your code goes here\n" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZPcAMR57lode" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "PNP9Kv6bcFew" + }, + "source": [ + "#X[len(X)-1]\n", + "X[-1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "izQBJbMncFew" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, show it's first three elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "wcfdDe4bcFex" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dBjHmha9mMwO" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "IVRA4qKocFex" + }, + "source": [ + "X[0:3] # remember! elements start at zero index" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "I0f4gYNMcFex" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, show all middle elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "lOM3Up8ecFex" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AEPykDgImUDd" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "M_oKDjsJcFey" + }, + "source": [ + "X[1:-1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EpNOSx2LcFez" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, show the elements in reverse position" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YzAYLCOFcFez" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SVssDtChmXv_" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "8s0rLxllcFez" + }, + "source": [ + "X[::-1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FcNO2Y7DcFez" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, show the elements in an odd position" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "CPxzGAZUcFe0" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7SBRVvEPmbZN" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "qmFp5o9jcFe1" + }, + "source": [ + "#X[[0, 2, -1]]\n", + "X[::2]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "M-jQWZ7XpC4f" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\r\n", + "\r\n", + "## Array Indexation (Matrix)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "IkLoHqN1mpul" + }, + "source": [ + "X = np.array([\r\n", + " [1, 2, 3, 4],\r\n", + " [5, 6, 7, 8],\r\n", + " [9, 10, 11, 12],\r\n", + " [13, 14, 15, 16]\r\n", + "])\r\n", + "\r\n", + "# use 'x' for solve problems\r\n", + "x = X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CTMJniklcFe1" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, show the first row elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cyZ6589dcFe2" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CepuptpHnIUd" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "ClDukQW0cFe2" + }, + "source": [ + "X[0]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UAe9XKAkcFe2" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, show the last row elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JjRX3IG6cFe2" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zRLYi6wuoK4u" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "oIgIxyfpcFe2" + }, + "source": [ + "X[-1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NO9mz43ycFe3" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, show the first element on first row" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5dxUoByocFe3" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WFjJhatyoaEE" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "_AHjrMBacFe3" + }, + "source": [ + "#X[0][0]\n", + "X[0, 0]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RfL_7qeScFe3" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, show the last element on last row" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ddUiKHIHcFe4" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TplkU5JRof4C" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "5xjSv5WAcFe4" + }, + "source": [ + "#X[-1][-1]\n", + "X[-1, -1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bXQOffUvcFe4" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, show the middle row elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "FXacTBMHcFe4" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "veb8ZeJZojN8" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "mlNIl2MfcFe5" + }, + "source": [ + "#X[1:-1][1:-1] wrong!\n", + "X[1:-1, 1:-1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LRTp7B67cFe5" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, show the first two elements on the first two rows" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ATv2UNUYcFe5" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "myGJLoEZornd" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "UJhp-h0XcFe5" + }, + "source": [ + "#X[:2][:2] wrong!\n", + "#X[0:2, 0:2]\n", + "X[:2, :2]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SuS92zWHcFe6" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, show the last two elements on the last two rows" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cbS-v_DpcFe6" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UUHZGbaSovRM" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "GId0H-K8cFe6" + }, + "source": [ + "X[2:, 2:]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1mXDU8uucFe7" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Array manipulation\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Pcll4H6dcFe7" + }, + "source": [ + "### Convert the given integer numpy array to float" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "RqY0XUxHcFe7" + }, + "source": [ + "x = [-5, -3, 0, 10, 40]\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PRac47Obpx3V" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "o2IRR39pcFe7" + }, + "source": [ + "X = [-5, -3, 0, 10, 40]\n", + "\n", + "np.array(X, np.float)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SDYM-dsacFe8" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Reverse the given numpy array (first element becomes last)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "viS8G1iDcFe8" + }, + "source": [ + "x = [-5, -3, 0, 10, 40]\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E2iR0i8Zp0NN" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "eL15nDh0cFe8" + }, + "source": [ + "X = [-5, -3, 0, 10, 40]\n", + "\n", + "X[::-1]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hjSXjb16cFe8" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Order (sort) the given numpy array" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "_gtxG92qcFe8" + }, + "source": [ + "x = [0, 10, -5, 40, -3]\n", + "\n", + "# your code goes here\n", + "\n", + "x" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VvDchpXIp3Ru" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "H5RbHF11cFe9" + }, + "source": [ + "X = [0, 10, -5, 40, -3]\n", + "\n", + "X.sort()\n", + "\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "k7f9DwwwcFe9" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, set the fifth element equal to 1" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tpf4s-8IcFe9" + }, + "source": [ + "x = np.zeros(10)\n", + "\n", + "# your code goes here\n", + "\n", + "x" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "solkzGkjp5bl" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "azyf7yEocFe9" + }, + "source": [ + "X = np.zeros(10)\n", + "\n", + "X[4] = 1\n", + "\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3b3hznRxcFe-" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, change the 50 with a 40" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "okN8gcfxcFe-" + }, + "source": [ + "x = np.array([10, 20, 30, 50])\n", + "\n", + "# your code goes here\n", + "\n", + "x" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hZa7mvX6p7am" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "vheYsbKfcFe-" + }, + "source": [ + "X = np.array([10, 20, 30, 50])\n", + "\n", + "X[3] = 40\n", + "\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ygMw_GfccFe-" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, change the last row with all 1" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "EpfyUrljcFe_" + }, + "source": [ + "x = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "# your code goes here\n", + "\n", + "x" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0t0VNUi0p86w" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "iO61VsgDcFe_" + }, + "source": [ + "X = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "X[-1] = np.array([1, 1, 1, 1])\n", + "\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WNA3j53JcFe_" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, change the last item on the last row with a 0" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3iBzAjzscFe_" + }, + "source": [ + "x = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "# your code goes here\n", + "\n", + "x" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WiE6fLFRp-bt" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "D9I3m5bwcFfA" + }, + "source": [ + "X = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "X[-1, -1] = 0\n", + "\n", + "X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZHM-MeoZcFfA" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, add 5 to every element" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "OHVyEbc5cFfB" + }, + "source": [ + "X = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xzzlWu5rp_JU" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "GUfqe-FjcFfB" + }, + "source": [ + "X = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "X + 5" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CykPiLcAcFfB" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Boolean arrays _(also called masks)_\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xuOLsTrNvLKT" + }, + "source": [ + "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\r\n", + "\r\n", + "# Use 'x' to solve problems\r\n", + "x = X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HoPkYXG_cFfB" + }, + "source": [ + "### Given the X numpy array, make a mask showing negative elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XWMjybDZcFfC" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BXKEchU3u_dR" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "RGjmvrDecFfC" + }, + "source": [ + "mask = X <= 0\n", + "mask" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jeRE15CgcFfC" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, get the negative elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tuDRCLXTcFfH" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "g2wkqWY3vfYx" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "JEpsRMtKcFfH" + }, + "source": [ + "mask = X <= 0\n", + "X[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EISvS1vPcFfH" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, get numbers higher than 5" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "p9zi7dI_cFfI" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UkGuUXwXviBP" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "x-cAKotEcFfI" + }, + "source": [ + "mask = X > 5\n", + "X[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O5-MJJ03cFfI" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, get numbers higher than the elements mean" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oNTt-Dz7cFfJ" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FDcjUo7bvrj4" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "WdX21zlbcFfJ" + }, + "source": [ + "mask = X > X.mean()\n", + "X[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ONrgcp7JcFfK" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, get numbers equal to 2 or 10" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "FQXTU1jocFfL" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "t4Q55u6-vxSP" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "scrolled": true, + "id": "jnj-KQllcFfL" + }, + "source": [ + "mask = (X == 2) | (X == 10)\n", + "X[mask]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rqJMtMVpcFfL" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Logic functions\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A60geikzv6Ie" + }, + "source": [ + "X = np.array([-1, 2, 0, -4, 5, 6, 0, 0, -9, 10])\r\n", + "\r\n", + "# use 'x' to solve problems\r\n", + "x = X" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iYc-OKiHcFfM" + }, + "source": [ + "### Given the X numpy array, return True if none of its elements is zero" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jvOqSx8tcFfM" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O7yvCc66wP60" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "OuyR5_i-cFfM" + }, + "source": [ + "X.all()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7uUlYl6scFfM" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, return True if any of its elements is zero" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XauUpImVcFfM" + }, + "source": [ + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1g1MR51AwSJ9" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "mQgYbz17cFfN" + }, + "source": [ + "X.any()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "srydAd9hcFfN" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)\n", + "\n", + "## Summary statistics" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Zzcd5D8YcFfN" + }, + "source": [ + "### Given the X numpy array, show the sum of its elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ypttZVjxcFfN" + }, + "source": [ + "x = np.array([3, 5, 6, 7, 2, 3, 4, 9, 4])\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IsOzRpVxxEeN" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "6E1QnHHbcFfN" + }, + "source": [ + "X = np.array([3, 5, 6, 7, 2, 3, 4, 9, 4])\n", + "\n", + "#np.sum(X)\n", + "X.sum()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CSW8V5izcFfO" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, show the mean value of its elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "FroRPgR0cFfO" + }, + "source": [ + "x = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nx7wc0jpxKUE" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "H1hKstiFcFfO" + }, + "source": [ + "X = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", + "\n", + "#np.mean(X)\n", + "X.mean()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lXNocrDKcFfO" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, show the sum of its columns" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZP07XkTFcFfO" + }, + "source": [ + "x = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "S0oGR69txNZV" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "AKxvtqbmcFfP" + }, + "source": [ + "X = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "X.sum(axis=0) # remember: axis=0 columns; axis=1 rows" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1L0uaHgacFfP" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy matrix, show the mean value of its rows" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "NLLOKSlncFfP" + }, + "source": [ + "x = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bQOb8JcnxOQ9" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "ii_Cu24IcFfP" + }, + "source": [ + "X = np.array([\n", + " [1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12],\n", + " [13, 14, 15, 16]\n", + "])\n", + "\n", + "X.mean(axis=1) # remember: axis=0 columns; axis=1 rows" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eQrRTfpLcFfQ" + }, + "source": [ + "![green-divider](https://user-images.githubusercontent.com/7065401/52071924-c003ad80-2562-11e9-8297-1c6595f8a7ff.png)\n", + "\n", + "### Given the X numpy array, show the max value of its elements" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "vVJqtyeecFfQ" + }, + "source": [ + "X = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", + "\n", + "# your code goes here" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kpZColgxxO_r" + }, + "source": [ + "#### _Answer_:" + ] + }, + { + "cell_type": "code", + "metadata": { + "cell_type": "solution", + "id": "EboF5e86cFfR" + }, + "source": [ + "X = np.array([1, 2, 0, 4, 5, 6, 0, 0, 9, 10])\n", + "\n", + "#np.max(X)\n", + "X.max()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sky14SRmcFfR" + }, + "source": [ + "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" + ] + } + ] +} \ No newline at end of file From 192ddb57c3339f90a51216b0c6efc746d74a1f7a Mon Sep 17 00:00:00 2001 From: webdnd <76805904+webdnd@users.noreply.github.com> Date: Tue, 2 Feb 2021 19:24:41 -0800 Subject: [PATCH 2/2] Collapsed Expanded Sections --- 3. NumPy exercises.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3. NumPy exercises.ipynb b/3. NumPy exercises.ipynb index d115510..a221e21 100644 --- a/3. NumPy exercises.ipynb +++ b/3. NumPy exercises.ipynb @@ -2549,7 +2549,7 @@ "id": "Sky14SRmcFfR" }, "source": [ - "![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" + "## ![purple-divider](https://user-images.githubusercontent.com/7065401/52071927-c1cd7100-2562-11e9-908a-dde91ba14e59.png)" ] } ]