Creating and Grading Assignments in the IPython/Jupyter Notebook

Jess Hamrick
UC Berkeley
Project Jupyter
@jhamrick

Computational Models of Cognition

  • Berkeley Cognitive Science program
  • Related to artificial intelligence
  • 200 students
  • ~1 semester of programming
  • Coding assignments + written interpretations of results

What is the IPythonJupyter notebook?

What is the IPythonJupyter notebook?

Live demo: try.jupyter.org

Notebook viewer: nbviewer.org

Example notebook: Perceptron Demo

In [2]:
@interact
def plot_histogram(num_samples=(0, 10000), bins=(10, 100), normed=True):
    rv = scipy.stats.norm(0, 1)
    samples = rv.rvs(num_samples)
    x = np.linspace(-3, 3)
    y = rv.pdf(x)
    plt.hist(samples, normed=normed, color='k', bins=bins)
    plt.plot(x, y, 'r-', lw=3)

Teaching computational science

  • Fast code iteration
  • Inline plots
  • Interactive visualization
  • Analyses documented inline

Notebooks as assignments

  • and many more!

Issues

  • Instructor and student versions

Instructor

Write code to compute the mean error between two arrays:

In [3]:
def error(x, y):
    return np.sum(np.abs(x - y))

Student

Write code to compute the mean squared error between two arrays:

In [4]:
def error(x, y):
    # YOUR CODE HERE
    raise NotImplementedError

Issues

  • Instructor and student versions
  • What should/should not be autograded

What should be graded?

In [5]:
def error(x, y):
    return np.sum(np.abs(x - y))
In [6]:
print(error(1, 2))
print(error(np.array([1, 2]), np.array([3, 4]))
  File "<ipython-input-6-2b19b0758384>", line 2
    print(error(np.array([1, 2]), np.array([3, 4]))
                                                   ^
SyntaxError: unexpected EOF while parsing

Issues

  • Instructor and student versions
  • What should and should not be autograded?
  • Grading code and written answers together

Grading written answers

Run your error code on the ouput of the two models:

In [7]:
print("Model 1: {}".format(error(model1(x), y)))
print("Model 2: {}".format(error(model2(x), y)))
Model 1: 5.692703597805511
Model 2: 0.02898058018268955

Explain why the models have different error rates.

YOUR ANSWER HERE

Issues

  • Instructor and student versions
  • What should and should not be autograded?
  • Grading code and written answers together
  • Students changing support code or tests

Meet nbgrader!