Week 7

Quiz

  1. What function is used to determine the dimensions of a NumPy array?

  2. Write a function that adds up all the entries in a given array.

  3. Recreate the below checkerboard array using NumPy.

In [2]:
import numpy as np
checkerboard(5)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [2], in <cell line: 2>()
      1 import numpy as np
----> 2 checkerboard(5)

NameError: name 'checkerboard' is not defined

Answers:

  1. arr.shape, np.shape(arr) 2.
In [3]:
def entry_sum(arr):
    summation = 0
    r,c = arr.shape
    for i,j in [(i,j) for i in range(r) for j in range(c)]:
        summation += arr[i,j]
    return summation
In [ ]:
 
In [4]:
def entry_sum1(arr):
    summation = 0
    for row in arr:
        for entry in row:
            summation += entry
    return summation
In [5]:
arr = np.ones((5,5))
entry_sum(arr)
Out[5]:
25.0
In [6]:
checkerboard = np.zeros(25,dtype=int)
checkerboard[1::2] = 1
checkerboard = np.reshape(checkerboard,(5,5))
print(checkerboard)
[[0 1 0 1 0]
 [1 0 1 0 1]
 [0 1 0 1 0]
 [1 0 1 0 1]
 [0 1 0 1 0]]
In [7]:
checkerboard = np.zeros((5,5),dtype=int)
checkerboard[0::2,1::2] = 1
checkerboard[1::2,0::2] = 1
print(checkerboard)
[[0 1 0 1 0]
 [1 0 1 0 1]
 [0 1 0 1 0]
 [1 0 1 0 1]
 [0 1 0 1 0]]

Notes

Broadcasting arrays

In [8]:
arr = np.ones((5,5),dtype=int)
print(arr)
[[1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]
In [9]:
arr[:2,:2] = 0
print(arr)
[[0 0 1 1 1]
 [0 0 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]
In [10]:
arr[2::2,2::2] = 3
print(arr)
[[0 0 1 1 1]
 [0 0 1 1 1]
 [1 1 3 1 3]
 [1 1 1 1 1]
 [1 1 3 1 3]]
In [11]:
arr[[0,2,3],[1,1,4]] = 5
print(arr)
[[0 5 1 1 1]
 [0 0 1 1 1]
 [1 5 3 1 3]
 [1 1 1 1 5]
 [1 1 3 1 3]]
In [12]:
arr[1:4,1:4] = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(arr)
[[0 5 1 1 1]
 [0 1 2 3 1]
 [1 4 5 6 3]
 [1 7 8 9 5]
 [1 1 3 1 3]]
In [13]:
arr1 = np.zeros((5,5),dtype=int)
arr1[1:4,1:4] = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(arr1)
[[0 0 0 0 0]
 [0 1 2 3 0]
 [0 4 5 6 0]
 [0 7 8 9 0]
 [0 0 0 0 0]]

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

In [24]:
def middle_ones(n,k):
    arr = np.zeros((n,n),dtype=int)
    arr[(n-k)//2:(n+k)//2,(n-k)//2:(n+k)//2] = 1
    return arr
In [25]:
middle_ones(8,3)
Out[25]:
array([[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, 1, 1, 1, 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]])
In [ ]:
 

Exercise: Write a function that returns the sum of the entries surrounding the $ (i,j) $-entry of a given array.

In [28]:
np.sum(arr1)
Out[28]:
45
In [27]:
np.sum(arr1[[1,3,4],[2,2,3]])
Out[27]:
10
In [ ]:
def neighbor_sum(i,j,arr):
    
    return summation

Exercise: Write a function that replaces every entry of a given array with the sum of its neighbors modulo 3.

In [ ]:
 
In [29]:
import matplotlib.pyplot as plt
In [36]:
plt.imshow(np.random.random((10,10)),cmap='gray')
plt.show()
In [37]:
%matplotlib notebook



import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import colors

x = np.zeros((200,200), dtype = int)

fig = plt.figure(figsize = (6,6))
ax = plt.subplot(111)
plt.title("Animation test")
cmap = colors.ListedColormap(['palegreen', 'green', 'orange', 'black'])
im = ax.imshow(x, cmap = cmap, vmin = -1, vmax = 2)


def animate(i):
    global x

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


anim = FuncAnimation(fig=fig, func=animate,  interval=500, blit=True, repeat=False)
plt.show()
In [ ]: