import numpy as np
print('square root of 10:',np.sqrt(10))
square root of 10: 3.1622776601683795
Individual functions can be imported from a module without importing the entire module:
from numpy.random import randint
print('a random integer up to 100:', randint(100))
a random integer up to 100: 70
You can also import everything from a module:
from numpy import *
print('e ^ 3:', exp(3.0))
e ^ 3: 20.085536923187668
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.
The numpy 'array' is another way of storing data. It is similar to a list, but designed for working with numerical data.
# 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])
array: [ 2. 5. 7. 10.] 2nd element: 5.0
A key difference between lists and arrays is how they deal with mathematical operations:
print('my_list * 2:', my_list * 2)
print('my_array * 2:', my_array * 2)
my_list * 2: [2.0, 5.0, 7.0, 10.0, 2.0, 5.0, 7.0, 10.0] my_array * 2: [ 4. 10. 14. 20.]
Mathematical operations on arrays are applied to each element in the array:
print('my_array squared:', my_array**2)
print('my_array square rooted:', np.sqrt(my_array))
my_array squared: [ 4. 25. 49. 100.] my_array square rooted: [1.41421356 2.23606798 2.64575131 3.16227766]
Operations between two arrays of the same length:
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)
my_array: [ 2. 5. 7. 10.] other_array: [1. 7. 1. 2.] my_array + other_array: [ 3. 12. 8. 12.] my_array * other_array: [ 2. 35. 7. 20.]
Here are a few different ways of making arrays, besides specifically defining them:
# array equivalent of 'range':
print(np.arange(0.0,10.0,1.5))
[0. 1.5 3. 4.5 6. 7.5 9. ]
# array of zeros
print(np.zeros(5))
[0. 0. 0. 0. 0.]
# array of ones
print(np.ones(5))
[1. 1. 1. 1. 1.]
# array of random numbers
print(np.random.rand(5))
[0.50999145 0.44111399 0.99652254 0.16413813 0.64181847]
Numpy comes with a number of mathematical functions that can be applied element-wise over an array. Some examples:
x = np.arange(1,6)
print(x)
[1 2 3 4 5]
# trigonometric functions
print(np.sin(x))
[ 0.84147098 0.90929743 0.14112001 -0.7568025 -0.95892427]
# logarithm
print(np.log(x))
[0. 0.69314718 1.09861229 1.38629436 1.60943791]
# rounding to 3 decimal places
print(np.round(np.log(x), 3))
[0. 0.693 1.099 1.386 1.609]
There are also some useful functions that perform a calculation using the whole array:
x = np.arange(1,6)
# mean
print(np.mean(x))
# standard deviation
print(np.std(x))
3.0 1.4142135623730951
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.