Week 6

Notes

Arrays using NumPy

In [1]:
import numpy as np
In [2]:
arr1 = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(arr1)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
In [3]:
arr1[1,2]
Out[3]:
7
In [4]:
arr1[1][2]
Out[4]:
7
In [5]:
arr1[-1,1]
Out[5]:
10
In [6]:
arr1[1:3,1:3]
Out[6]:
array([[ 6,  7],
       [10, 11]])
In [7]:
np.shape(arr1)
Out[7]:
(3, 4)
In [8]:
np.sum(arr1)
Out[8]:
78
In [9]:
np.mean(arr1)
Out[9]:
6.5
In [10]:
np.transpose(arr1)
Out[10]:
array([[ 1,  5,  9],
       [ 2,  6, 10],
       [ 3,  7, 11],
       [ 4,  8, 12]])
In [11]:
arr2 = np.zeros((5,4),dtype=int)
print(arr2)
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]
In [12]:
type(arr2[0,0])
Out[12]:
numpy.int32
In [13]:
arr3 = np.ones((3,3),dtype=int)
print(arr3)
[[1 1 1]
 [1 1 1]
 [1 1 1]]
In [14]:
np.concatenate([arr1,arr3],axis=1)
Out[14]:
array([[ 1,  2,  3,  4,  1,  1,  1],
       [ 5,  6,  7,  8,  1,  1,  1],
       [ 9, 10, 11, 12,  1,  1,  1]])
In [15]:
np.concatenate([arr1,arr2])
Out[15]:
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [ 0,  0,  0,  0],
       [ 0,  0,  0,  0],
       [ 0,  0,  0,  0],
       [ 0,  0,  0,  0],
       [ 0,  0,  0,  0]])
In [16]:
arr2[:3,:3] = arr3
print(arr2)
[[1 1 1 0]
 [1 1 1 0]
 [1 1 1 0]
 [0 0 0 0]
 [0 0 0 0]]
In [17]:
arr1[:2,:2] = 18
print(arr1)
[[18 18  3  4]
 [18 18  7  8]
 [ 9 10 11 12]]
In [18]:
arr2[:3,:] = arr1
print(arr2)
[[18 18  3  4]
 [18 18  7  8]
 [ 9 10 11 12]
 [ 0  0  0  0]
 [ 0  0  0  0]]

Exercise: Write a function which will create an $ n \times n $ array of zeros with a $ k \times k $ block of ones in the middle.

In [2]:
"""
n is the size of the array
k is the size of the block of ones
"""
def bullseye(n,k):
    arr = np.zeros((n,n),dtype = int)
    ones = np.ones((k,k),dtype = int)
    m = (n-k)//2
    arr[m:m+k,m:m+k] = ones
    return arr
In [5]:
B = bullseye(10,3)
print(B)
[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 1 1 1 0 0 0 0]
 [0 0 0 1 1 1 0 0 0 0]
 [0 0 0 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

Exercise: Write a function that adds up the entries neighboring a given location in an array.

In [4]:
"""
arr is an m by n array of numbers
entry is the location (i,j)
"""
def neighbor_sum(arr,entry):
    m,n = np.shape(arr)
    padd = np.zeros((m+1,n+1),dtype=int)
    padd[1:,1:] = arr
    i,j = entry
    square = padd[i:i+3,j:j+3]
    summ = np.sum(square) - arr[i,j]
    return summ
In [8]:
neighbor_sum(B,(4,4))
Out[8]:
8

Exercise: Write a function that updates each entry of an array with the sum of its neighbors modulo $ k $.

In [9]:
"""
arr is an m by n array of integers between 0 and k
k is the modulus for the sum
"""
def update(arr,k):
    m,n = np.shape(arr)
    new_arr = np.zeros((m,n),dtype=int)
    for i in range(m):
        for j in range(n):
            summ = neighbor_sum(arr,(i,j))%k
            new_arr[i,j] = summ
    return new_arr
In [11]:
update(B,8)
Out[11]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 2, 3, 2, 1, 0, 0, 0],
       [0, 0, 2, 3, 5, 3, 2, 0, 0, 0],
       [0, 0, 3, 5, 0, 5, 3, 0, 0, 0],
       [0, 0, 2, 3, 5, 3, 2, 0, 0, 0],
       [0, 0, 1, 2, 3, 2, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

Color arrays

In [19]:
import matplotlib.pyplot as plt
In [20]:
plt.imshow(arr1)
plt.show()
In [21]:
print(arr1)
[[18 18  3  4]
 [18 18  7  8]
 [ 9 10 11 12]]
In [23]:
plt.imshow(arr1,cmap='inferno')
plt.show()
In [25]:
plt.imshow(arr1,cmap='gray')
Out[25]:
<matplotlib.image.AxesImage at 0x148043bc7f0>
In [26]:
from matplotlib import colors
In [32]:
cmap = colors.ListedColormap(['palegreen', 'green', 'orange', 'black'])
arr4 = np.random.randint(-1,3,(4,4))
plt.imshow(arr4,cmap=cmap,vmin=-1,vmax=2)
plt.show()

Week 7

Animation

In [12]:
%matplotlib notebook
In [13]:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import colors
In [19]:
x = bullseye(10,3)  # Initial configuration of the array

fig = plt.figure(figsize = (6,6))
ax = plt.subplot(111)
plt.title("Deterministic cellular automata (modulo 3)")
#cmap = colors.ListedColormap(['palegreen', 'green', 'orange', 'black'])
im = ax.imshow(x, cmap = 'inferno', vmin=0, vmax=3)


def animate(i):
    global x

    x=update(x,3)
    im.set_data(x)
    return im


anim = FuncAnimation(fig=fig, func=animate,  interval=300, blit=True, repeat=False)
plt.show()

Game of Life

Evolution of a 2D grid of 0s and 1s. Here are the rules

  • A cell that is not alive (0) will come to life (become 1) if it is surrounded by exactly three living cells (1s).
  • A cell that is living (1) will die off (become 0) if one of the following
    1. It has fewer than two living neighbors.
    2. It has more than 3 living neighbors.

Exercise: Write an update function that will update an array of 0s and 1s according to the rules from Conway's game of life.

Exercise: Write a function to produce a random array of 0s and 1s with each entry having probability $ p $ of being a 1.

Exercise: Animate the game of life using a random starting configuration.

In [ ]: