Introduction to NumPy#
Author: Mike Wood
Learning objectives: By the end of this notebook, you should be able to:
Access standard mathemtical functions in the NumPy library
Generate arrays and use methods to describe array shapes and sizes
Run computations using arrays in Numpy
Import the NumPy library
# import the numpy library using the np alias
import numpy as np
Mathematical Functions in NumPy#
The numpy library provides all of the standard mathematical functions you might come across in your scientific calculations.
For example, numpy has some simple algebraic functions:
# define a variable x
x = 9
# compute x^2
x_squared = np.power(x,2)
print('x^2 =', x_squared)
# compute the square root of x
square_root_x = np.sqrt(x)
print('sqrt(x) =', square_root_x)
# compute 10 to the xth power
ten_raised_to_the_x = 10**x
print('10^x =',ten_raised_to_the_x)
x^2 = 81
sqrt(x) = 3.0
10^x = 1000000000
Numpy also has the standard trigonometric functions:
# define a variable theta
theta = np.pi/6
# compute the value of sin(theta)
sin_theta = np.sin(theta)
print('sin(theta) = ', sin_theta)
# compute the value of cos(theta)
cos_theta = np.cos(theta)
print('cos(theta) = ', cos_theta)
# compute the value of tan(theta)
tan_theta = np.tan(theta)
print('tan(theta) = ', tan_theta)
sin(theta) = 0.49999999999999994
cos(theta) = 0.8660254037844387
tan(theta) = 0.5773502691896256
Finally, numpy has functions for exponentials and logarithms:
# define a number with the value e
z = np.e
# print the value to check
print(z)
# compute the exponential of 1 and the exponential of 2
# in otherwords, e^1 and e^2
print('e^1 =',np.exp(1))
print('e^2 =',np.exp(2))
# compute the logarithm of e
# - what is the base of the logarithm?
# - what is the other name for this logarithm?
print('log(z) =',np.log(z))
# compute the logarithm with the base 10
print('log10(z) =',np.log10(z))
print('log10(10^3) =',np.log10(1e3))
2.718281828459045
e^1 = 2.718281828459045
e^2 = 7.38905609893065
log(z) = 1.0
log10(z) = 0.4342944819032518
log10(10^3) = 3.0
🤔 Mini-Exercise#
Goal: Estimate the value of sin using a polynomial. Then, compute the error from the true value of sin.
The sine of a number can be estimated with the first four terms of its “Taylor expansion” as: $\( sin(x) \approx x - \frac{x^3}{6} + \frac{x^5}{120} - \frac{x^7}{5040} \)$
Compute sin for a given value of \(x\) using this approximatation. Then, calculate the error from the true value of sin
# declare a variable x and give it a value between -2 and 2
x = np.pi/4
# compute and print the sin approximation
sin_approx = x - x**3/6 + x**5/120 - x**7/5040
print(sin_approx)
# compute the true value of sin
sin_true = np.sin(x)
print(sin_true)
# compute the error between your approximated value and the true value
print('error:',sin_approx-sin_true)
0.7071064695751781
0.7071067811865475
error: -3.116113693746314e-07
Arrays in NumPy#
Calculations on individiual numbers is at the heart of the numpy package. However, NumPy is much more powerful than that. We can do rapid calculations on large collections of values in when we operate with arrays
Defining arrays#
Arrays can be defined in a variety of ways as follows:
# define an array using a range
x = np.arange(1,11)
print('x', x)
# define an array by wrapping a list with the numpy array method
y = np.array([10,11,12,13,14])
print('y',y)
# define an array as a list of lists of equal length
A = np.array([[1, 2], [3, 4]])
print('A',A)
x [ 1 2 3 4 5 6 7 8 9 10]
y [10 11 12 13 14]
A [[1 2]
[3 4]]
Once arrays are created, its helpful to know their shapes and sizes::
# print the shape of x
print('shape of x:', np.shape(x))
# print the size of x
print('size of x:', np.size(x))
# print the shape of y
print('shape of y:', np.shape(y))
# print the size of y
print('size of y:', np.size(y))
# print the shape of A
print('shape of A:', np.shape(A))
# print the size of A
print('size of A:', np.size(A))
shape of x: (10,)
size of x: 10
shape of y: (5,)
size of y: 5
shape of A: (2, 2)
size of A: 4
Calculations with One Array#
When working with arrays, calculations can be carried out elementwise using the same functions as above. Try the following functions using the array x defined in the previous cell:
# compute the square of all elements in x
x_squared = x**2
print('x_squared', x_squared)
# compute the sin of all elements in y
sin_y = np.sin(y)
print('sin(y)', sin_y)
# compute the exponential of all elements in A
A_x = np.exp(A)
print('A^(x)', A_x)
# compute the mean of all elements in x
print('mean of x:',np.mean(x))
# compute the sum of all elements in y
print('sum of y:',np.sum(y))
x_squared [ 1 4 9 16 25 36 49 64 81 100]
sin(y) [-0.54402111 -0.99999021 -0.53657292 0.42016704 0.99060736]
A^(x) [[ 2.71828183 7.3890561 ]
[20.08553692 54.59815003]]
mean of x: 5.5
sum of y: 60
Calculations with multiple arrays#
When working with arrays, calculations can be carried out elementwise
# define a new array z that is the same length as the array y above
z = np.array([1,5,4,7,9])
# compute the sum of arrays y and z
print(y+z)
# compute the product of arrays y and z
print(y*z)
[11 16 16 20 23]
[ 10 55 48 91 126]
🤔 Mini-Exercise#
Goal: Compute the root mean square error between a list of estimated and true values.
The root mean square error (RMSE) is calculated as
where \(N\) is the number of points, \(e_i\) is the \(i\)th estimated value, and \(t_i\) is the \(i\)th true value.
Repeat your calculation for the approximation to sin(x) now for 100 values between -1 and 1. Compute the RMSE between your approximation and the true value, calculated with np.sin(x)
# define an array x with values between -1 and 1
x = np.linspace(-1,1,100)
# compute the estimate value of sin for each of these values
# using functions on the array
sin_approx = x - np.power(x,3)/6 + np.power(x,5)/120 - np.power(x,7)/5040
# compute the true value of sin for each value in x
sin_true = np.sin(x)
# compute the root mean square error for the true vs estimate value
RMSE = np.sqrt(np.mean((sin_true-sin_approx)**2))
print(RMSE)
6.843412938785241e-07