NumpyEx01


Numpy Exercise 1

Imports


In [14]:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns

In [15]:
import antipackage
import github.ellisonbg.misc.vizarray as va

Checkerboard

Write a Python function that creates a square (size,size) 2d Numpy array with the values 0.0 and 1.0:

  • Your function should work for both odd and even size.
  • The 0,0 element should be 1.0.
  • The dtype should be float.

In [18]:
c = np.arange(0, 10, 2, dtype=np.float)
c


data = [1.0,0.0],[0.0,1.0]
a = np.array(data)
va.vizarray(a)


Out[18]:

In [25]:
def checkerboard(x,y):
    a = np.ones((x,y))
    while x<2:


Out[25]:

In [7]:
a = checkerboard(4)
assert a[0,0]==1.0
assert a.sum()==8.0
assert a.dtype==np.dtype(float)
assert np.all(a[0,0:5:2]==1.0)
assert np.all(a[1,0:5:2]==0.0)

b = checkerboard(5)
assert b[0,0]==1.0
assert b.sum()==13.0
assert np.all(b.ravel()[0:26:2]==1.0)
assert np.all(b.ravel()[1:25:2]==0.0)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-4aa63174c227> in <module>()
      1 a = checkerboard(4)
----> 2 assert a[0,0]==1.0
      3 assert a.sum()==8.0
      4 assert a.dtype==np.dtype(float)
      5 assert np.all(a[0,0:5:2]==1.0)

TypeError: 'NoneType' object has no attribute '__getitem__'

Use vizarray to visualize a checkerboard of size=20 with a block size of 10px.


In [ ]:
# YOUR CODE HERE
raise NotImplementedError()

In [ ]:
assert True

Use vizarray to visualize a checkerboard of size=27 with a block size of 5px.


In [ ]:
# YOUR CODE HERE
raise NotImplementedError()

In [ ]:
assert True