#!/usr/bin/env python # coding: utf-8 # # Module 3 # # ## Video 11: NumPy # **Python for the Energy Industry** # # ## Importing Modules # # Modules can be accessed by 'importing' them. # In[2]: import numpy as np print('square root of 10:',np.sqrt(10)) # Individual functions can be imported from a module without importing the entire module: # In[3]: from numpy.random import randint print('a random integer up to 100:', randint(100)) # You can also import everything from a module: # In[4]: from numpy import * print('e ^ 3:', exp(3.0)) # 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. # # ## NumPy Arrays # # The numpy 'array' is another way of storing data. It is similar to a list, but designed for working with numerical data. # In[5]: # creating an array from a list my_list = [2.0,5.0,7.0,10.0] my_array = np.array(my_list) print('array:',my_array) print('2nd element:',my_array[1]) # A key difference between lists and arrays is how they deal with mathematical operations: # In[6]: print('my_list * 2:', my_list * 2) print('my_array * 2:', my_array * 2) # Mathematical operations on arrays are applied to each element in the array: # In[7]: print('my_array squared:', my_array**2) print('my_array square rooted:', np.sqrt(my_array)) # Operations between two arrays of the same length: # In[8]: other_array = np.array([1.0,7.0,1.0,2.0]) print('my_array:',my_array) print('other_array:',other_array) print('my_array + other_array:',my_array + other_array) print('my_array * other_array:',my_array * other_array) # ## Generating Arrays # # Here are a few different ways of making arrays, besides specifically defining them: # In[9]: # array equivalent of 'range': print(np.arange(0.0,10.0,1.5)) # In[10]: # array of zeros print(np.zeros(5)) # In[11]: # array of ones print(np.ones(5)) # In[12]: # array of random numbers print(np.random.rand(5)) # ## Mathematical Functions # # Numpy comes with a number of mathematical functions that can be applied element-wise over an array. Some examples: # In[13]: x = np.arange(1,6) print(x) # In[14]: # trigonometric functions print(np.sin(x)) # In[15]: # logarithm print(np.log(x)) # In[16]: # rounding to 3 decimal places print(np.round(np.log(x), 3)) # There are also some useful functions that perform a calculation using the whole array: # In[17]: x = np.arange(1,6) # mean print(np.mean(x)) # standard deviation print(np.std(x)) # ### Exercise # # 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. # # 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. # In[ ]: