Basic Python and Numpy Coding

profilenieyanan
NumpyExercise.ipynb

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Numpy Class Exercises\n", "The code is removed but the output is provided for you to check the correctness of your work. You can download 2 copies of the notebook to compare the result with the original copy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array Creation Important Attributes and Functions" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 2.6 7.8 9.3 11.6]\n" ] } ], "source": [ "# Create an 1-dimensional array with elements 2.6, 7.8, 9.3. 11.6 and print it\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2]\n", " [3 4]\n", " [5 6]]\n" ] } ], "source": [ "# Create a 2-dimensional (3 X 2) array with any number you like. Pass the values in as a tuple of 3 tuples\n", "# Change the tuples to lists or a mixture of lists and tuples and recreate the numpy array\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[ 1.3, 21. , 11. ],\n", " [72. , 4.4, 9. ]],\n", "\n", " [[15. , 62. , 9.9],\n", " [71. , 28. , 5. ]]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a 3-dimensional array with nested lists, tuples or a combination of them.\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(3, 2)\n", "2\n", "6\n", "(2, 2, 3)\n" ] } ], "source": [ "# print out the shape, number of dimensions and size of the arrays you created before\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.07142857 0.14285714 0.21428571 0.28571429 0.35714286\n", " 0.42857143 0.5 0.57142857 0.64285714 0.71428571 0.78571429\n", " 0.85714286 0.92857143 1. ]\n" ] } ], "source": [ "# Create a one-dimensional array 'a' using linspace() function, \n", "# start with 0 and end with 1. Create 15 elements\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.42857142857142855\n" ] } ], "source": [ "# Get the 7th element of 'a'\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 3 6 9 12 15 18 21 24 27]\n", " [30 33 36 39 42 45 48 51 54]]\n" ] } ], "source": [ "# Create a one-dimensional array using arange() function starting at 3 and ending at 54. \n", "# The elements are the multiples of 3. Reshape it to a 3X6 array. Name the array 'b'\n", "# When calling reshape(), can you passing -1 as an argument? Try it\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[30 33]\n", "[[12 15 18]\n", " [30 33 36]\n", " [48 51 54]]\n", "[ 3 6 9 12 15 18]\n" ] } ], "source": [ "# Get number 30 and 33 from 'b'\n", "\n", "# Get the last 3 columns from 'b'\n", "\n", "# only one index is needed if the entire row is retrieved\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[10, 10, 10],\n", " [10, 10, 10],\n", " [10, 10, 10]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a 3X3 array using full() function that takes 10 as the values for all elements\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0. 0. 0. 0.]\n", " [ 0. 22. 22. 0.]\n", " [ 0. 0. 0. 0.]\n", " [ 1. 1. 1. 33.]\n", " [ 1. 1. 1. 33.]\n", " [ 1. 1. 1. 33.]]\n", "[[ 0. 0. 0. 0. 1. 1. 1. 33.]\n", " [ 0. 22. 22. 0. 1. 1. 1. 33.]\n", " [ 0. 0. 0. 0. 1. 1. 1. 33.]]\n" ] } ], "source": [ "# Create a zero array with dimension 3X4 in variable 'arr0'. \n", "# Create another 3X4 array with all 1s in variable 'arr1'\n", "\n", "# Set the 2 middle values of arr0 in row 2 to 22\n", "\n", "# Set the last column of arr1 to 33\n", "\n", "# Use vstack function to stack up the 2 arrays vertically into 'arr3'\n", "\n", "# Use concatenate function to merge the 2 arrays horizontally into 'arr4'\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array Math and Stats" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 0.6 0.6 0.6 0.6]\n", " [ 0.6 22.6 22.6 0.6]\n", " [ 0.6 0.6 0.6 0.6]]\n" ] } ], "source": [ "# Simple math operation. Add 0.6 to all elements of 'arr0'\n", "\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1.6 1.6 1.6 33.6]\n", " [ 1.6 23.6 23.6 33.6]\n", " [ 1.6 1.6 1.6 33.6]]\n" ] } ], "source": [ "# Add 'arr0' and 'arr1'\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1. , 1. , 1. , 5.74456265],\n", " [1. , 1. , 1. , 5.74456265],\n", " [1. , 1. , 1. , 5.74456265]])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get the square root of 'arr0'\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "51.20000000000001\n", "0.6\n", "22.6\n", "4.2666666666666675\n" ] } ], "source": [ "# Get sum, min, max, mean of 'arr0'\n", "\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 22. 22. 0.]\n", "[0. 0. 0. 0.]\n", "[ 0. 22. 22. 0.]\n", "[0. 7.33333333 7.33333333 0. ]\n" ] } ], "source": [ "# Get column sum, min, max, mean of 'arr0' \n", "\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 44. 0.]\n", "[0. 0. 0.]\n", "[ 0. 22. 0.]\n", "[ 0. 11. 0.]\n" ] } ], "source": [ "# Get row sum, min, max, mean of 'arr0'\n" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 1., 1., 33.],\n", " [ 2., 2., 2., 66.],\n", " [ 3., 3., 3., 99.]])" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get cumulative sum of 'arr1' along the columns\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random Number Generation" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[51 74 77 22 90]\n", " [24 63 89 96 43]\n", " [24 23 86 43 58]]\n" ] } ], "source": [ "# Create a 3X5 array with random integers between 1 and 100. Your result will be different\n", "# Assign it to a variable 'rand_array' to be used later\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[22, 51, 74, 77, 90],\n", " [24, 43, 63, 89, 96],\n", " [23, 24, 43, 58, 86]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sort 'rand_array' along the row. the result should be consistent with your array\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[22, 24, 43, 58, 86],\n", " [23, 43, 63, 77, 90],\n", " [24, 51, 74, 89, 96]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sort the random array along the column. the result should be according to your array\n" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.80223791, 0.310615 ],\n", " [0.3799499 , 0.20464882]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Generate a random 2 by 2 array with elements between 0 and 1. Your elements will differ\n", "# from mine. Execute the code multiple times, ensure the values change with execution.\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([117.64052346, 104.00157208, 109.78737984, 122.40893199,\n", " 118.6755799 , 90.2272212 , 109.50088418, 98.48642792])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# set a random seed to be 0. Create an 1 X 8 array by sampling from a normal distrubtion\n", "# with mean 100 and standard deviation 10. Assign it to a variable 'arr_normal'. \n", "# With the same seed value, your result will be the same as mine below.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array Indexing, Slicing and Setting" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "109.78737984105739" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# from the array 'arr_normal', get value at index 2\n" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[104.00157208 109.78737984]\n", "[104.00157208 109.78737984]\n", "[109.78737984 122.40893199 118.6755799 90.2272212 109.50088418\n", " 98.48642792]\n", "[104.00157208 109.78737984 122.40893199 118.6755799 90.2272212\n", " 109.50088418]\n", "[117.64052346 122.40893199 109.50088418]\n", "[ 98.48642792 109.50088418 90.2272212 118.6755799 122.40893199\n", " 109.78737984 104.00157208 117.64052346]\n" ] } ], "source": [ "# User 'arr_normal':\n", "# Get the 2nd and 3rd items from the left and from the right\n", "\n", "\n", "# Get items from the 3rd to the end\n", "\n", "# Get items from the 2nd to the one before the end\n", "\n", "# Get every 3rd item starting from the first one\n", "\n", "# Get elements back in reverse order\n" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([117.64052346, 104.00157208, 109.78737984, 122.40893199,\n", " 118.6755799 , 100. , 100. , 98.48642792])" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Set the values in 6th and 7th element in 'arr_normal' to be 100\n" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[188, 112, 158, 165, 139],\n", " [187, 146, 188, 181, 137],\n", " [125, 177, 172, 109, 120],\n", " [180, 169, 179, 147, 164]])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# generate an integer 4X5 array with random integers between 100 and 200\n", "# assign it to a variable 'arr_int'.\n", "\n" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[187 146 188 181 137]\n", "[[125 177 172 109 120]\n", " [180 169 179 147 164]]\n" ] } ], "source": [ "# get the 2nd row. Again your result should be consistant with your array\n", "\n", "# get the 3rd and 4th rows\n" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "158\n", "[112 146 177 169]\n", "[[146 188 181]\n", " [177 172 109]]\n" ] } ], "source": [ "# Your result should be consistant with your array\n", "# get the element in the first row and 3rd column\n", "\n", "# get the second column\n", "\n", "# get the middle 6 elements \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean Indexing and Fancy Indexing" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([188, 158, 165, 187, 188, 181, 177, 172, 180, 169, 179, 164])" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# from 'arr_int', get elements >= 150\n", "# Again your result should be consistant with your array\n" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([112, 109, 120])" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# from 'arr_int', get elements between 100 and 120\n" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "# Create a 1X5 array named 'label'\n", "label = np.array(['a', 'b', 'c', 'd', 'e'])" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[188, 139],\n", " [187, 137],\n", " [125, 120],\n", " [180, 164]])" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# select values corresponding to labels 'a', or 'e' in 'arr_int'\n", "# Again your result should be consistant with your array\n" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([100. , 104.00157208, 109.78737984, 109.78737984,\n", " 100. , 118.6755799 , 104.00157208])" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Fancy indexing\n", "# use 'arr_normal' created above\n", "# Get values in index 6, 1, 2, 2, 5, 4, 1 in this order\n" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[187, 146, 188, 181, 137],\n", " [180, 169, 179, 147, 164],\n", " [125, 177, 172, 109, 120],\n", " [187, 146, 188, 181, 137],\n", " [125, 177, 172, 109, 120]])" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# From 'arr_int', get the rows in this index order: 1, 3, 2, 1, 2\n" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[112, 165, 158],\n", " [146, 181, 188],\n", " [177, 109, 172],\n", " [169, 147, 179]])" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# From 'arr_int', Get the 2nd, 4th and 3rd columns\n" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[188, 1, 158, 165, 1],\n", " [187, 146, 188, 181, 1],\n", " [ 1, 177, 172, 1, 1],\n", " [180, 169, 179, 147, 164]])" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# use numpy where to set the values in 'arr_int' to be 1 if less than 140\n" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }