{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# NumPy\n", "\n", "This notebook was written by Kayla Leonard (June 2019) to supplement the currently existing IceCube Bootcamp Tutorials.\n", "\n", "NumPy is an external python package that has very useful mathematical tools. It's documentation can be found at https://docs.scipy.org/doc/numpy/reference/.\n", "\n", "Realistically the best way to find a new function is just to Google \"numpy\" with what ever you are trying to do (for example \"numpy convert radians to degrees\"). \n", "\n", "It is standard to name the package \"np\" when you import it.\n", "\n", "**** Feel free to add additional cells and change the numbers to try your own ****\n", "\n", "**Topics include:**\n", " - Special numbers (pi, infinity, etc.)\n", " - Trigonometry (sin, cos, etc.)\n", " - Random Numbers\n", " - Arrays (NumPy's version of lists)\n", " - Dataset calculations (mean, median, etc.)\n", " - Masks" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Special Numbers" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.141592653589793" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.pi" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.718281828459045" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.e" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "inf" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.inf # infinity" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Trigonometry\n", "\n", "If you are looking for standard trigometric functions in python, this package is probably what you want to use." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1.0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.cos(np.pi)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sin(np.pi/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The trig functions assume the input is in radians, so if you'd like to use degrees, there are useful functions to convert." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "90.0\n", "90.0\n" ] } ], "source": [ "# These are equivalent functions\n", "print(np.rad2deg(np.pi/2))\n", "print(np.degrees(np.pi/2))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.7853981633974483\n", "0.7853981633974483\n" ] } ], "source": [ "# These are equivalent functions\n", "print(np.deg2rad(45))\n", "print(np.radians(45))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7071067811865475" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sin(np.deg2rad(45)) # convets 45 degrees to radians, then takes the sin of that" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Random Numbers\n", "\n", "NumPy has an exentsive list of functions helpful random nubmers, probability, and statistics." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.3883925064755209" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.random() # picks a random number between 0 and 1" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9854419945815169" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# rerun this cell several times and you will see the value change each time\n", "np.random.random()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want your results to be re-producible, you can run np.random.seed(13) or with your favorite number, and then every time you re-run the cell you will get the same number." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-6.290842493187123" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.normal(0,10) # draws a random number from a gaussian distribution centered at 0 with standard deviation 10" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 3.18359367, 10.65710673, 15.70076931, 6.11426628,\n", " -5.56016407, 2.38773831, -9.34114013, -12.87750992,\n", " 3.15110509, -1.97483647])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.random.normal(0,10,10) # we can give it an additonal argument that is the length of the array we want" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are many other distributions available like binomial, chi-squared, poisson, gamma, etc.\n", "https://docs.scipy.org/doc/numpy/reference/routines.random.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arrays\n", "\n", "There are several quick ways to initialize numpy arrays." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,\n", " 13., 14., 15., 16., 17., 18., 19., 20.])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linspace(0,20,num=21) # returns an array of evenly spaced values from 0 to 20.\n", "# Note: This will include both the start and end points so you must make the number n+1 " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0., 4., 8., 12., 16., 20., 24., 28., 32., 36., 40.,\n", " 44., 48., 52., 56., 60., 64., 68., 72., 76., 80., 84.,\n", " 88., 92., 96., 100.])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linspace(0,100,num=26) # it will calculate the step size, it doesn't need to be 1" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.0625, 0.125 , 0.1875, 0.25 , 0.3125, 0.375 , 0.4375,\n", " 0.5 , 0.5625, 0.625 , 0.6875, 0.75 , 0.8125, 0.875 , 0.9375,\n", " 1. ])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.linspace(0,1,num=17) # it can also handle decimal numbers" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 100. , 125.89254118, 158.48931925, 199.5262315 ,\n", " 251.18864315, 316.22776602, 398.10717055, 501.18723363,\n", " 630.95734448, 794.32823472, 1000. , 1258.92541179,\n", " 1584.89319246, 1995.26231497, 2511.88643151, 3162.27766017,\n", " 3981.07170553, 5011.87233627, 6309.5734448 , 7943.28234724,\n", " 10000. ])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.logspace(2,4,21) # similar to linspace but returns values that are evenly spaced in log space from 10**2 t o 10**4" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64,\n", " 68, 72, 76, 80, 84, 88, 92, 96])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.arange(0,100,4) # If you know the stepsize you want but not the number of items, you can use arange\n", "# Note: This one does not include the end point in the list" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.zeros(10) # creates an array of 10 items, where all entries are zero" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones(10) # creates an array of 10 items, where all entries are one" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arrays are useful because they allow for elementwise manipulation of numbers that are prohibited with python lists." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for ** or pow(): 'list' and 'int'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m \u001b[0;31m# This will give us an error because we can't square a list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'list' and 'int'" ] } ], "source": [ "[1,2,3,4]**2 # This will give us an error because we can't square a list" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 9, 16])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1,2,3,4])**2 # this will square each element in the array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use elementwise calculations of two arrays of the same length:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 6, 8, 10, 12])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1,2,3,4])+np.array([5,6,7,8])" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 5, 12, 21, 32])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array([1,2,3,4])*np.array([5,6,7,8])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also run numpy functions and it will apply it to each element." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1. , 2. , 3. , 3.16227766])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sqrt([1,4,9,10])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.70710678, 1. ])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sin([0,np.pi/4,np.pi/2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic data set calculations\n", "\n", "Given an array of values, we can calculate all the standard things like mean, median, etc." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 95 41 72 100 80 97 95]\n", "\n" ] } ], "source": [ "scores = np.array([95,41,72,100,80,97,95])\n", "print(scores)\n", "print(type(scores))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "82.85714285714286\n", "82.85714285714286\n" ] } ], "source": [ "# Mean\n", "print(np.mean(scores))\n", "print(np.average(scores))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "95.0\n" ] } ], "source": [ "# Median\n", "print(np.median(scores))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "98.2\n" ] } ], "source": [ "# calculate the value of the 90th percentile\n", "print(np.percentile(scores,90))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "19.51869851800408\n" ] } ], "source": [ "# Standard Deviation\n", "print(np.std(scores))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Masks\n", "\n", "Masks are arrays of True or False that can be used to identify certain elements in an array." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ True True False True False True False]\n" ] } ], "source": [ "# Let's figure out which elements in this array are not zero:\n", "my_array = np.array([1,2,0,3,0,4,0])\n", "my_mask = my_array!=0\n", "print(my_mask)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[False False False False False True False]\n" ] } ], "source": [ "# We can also see which elements are greater than 3\n", "my_mask2 = my_array>3\n", "print(my_mask2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Masks allow you to extract only certain elements using the following notation:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_array[my_mask]" ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }