import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
For some values of b, the population x always converges to the same value, regardless of the initial population. This is illustrated below.
plt.figure(figsize=(14, 6))
b = 2.9
for x in np.linspace(.1, .9, 9):
pops = [x]
for t in range(50):
x = b*x*(1-x)
pops.append(x)
plt.plot(pops, '.-')
plt.xlabel('t')
plt.ylabel('x')
plt.title('Population vs. time for different initial populations');
This finds the mean value of the elements of an array.
x = np.arange(11)
print(x)
np.mean(x)
This solves a matrix equation of the form Aw = b, and returns the value of w.
? np.linalg.solve
NumPy arrays can be multidimensional:
Two dimensional arrays can be created from a list of lists using np.array.
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
The shape can be accessed as a property of the array
a.shape
The dtype of an aray is the data type of the individual elements
a.dtype
The array creation functions np.zeros, np.ones, np.empty etc can all be passed a shape as an argument. This specifies the shape of the array to create. By default, arrays are created as arrays of floats.
b = np.ones((3, 4))
print(b)
Change the data type of the elements using the dtype keyword argument.
b = np.ones((3, 4), dtype=int) # Create an array of integers
print(b)
b = np.ones((3, 4), dtype=bool) # Create an array of booleans
print(b)
Each data type has one value that equates to False. All others equate to True. The False values are:
np.array([0, 1, 2], dtype=bool)
List indexing and slicing
a = np.arange(20).reshape((4, 5))
print(a)
Access individual elements of an array using an integer index for each axis, separated by commas.
print(a[1,2])
Modify elements using the '=' assignment operator.
a[1,2] = 100
print(a)
a[1,2] = 7
print(a)
Slice out a row in a 2-dimensional array using a single integer.
print(a[0])
Slice out a column using a colon ':' for the first axis (meaning 'select all rows'), then an integer for the column index.
print(a[:,0])
Specify a slice for each axis to slice out the central section.
print(a[1:-1,1:-1])
Array operations are performed element-by-element for multidimensional arrays.
a + 1 # Add 1 to each element
a *= 3 # Multiply each element by 3, and store the result back to the same array
a
Using +=, *=, -=, /= modifies the original array.
a +=1
print(a)
Performing an operation on an array does not modify the array.
a + 10
a
A figure can be divided into a grid of cells using the subplot function:
These are a family of parametrized curves of the form:
subplot can be used to explore the types of figures generated for different values of a and b. We create a grid of cells where the value of a changes with each row, and the value of b changes with each column.
rows, cols = 5, 5
t = np.linspace(0, 2*np.pi, 100)
i = 1
plt.figure(figsize=(10,10))
for a in range(1, rows+1):
for b in range(1, cols+1):
plt.subplot(rows, cols, i)
plt.plot(np.cos(a*t), np.sin(b*t))
i += 1
The Matplotlib functions xticks and yticks can be used to set the locations of the tick points on the x and y axes. Calling these functions with an empty list will turn off the ticks.
rows, cols = 5, 5
t = np.linspace(0, 2*np.pi, 100)
i = 1
plt.figure(figsize=(10,10))
for a in range(1, rows+1):
for b in range(1, cols+1):
plt.subplot(rows, cols, i)
plt.plot(np.cos(a*t), np.sin(b*t))
plt.xticks([])
plt.yticks([])
plt.title('a = {}, b = {}'.format(a, b))
i += 1