Quantitative Neurobiology

Notes, assignments, and code for NEUROBIO 735 (Spring 2024).

Class details:

1/11 – 4/17:
Tuesday, Thursday
3:00 – 4:30
301 Bryan Research Building

Syllabus

Exercises

Solutions

Book

Homework: Functions and NumPy

Functions

  1. Write a function that takes two inputs, an iterable (not necessarily a list!) of items and a string. The function should output a string that splices together the pieces using the separator. For instance,
    splicer(['foo', 'bar', 'baz'], '.')
    

    should return

    'foo.bar.baz'
    
  2. Make the separator default to the underscore, '_'. Would this work if the separator were the first input? Why or why not?

  3. What if not all the elements of the first argument are strings? What if some can be converted to strings? Modify the function to handle this case. In cases you can’t handle, be sure to print an error message! (Hint: try...except.) Note that all the data types we’ve seen so far can be converted to strings. If you want to test this behavior, you might try:
class Failer:
    def __str__(self):
        pass

test_obj = Failer()

to get an object that cannot be converted to a string (h/t this StackOverflow answer).

NumPy

  1. Create a diagonal matrix whose nonzero entries are the numbers 1 through 10.

  2. Create an array of random numbers, each entry drawn from a Gaussian (normal) distribution of mean 0 and standard deviation 1. The array should be 3 by 4 by 7.

  3. Get the first and third “columns” (second dimension) of this array. What is the shape of the resulting array?

  4. Let’s say that we need to manipulate the dimensions of this array. One way to do this is to flatten the matrix. In what order are the entries read out? What if we wanted to swap dimensions so that the last index became the first index? (Hint: what is it called when we swap the rows and columns of a matrix? Does NumPy have a function for this? What does it do to higher-order arrays?)

  5. Write a function that finds the value in a NumPy array closest to a given number. The first input should be the array. There are many ways to do this.