An introduction to NumPy#
Learning objectives#
Create
numpyarrays of one and two dimensions.Perform mathematical operations on arrays.
Extract a single value from an array at a particular location.
Prerequisites#
Variables
Lists
Introduction#
numpy is a Python library that enables us to create and use arrays of variables. numpy is very useful for working with data, as we will explore in the lesson.
The basic object of numpy is an array, this is a collection of variables that looks much like a list but can be multi-dimensional. numpy typically uses numeric variables, with arrays of either integers of floats.
Import the NumPy library#
For numpy, the standard-practice alias is np.:
import numpy as np
Creating a NumPy array#
Imagine running an experiment in the lab in which the concentration of one product over time is measured. Mole fractions of our product of 0.0, 0.05, 0.08, 0.1, 0.11 are measured as a time series.
In order to create a numpy array of our data, it’s easiest to first create a list of the data. (Is this the best way to introduce this?)
product_fraction_list = [0.0, 0.05, 0.08, 0.1, 0.11]
The numpy array() function is used to create an array from a list.
product_fraction_array = np.array(product_fraction_list)
Printing product_fraction_array allows inspection of the array.
print(product_fraction_array)
[0. 0.05 0.08 0.1 0.11]
product_fraction_array is a one dimensional array containing the experimental values of mole fraction.
numpy also has function for creating arrays filled with zeros or ones. These functions are fandily named zeros and ones! For one-dimensional arrays, you have to pass the length of array that you want to create to the function.
array_of_zeros = np.zeros(5)
print(array_of_zeros)
[0. 0. 0. 0. 0.]
Excercise#
Create an array containing the following mole fractions of a reactant in a reaction: 0.5, 0.4, 0.37, 0.35, 0.34.
Answer:
reactant_fraction_list = [0.5, 0.4, 0.37, 0.35, 0.34]
reactant_fraction_array = np.array(reactant_fraction_list)
Mathematical Operations#
numpy arrays allow mathematical operations. The most basic mathematical operations involve a single array and an integer. For instance, we might want to turn our mole fraction into a percentage by multiplying every value by 100. This is simple through numpy as mathematical operations are applied to each element.
product_percentage_array = product_fraction_array * 100
print(product_percentage_array)
[ 0. 5. 8. 10. 11.]
All mathematical operators work in the same element by element way, including +, -, / and **.
Mathematical operators also work between two arrays. For example, it would be possible to calculate the mole fraction of our known reactant and product at every time point by adding the two arrays together.
reactant_fraction_list = [0.5, 0.4, 0.37, 0.35, 0.34]
reactant_fraction_array = np.array(reactant_fraction_list)
summed_fraction_array = product_fraction_array + reactant_fraction_array
print(summed_fraction_array)
[0.5 0.45 0.45 0.45 0.45]
Mathematical operations between two arrays works in an element by element way too. The first element in one array is added to the first element in the other array, and so on.
If an attempt is made to add two arrays of different size together, this gives an error!
#array_four = np.array([1,2,3,4])
#array_five = np.array([1,2,3,4,5])
#array_four + array_five
Excercises#
Using the mole fraction of products and reactants given above, create an array containing the mole fraction of other reactants and products in the reaction.
Answer:
remaining_fraction_array = np.ones(5) - summed_fraction_array
The reaction time was also collected during the experiment. This data in minutes is: [0, 2, 4, 6, 8]. Create an array for this time data and convert the values to seconds.
Answer:
minutes = np.array((0, 2, 4, 6, 8))
seconds = minutes*60
Indexing elements#
Individual elements in numpy arrays can be accessed very simply using indexing. Indexing of one-dimensional arrays works very similarly to lists, i.e. the number of the element is given in square brackets.
For example, to take the first value of product mole fraction from product_fraction_array:
first_element = product_fraction_array[0]
print(first_element)
0.0
As ever in Python, indexing starts at 0, i.e. the first element is at position 0 and so on.
Excercise#
Calculate the difference in time (in seconds) between the second and fifth time points in the time series used in the previous excercises.
Answer:
time_difference = seconds[4] - seconds[1]
Multi-dimensional Arrays#
numpy arrays are not just restricted to one dimension. There are no restrictions on the number of dimensions for numpy arrays.
For example, it’s possible to combine both the concentration and time data in a 2D numpy array. One possible way to do this is to combine two lists into a list of lists.
combined_list = (product_fraction_list, (0, 120, 240, 360, 480))
Using the array function on a list of lists creates a multi-dimensional array.
combined_array = np.array(combined_list)
Creating a 2D array also works from a list of 1D arrays. Note, the lists (or arrays) must be the same length for this to work.
Excercise#
Create a two-dimensional array containing the reactant_fraction_array and seconds.
Answer:
reaction_array = np.array((reactant_fraction_array,seconds))
Learning Objectives#
Create numpy arrays of one and two dimensions.
Perform mathematical operations on arrays.
Understand how to index
numpyarrays.
TODO#
Check content vs other NumPy lesson.
Add more complex excercises.