(c) 2016 Justin Bois. This work is licensed under a Creative Commons Attribution License CC-BY 4.0. All code contained herein is licensed under an MIT license.
This tutorial was generated from a Jupyter notebook. You can download the notebook here.
# NumPy, the engine of scientific computing
import numpy as np
Numpy arrays can take a while to get the hang of. Therefore, it's important to practice practice practice!
The functions np.arange()
and np.linspace()
are really useful.
np.arange?
, or by reading the respective webpages (np.arange
, np.linspace
)).np.arange()
to make an array of numbers 0 through 10.np.linspace()
.float
.# Integers (as floats) 0 through 10
np.arange(11, dtype=float)
# Integers 0 through 10 with np.linspace()
np.linspace(0, 10, 11)
In the last lesson, we looked at a data set from Harvey and Orbidans on the cross-sectional area of C. elegans eggs. Recall, we loaded the data like this:
# Load in data
xa_high = np.loadtxt('data/xa_high_food.csv', comments='#')
xa_low = np.loadtxt('data/xa_low_food.csv', comments='#')
Now we would like to compute the diameter of the egg from the cross-sectional area. Write a function that takes in an array of cross-sectional areas and returns an array of diameters. Recall that the diameter $d$ and cross-sectional area $A$ are related by $A = \pi d^2/4$. There should be no for
loops in your function!
Below, is a skeleton for your function for you to fill in.
def xa_to_diameter(xa):
"""
Convert an array of cross-sectional areas
to diameters with commensurate units.
"""
# Compute diameter from area
diameter = ____
return diameter
Use your function to compute the diameters of the eggs.
def xa_to_diameter(xa):
"""
Convert an array of cross-sectional areas
to diameters with commensurate units.
"""
# Compute diameter from area
diameter = 2 * np.sqrt(xa / np.pi)
return diameter
print('Diameters of eggs from well fed mothers:\n', xa_to_diameter(xa_high))
print('\nDiameters of eggs from poorly fed mothers:\n', xa_to_diameter(xa_low))
NumPy enables you do to matrix calculations on two-dimensional arrays. In exercise, you will practice doing matrix calculations on arrays. We'll start by making a matrix and a vector to practice with. You can copy the code below into your script or IPython shell.
A = np.array([[6.7, 1.3, 0.6, 0.7],
[0.1, 5.5, 0.4, 2.4],
[1.1, 0.8, 4.5, 1.7],
[0.0, 1.5, 3.4, 7.5]])
b = np.array([1.1, 2.3, 3.3, 3.9])
First, let's practice slicing.
A
.A
.A
that is greater than 2.A
. using the np.diag()
function.# 1.
print(A[1,:])
# 2.
print(A[:,[1,3]])
# 3.
print(A[A>2])
# 4.
print(np.diag(A))
The np.linalg
module has some powerful linear algebra tools.
np.linalg.solve()
. Store your answer in the Numpy array x
.np.dot(A, x)
to verify that $\mathsf{A}\cdot \mathbf{x} = \mathbf{b}$.np.transpose()
to compute the transpose of A
.np.linalg.inv()
to compute the inverse of A
.# 1.
x = np.linalg.solve(A, b)
# 2.
print('A . x:\n', np.dot(A, x))
print('\nb:\n', b)
# 3.
print('\ntranspose of A:\n', np.transpose(A))
# 4.
print('\ninverse of A:\n', np.linalg.inv(A))
Sometimes you want to convert a two-dimensional array to a one-dimensional array. This can be done with np.ravel()
.
B = np.ravel(A)
.np.reshape()
. Then, reshape B
to make it look like A
again.# 1.
B = np.ravel(A)
print(B)
# 2.
np.reshape(B, A.shape)