Task 1: Create a 2D Numpy array, named A, that is a 10x10 array integers, each of which is set to 0. Write a pair of for loops that iterate over A and sets A[i,j] = i + j. Print that array out to verify that it's behaving as expected - it should look like this:
[[ 0 1 2 3 4 5 6 7 8 9]
[ 1 2 3 4 5 6 7 8 9 10]
[ 2 3 4 5 6 7 8 9 10 11]
[ 3 4 5 6 7 8 9 10 11 12]
[ 4 5 6 7 8 9 10 11 12 13]
[ 5 6 7 8 9 10 11 12 13 14]
[ 6 7 8 9 10 11 12 13 14 15]
[ 7 8 9 10 11 12 13 14 15 16]
[ 8 9 10 11 12 13 14 15 16 17]
[ 9 10 11 12 13 14 15 16 17 18]]
In [ ]:
# Put your code here!
import numpy as np
A = np.zeros((10,10), dtype='int')
for i in range(a.shape[0]):
for j in range(a.shape[1]):
A[i,j] = i+j
print(A)
Task 2: Use Numpy's array slicing capabilities to create a new array, B, which is a subset of the values in array A. Specifically, extract every second element in both dimensions in array A, starting with the second element (i.e., index=1) in each dimension. Store this in B, and print out array B. It should look like this:
[[ 2 4 6 8 10]
[ 4 6 8 10 12]
[ 6 8 10 12 14]
[ 8 10 12 14 16]
[10 12 14 16 18]]
In [ ]:
B = A[1::2,1::2]
print(B)
Task 3: Using Numpy's slicing capabilities, extract the second and third rows of array B and store them in a third array, C. Print out C. It should look like this:
[[ 4 6 8 10 12]
[ 6 8 10 12 14]]
In [ ]:
C = B[1:3,:]
print(C)
Task 4: Write a function, called add_neighborhood()
, that:
shape()
method to adjust for the fact that you don't know what its size is) and sets D[i,j] equal to the values of A[i,j] plus its four neighbors, A[i+1,j], A[i-1,j], A[i,j+1], A[i,j-1]. If you are at the edge or corner of the array (say, at A[0,0]) do not include any values that go over the edge of the array (into negative numbers or beyond the last index in any dimension).Test this out using array A and B. When applied to array A, you should get this output:
[[ 2 5 9 13 17 21 25 29 33 27]
[ 5 10 15 20 25 30 35 40 45 39]
[ 9 15 20 25 30 35 40 45 50 43]
[13 20 25 30 35 40 45 50 55 47]
[17 25 30 35 40 45 50 55 60 51]
[21 30 35 40 45 50 55 60 65 55]
[25 35 40 45 50 55 60 65 70 59]
[29 40 45 50 55 60 65 70 75 63]
[33 45 50 55 60 65 70 75 80 67]
[27 39 43 47 51 55 59 63 67 52]]
and when you apply this function to array B, you should get this output:
[[10 18 26 34 30]
[18 30 40 50 46]
[26 40 50 60 54]
[34 50 60 70 62]
[30 46 54 62 50]]
Note: Make sure that the edges and corners have the right values!
In [ ]:
def add_neighborhood(arr):
D = np.zeros_like(arr)
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
D[i,j] = arr[i,j]
if i >= 1:
D[i,j] += arr[i-1,j]
if i < arr.shape[0]-1:
D[i,j] += arr[i+1,j]
if j >= 1:
D[i,j] += arr[i,j-1]
if j < arr.shape[1]-1:
D[i,j] += arr[i,j+1]
return D
new_array = add_neighborhood(A)
print(A)
print(new_array)
new_array = add_neighborhood(B)
print(B)
print(new_array)
Task 5: Using the pyplot matshow()
method, plot array A in a plot that uses a color map of your choice (that is not the default color map!), and where the axes are invisible.
In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
myplot = plt.matshow(A, cmap='hot')
myplot.axes.get_xaxis().set_visible(False)
myplot.axes.get_yaxis().set_visible(False)
In [ ]:
from IPython.display import HTML
HTML(
"""
<iframe
src="https://goo.gl/forms/VwY5ods4ugnwidnG2?embedded=true"
width="80%"
height="1200px"
frameborder="0"
marginheight="0"
marginwidth="0">
Loading...
</iframe>
"""
)