{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 3\n", "\n", "## Video 11: NumPy\n", "**Python for the Energy Industry**\n", "\n", "## Importing Modules\n", "\n", "Modules can be accessed by 'importing' them. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "square root of 10: 3.1622776601683795\n" ] } ], "source": [ "import numpy as np\n", "\n", "print('square root of 10:',np.sqrt(10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Individual functions can be imported from a module without importing the entire module:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a random integer up to 100: 70\n" ] } ], "source": [ "from numpy.random import randint\n", "\n", "print('a random integer up to 100:', randint(100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also import everything from a module:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "e ^ 3: 20.085536923187668\n" ] } ], "source": [ "from numpy import *\n", "\n", "print('e ^ 3:', exp(3.0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This lets you access functions without the 'np.' prefix, but be careful about conflicts between functions defined in the module and functions you've defined.\n", "\n", "## NumPy Arrays\n", "\n", "The numpy 'array' is another way of storing data. It is similar to a list, but designed for working with numerical data." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array: [ 2. 5. 7. 10.]\n", "2nd element: 5.0\n" ] } ], "source": [ "# creating an array from a list\n", "my_list = [2.0,5.0,7.0,10.0]\n", "my_array = np.array(my_list)\n", "print('array:',my_array)\n", "print('2nd element:',my_array[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A key difference between lists and arrays is how they deal with mathematical operations:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_list * 2: [2.0, 5.0, 7.0, 10.0, 2.0, 5.0, 7.0, 10.0]\n", "my_array * 2: [ 4. 10. 14. 20.]\n" ] } ], "source": [ "print('my_list * 2:', my_list * 2)\n", "print('my_array * 2:', my_array * 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mathematical operations on arrays are applied to each element in the array:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_array squared: [ 4. 25. 49. 100.]\n", "my_array square rooted: [1.41421356 2.23606798 2.64575131 3.16227766]\n" ] } ], "source": [ "print('my_array squared:', my_array**2)\n", "print('my_array square rooted:', np.sqrt(my_array))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Operations between two arrays of the same length:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_array: [ 2. 5. 7. 10.]\n", "other_array: [1. 7. 1. 2.]\n", "my_array + other_array: [ 3. 12. 8. 12.]\n", "my_array * other_array: [ 2. 35. 7. 20.]\n" ] } ], "source": [ "other_array = np.array([1.0,7.0,1.0,2.0])\n", "print('my_array:',my_array)\n", "print('other_array:',other_array)\n", "print('my_array + other_array:',my_array + other_array)\n", "print('my_array * other_array:',my_array * other_array)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generating Arrays\n", "\n", "Here are a few different ways of making arrays, besides specifically defining them:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 1.5 3. 4.5 6. 7.5 9. ]\n" ] } ], "source": [ "# array equivalent of 'range':\n", "print(np.arange(0.0,10.0,1.5))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0. 0. 0. 0.]\n" ] } ], "source": [ "# array of zeros\n", "print(np.zeros(5))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 1. 1. 1. 1.]\n" ] } ], "source": [ "# array of ones\n", "print(np.ones(5))" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0.50999145 0.44111399 0.99652254 0.16413813 0.64181847]\n" ] } ], "source": [ "# array of random numbers\n", "print(np.random.rand(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mathematical Functions\n", "\n", "Numpy comes with a number of mathematical functions that can be applied element-wise over an array. Some examples:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5]\n" ] } ], "source": [ "x = np.arange(1,6)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427]\n" ] } ], "source": [ "# trigonometric functions\n", "print(np.sin(x))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.69314718 1.09861229 1.38629436 1.60943791]\n" ] } ], "source": [ "# logarithm\n", "print(np.log(x))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.693 1.099 1.386 1.609]\n" ] } ], "source": [ "# rounding to 3 decimal places\n", "print(np.round(np.log(x), 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are also some useful functions that perform a calculation using the whole array:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.0\n", "1.4142135623730951\n" ] } ], "source": [ "x = np.arange(1,6)\n", "\n", "# mean\n", "print(np.mean(x))\n", "\n", "# standard deviation\n", "print(np.std(x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Let's say that you were interested in estimating the total value of crude exported from some country on a typical day. All you know is that the price per barrel on a given day can be approximated as a random number between 60 and 62, and the number of barrels exported is 10 to the power of a random number between 6 and 7. \n", "\n", "Create two arrays of 100 random numbers representing 100 days for each of these quantities, and use these to calculate the mean and standard deviation of the total value of exports over that time period." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 4 }