This notebook was written by Kayla Leonard (June 2019) to supplement the currently existing IceCube Bootcamp Tutorials.
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/.
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").
It is standard to name the package "np" when you import it.
Feel free to add additional cells and change the numbers to try your own
Topics include:
import numpy as np
np.pi
np.e
np.inf # infinity
If you are looking for standard trigometric functions in python, this package is probably what you want to use.
np.cos(np.pi)
np.sin(np.pi/2)
The trig functions assume the input is in radians, so if you'd like to use degrees, there are useful functions to convert.
# These are equivalent functions
print(np.rad2deg(np.pi/2))
print(np.degrees(np.pi/2))
# These are equivalent functions
print(np.deg2rad(45))
print(np.radians(45))
np.sin(np.deg2rad(45)) # convets 45 degrees to radians, then takes the sin of that
NumPy has an exentsive list of functions helpful random nubmers, probability, and statistics.
np.random.random() # picks a random number between 0 and 1
# rerun this cell several times and you will see the value change each time
np.random.random()
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.
np.random.normal(0,10) # draws a random number from a gaussian distribution centered at 0 with standard deviation 10
np.random.normal(0,10,10) # we can give it an additonal argument that is the length of the array we want
There are many other distributions available like binomial, chi-squared, poisson, gamma, etc. https://docs.scipy.org/doc/numpy/reference/routines.random.html
There are several quick ways to initialize numpy arrays.
np.linspace(0,20,num=21) # returns an array of evenly spaced values from 0 to 20.
# Note: This will include both the start and end points so you must make the number n+1
np.linspace(0,100,num=26) # it will calculate the step size, it doesn't need to be 1
np.linspace(0,1,num=17) # it can also handle decimal numbers
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
np.arange(0,100,4) # If you know the stepsize you want but not the number of items, you can use arange
# Note: This one does not include the end point in the list
np.zeros(10) # creates an array of 10 items, where all entries are zero
np.ones(10) # creates an array of 10 items, where all entries are one
Arrays are useful because they allow for elementwise manipulation of numbers that are prohibited with python lists.
[1,2,3,4]**2 # This will give us an error because we can't square a list
np.array([1,2,3,4])**2 # this will square each element in the array
You can also use elementwise calculations of two arrays of the same length:
np.array([1,2,3,4])+np.array([5,6,7,8])
np.array([1,2,3,4])*np.array([5,6,7,8])
You can also run numpy functions and it will apply it to each element.
np.sqrt([1,4,9,10])
np.sin([0,np.pi/4,np.pi/2])
Given an array of values, we can calculate all the standard things like mean, median, etc.
scores = np.array([95,41,72,100,80,97,95])
print(scores)
print(type(scores))
# Mean
print(np.mean(scores))
print(np.average(scores))
# Median
print(np.median(scores))
# calculate the value of the 90th percentile
print(np.percentile(scores,90))
# Standard Deviation
print(np.std(scores))
Masks are arrays of True or False that can be used to identify certain elements in an array.
# Let's figure out which elements in this array are not zero:
my_array = np.array([1,2,0,3,0,4,0])
my_mask = my_array!=0
print(my_mask)
# We can also see which elements are greater than 3
my_mask2 = my_array>3
print(my_mask2)
Masks allow you to extract only certain elements using the following notation:
my_array[my_mask]