I am working on the file "Day6/code/bias_and_variance.ipynb".
The first task is:

**Hint**: Much of the material covered in this problem is introduced in the Geman et al. (1992) reading. If you are having trouble with the conceptual questions, this might be a good place to look.

We wish to evaluate the performance of a learning algorithm that takes a set of $(x,y)$ pairs as input and selects a function $g(x)$, which predicts the value of $y$ for a given $x$ (that is, for a given $(x,y)$ pair, $g(x)$ should approximate $y$).
We will evaluate the 'fit' of the functions that our learning algorithm selects using the mean squared error (MSE) between $g(x)$ and $y$. For a set of $n$ data points ${ (x_1, y_1), \ldots, (x_n,y_n) }$, the MSE associated with a function $g$ is calculated as
$$
MSE = \frac{1}{n} \sum_{i=1}^n \left ( y_i - g(x_i) \right )^2 .
$$
The set of candidate functions that we will allow our algorithm to consider is the set of $k$th-order polynomials. This means that our hypothesis space will contain all functions of the form $g(x) = p_kx^k + p_{k-1} x^{k-1} + \ldots + p_{1} x + p_0$. These functions are entirely characterized by their coefficient vector ${\bf p} = (p_k, p_{k-1}, \ldots, p_0)$, and become much more flexible as $k$ increases (think about the difference between linear ($k=1$), quadratic ($k=2$), and cubic ($k=3$) functions). If we are given a set of $(x,y)$ pairs, it is straightforward to find the $k$th-order polynomial that minimizes the MSE between $g(x)$ and the observed $y$ values.

For those who have done some statistics, the calculation for finding ${\bf p}$ is just a case of linear regression where the various powers of $x$ are the predictors

In this problem, we'll be trying to learn the function $g(x)$ that generated some data with the addition of some Gaussian (i.e., normally distributed) noise. The data is a $110\times 2$ array, where the first column corresponds to the $x$ coordinate, and the second column corresponds to the $y$ coordinate (which is the function evaluated at the...
Can you write Python code for it?

data = np.load("data/xy_data.npy")

# only show the first ten points, since there are a lot
data[:10]


I am working on the file "Day6/code/bias_and_variance.ipynb".
The first task is:

Examine the figure that is produced. Try changing the widget sliders to change the dataset that we are fitting the polynomial to, and the degree of that polynomial. Which degree polynomial both results in similar fits (i.e. similar coefficients) across all datasets _and_ does a good job at capturing the data? How can this be understood in terms of the bias and variance tradeoff we discussed in class?

If you said that polynomials with order 2 or 3 had the best fit, you got 0.5 points. If you noted that it is best to have neither too few nor too many polynomial terms to ensure that you neither under- nor over-fit your data, you received another 0.5 points.

Part B (1 point)
To get a more quantitative sense of how well each polynomial order fits the data, we'll now compute the actual mean squared error (MSE) of the polynomial fits in relationship to both a training dataset and a testing dataset.

Complete the function `mse` to compute the MSE for a polynomial with order $k$ that has been fitted to the training data. The completed `mse` function should return a tuple containing the MSE values for the training data and the test data.

Can you write Python code for it?

def mse(k, train, test):
    """Fits a polynomial with order `k` to a training dataset, and
    then returns the mean squared error (MSE) between the y-values
    of the training data and the fitted polynomial, and the MSE
    between the y-values of the test data and the fitted polynomial.

    Your answer can be done in 6 lines of code, including the return
    statement.

    Parameters
    ----------
    k : integer
        The polynomial order
    train : numpy array with shape (n, 2)
        The training data, where the first column corresponds to the
        x-values, and the second column corresponds to the y-values
    test : numpy array with shape (m, 2)
        The testing data, where the first column corresponds to the
        x-values, and the second column corresponds to the y-values

    Returns
    -------
    a 2-tuple consisting of the training set MSE and testing set MSE

    """
    ### BEGIN SOLUTION
    # compute the polynomial fit
    p = np.polyfit(train[:, 0], train[:, 1], k)

    # compute predictions and MSE for the training data
    train_prediction = np.polyval(p, train[:, 0])
    train_mse = np.mean((train_prediction - train[:, 1]) ** 2)

    # compute predictions and MSE for the testing data
    test_prediction = np.polyval(p, test[:, 0])
    test_mse = np.mean((test_prediction - test[:, 1]) ** 2)

    return train_mse, test_mse
    ### END SOLUTION


I am working on the file "Day6/code/bias_and_variance.ipynb".
The first task is:
For example, we can compute the MSE for $k=2$ by using the first ten datapoints as training data, and the other datapoints as testing data, as follows:
Can you write Python code for it?


I am working on the file "Day6/code/bias_and_variance.ipynb".
The first task is:
Part C (1 point)
Next, complete the function template plot_mse to plot MSE versus $k$ for both traindata and testdata. Be sure to include a proper legend, title, and axis labels.
Can you write Python code for it?

def plot_mse(axis, max_order, train, test):
    """Plot the mean squared error (MSE) for the given training and testing
    data as a function of polynomial order.

    * Your plot should show the MSE for 0 <= k < max_order
    * There should be two lines: one black, for the training set error, and
      one red, for the testing set error.
    * Make sure to include labels for the x- and y- axes.
    * Label the training error and testing error lines as "Training set error"
      and "Testing set error", respectively. These labels will be used to
      create a legend later on (and so you should NOT actually create the
      legend yourself -- just label the lines).

    Your answer can be done in 10 lines of code, including the return statement.

    Parameters
    ----------
    axis : matplotlib axis object
        The axis on which to plot the MSE
    max_order : integer
        The maximum polynomial order to compute a fit for
    train : numpy array with shape (n, 2)
        The training data, where the first column corresponds to the
        x-values, and the second column corresponds to the y-values
    test : numpy array with shape (m, 2)
        The testing data, where the first column corresponds to the
        x-values, and the second column corresponds to the y-values

    Returns
    -------
    numpy array with shape (max_order, 2)
        The MSE for the training data (corresponding to the first column) and
        for the testing data (corresponding to the second column). Each row
        corresponds to a different polynomial order.

    """
    ### BEGIN SOLUTION
    k = np.arange(0, max_order)

    # compute error for all values of k
    error = np.empty((max_order, 2))
    for i in range(max_order):
        error[i] = mse(k[i], train, test)

    axis.plot(k, error[:, 0], 'k-', label="Training set error")
    axis.plot(k, error[:, 1], 'r-', label="Testing set error")
    axis.set_xlabel("Polynomial model order (k)")
    axis.set_ylabel("Mean squared...


I am working on the file "Day6/code/bias_and_variance.ipynb".
The first task is:
You can calculate the values of $g$ at a set of $x$ coordinates using the command np.polyval. For example, if you wanted to compute $g(x)$ at $x=0$, $x=0.5$, and $x=1$, you could do:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:

Notice that myarray looks different than mylist -- it actually tells you that it's an array. If we take a look at the types of mylist and myarray, we will also see that one is a list and one is an array. Using type can be a very useful way to verify that your variables contain what you want them to contain:

Can you write Python code for it?

# look at what type mylist is
type(mylist)

# look at what type myarray is
type(myarray)


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:

There are many other vectorized computations that you can do on NumPy arrays, including multiplication (np.prod), mean (np.mean), and variance (np.var). They all act essentially the same way as np.sum -- give the function an array, and it computes the relevant function across all the elements in the array.

Exercise: Euclidean distance (2 points)
Recall that the Euclidean distance $d$ is given by the following equation:
$$
d(a, b) = \sqrt{\sum_{i=1}^N (a_i - b_i) ^ 2}
$$
In NumPy, this is a fairly simple computation because we can rely on array computations and the np.sum function to do all the heavy lifting for us.

Complete the function euclidean_distance below to compute $d(a,b)$, as given by the equation above. Note that you can compute the square root using np.sqrt.

Can you write Python code for it?

def euclidean_distance(a, b):
    """Computes the Euclidean distance between a and b.

    Hint: your solution can be done in a single line of code!

    Parameters
    ----------
    a, b : numpy arrays or scalars with the same size

    Returns
    -------
    the Euclidean distance between a and b

    """
    ### BEGIN SOLUTION
    return np.sqrt(np.sum((a - b) ** 2))
    ### END SOLUTION


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
<div class="alert alert-warning">
Note: make sure when adding arrays that you are actually working with arrays, because if you try to add two lists, you will *not* get an error. Instead, the lists will be concatenated:
</div>
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:

Note: be careful about setting modifying an array if what you really want is a copy of an array. Remember that in Python, variables are really just pointers to objects.

For example, if I want to create a second array that mutliples every other value in arr by two, the following code will work but will have unexpected consequences:
Can you write Python code for it?

arr = np.arange(10)
arr2 = arr
arr2[::2] = arr2[::2] * 2
print("arr:  " + str(arr))
print("arr2: " + str(arr2))


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:

Remember that you need to execute the cell above (with your definition of euclidean_distance), and then run the cell below to check your answer. If you make changes to the cell with your answer, you will need to first re-run that cell, and then re-run the test cell to check your answer again.

Can you write Python code for it?

# add your own test cases in this cell!

from nose.tools import assert_equal, assert_raises

# check euclidean distance of size 3 integer arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
assert_equal(euclidean_distance(a, b), 5.196152422706632)

# check euclidean distance of size 4 float arrays
x = np.array([3.6, 7., 203., 3.])
y = np.array([6., 20.2, 1., 2.])
assert_equal(euclidean_distance(x, y), 202.44752406487959)

# check euclidean distance of scalars
assert_equal(euclidean_distance(1, 0.5), 0.5)

# check that an error is thrown if the arrays are different sizes
a = np.array([1, 2, 3])
b = np.array([4, 5])
assert_raises(ValueError, euclidean_distance, a, b)
assert_raises(ValueError, euclidean_distance, b, a)

print("Success!")


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
Accessing and modifying multidimensional array elements
To access or set individual elements of the array, we can index with a sequence of numbers:
Can you write Python code for it?

# set the 3rd element in the 1st row to 0
arr[0, 2] = 0
arr


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
And to reverse the array:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
Creating multidimensional arrays
Previously, we saw that functions like np.zeros or np.ones could be used to create a 1-D array. We can also use them to create N-D arrays. Rather than passing an integer as the first argument, we pass a list or tuple with the shape of the array that we want. For example, to create a $3\times 4$ array of zeros:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
However, you cannot do this with NumPy arrays. If you tried to run the following code, for example:
python
myarray = np.array([])
myarray.append(7)
You'd get an error like this:
```
AttributeError                            Traceback (most recent call last)
<ipython-input-25-0017a7f2667c> in <module>()
      1 myarray = np.array([])
----> 2 myarray.append(7)
AttributeError: 'numpy.ndarray' object has no attribute 'append'
```

To create a NumPy array, you must create an array with the correct shape from the beginning. However, the array doesn't have to have all the correct values from the very beginning: these you can fill in later.

There are a few ways to create a new array with a particular size:

np.empty(size) -- creates an empty array of size size
np.zeros(size) -- creates an array of size size and sets all the elements to zero
np.ones(size) -- creates an array of size size and sets all the elements to one

So the way that we would create an array like the list above is:
Can you write Python code for it?

myarray = np.empty(2) # create an array of size 2
myarray[0] = 7
myarray[1] = 2
myarray


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
Just as when we are working with a single array, we can add, subtract, divide, multiply, etc. several arrays together:
Can you write Python code for it?

print("Subtraction: \t" + str(array_a - array_b))
print("Multiplication:\t" + str(array_a * array_b))
print("Exponent: \t" + str(array_a ** array_b))
print("Division: \t" + str(array_a / array_b))


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
Note that for 1-D arrays, the shape returned by the shape attribute is still a tuple, even though it only has a length of one:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
Now, we have access to all NumPy functions via the variable np (this is the convention in the Scientific Python community for referring to NumPy). We can take a look at what this variable actually is, and see that it is in fact the numpy module (remember that you will need to have run the cell above before np will be defined!):
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
Or, you could use a list comprehension:
Can you write Python code for it?

mylist = [3, 6, 1, 0, 10, 22]
mylist_plus1 = [x + 1 for x in mylist]
mylist_plus1


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
The colon in the first position essentially means "select from every row". So, we can interpret arr[:, 1] as meaning "take the second element of every row", or simply "take the second column".
Using this syntax, we can select whole regions of an array. For example:
Can you write Python code for it?

# select a rectangular region from the array
arr[2:5, 1:3]


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
To get all elements except the first and the last:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
To get all elements except the last:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
To get every element of the array (beginning from the second element):
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
To get every other element of the array (beginning from the first element):
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
To look at columns, we use the following syntax:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
Using NumPy arrays, we can just use the np.sum function:
Can you write Python code for it?

# you can also just do np.sum(mylist) -- it converts it to an
# array for you!
myarray = np.array(mylist)
np.sum(myarray)


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
We can also access the element on it's own, without having the equals sign and the stuff to the right of it:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
We can also create arrays and then reshape them into any shape, provided the new array has the same size as the old array:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_NumPy.ipynb".
The first task is:
Working with multiple arrays
We can also easily do these operations for multiple arrays. For example, let's say we want to add the corresponding elements of two lists together. Here's how we'd do it with regular lists:
Can you write Python code for it?

list_a = [1, 2, 3, 4, 5]
list_b = [6, 7, 8, 9, 10]
list_c = [list_a[i] + list_b[i] for i in range(len(list_a))]
list_c


I am working on the file "Day6/code/intro_to_notebook.ipynb".
The first task is:

When completing a problem set, it is often useful to supplement the autograder tests with test cases of your own. Below, we provide you with a "scratch" cell to be used for your own testing purposes.

Keep in mind, however, that writing your own test cases is **completely optional** -- these are only here to help you as you complete the assignment. This means that you will *not* be graded on anything you include in this cell!

Can you write Python code for it?

# add your own test cases in this cell!

"""(1 point) Test code for the 'hello' function. This cell should NOT give any errors when it is run."""
from nose.tools import assert_equal
assert_equal(hello("Jessica"), "Hello, Jessica!")
assert_equal(hello("jessica"), "Hello, jessica!")
assert_equal(hello("Tom"), "Hello, Tom!")
assert_equal(hello("123"), "Hello, 123!")

print("Success!")


I am working on the file "Day6/code/intro_to_notebook.ipynb".
The first task is:
However, this will not work unless the thing to be displayed is on the last line. For example, if we wanted to print the total sum and then a message after that, this will not do what we want (it will only print "Done computing total.", and not the total sum itself).
Can you write Python code for it?

total = 0
for i in range(1, 11):
    total += i
total
print("Done computing total.")


I am working on the file "Day6/code/intro_to_notebook.ipynb".
The first task is:
If you ran the summing examples from the previous section, you'll notice that total and i are listed under the %whos command. That is because when we ran the code for those examples, they also modified the kernel state.
(Note that commands beginning with a percent (%) or double percent (%%) are special IPython commands called magics. They will only work in IPython.)
Restarting the kernel
It is generally a good idea to periodically restart the kernel and start fresh, because you may be using some variables that you declared at some point, but at a later point deleted that declaration.

Your code should always be able to work if you run every cell in the notebook, in order, starting from a new kernel.

To test that your code can do this, first restart the kernel by clicking the restart button:

Then, run all cells in the notebook in order by choosing Cell$\rightarrow$Run All from the menu above.

There are many keyboard shortcuts for the notebook. To see a full list of these, go to Help$\rightarrow$Keyboard Shortcuts.

To learn a little more about what things are what in the IPython Notebook, check out the user interface tour, which you can access by going to Help$\rightarrow$User Interface Tour.

Tests
For many of the coding assignments we give you, we will provide tests that your code must pass. These will be in one or more cells after the code you need to write, and they must pass for you to get credit. In the following exercise, there is one test cell after the cell where you should put your answer.
Also, be aware that test cells will be included only for coding questions -- you're on your own for questions requiring a written response!

Implement the function hello and make sure the test cells runs without any errors. You will need to delete the line with `raise NotImplementedError`, write your own solution, and then re-run the...
Can you write Python code for it?

def hello(name):
    """Returns a message containing "Hello, <name>!",
    where <name> is passed in as an argument.

    Parameters
    ----------
    name : string
        The name of the person to say hello to

    Returns
    -------
    the message containing "Hello, <name>!"

    """
    ### BEGIN SOLUTION
    return "Hello, " + name + "!"
    ### END SOLUTION

# try running your hello function with your own name and see what
# it returns
hello("YOUR NAME HERE")

# Hint: if the test cell is not passing, but your function seems
# to be showing the right thing, make sure you are actually
# returning a value from your function! You should be using
# `return`, not `print`. For example, this cell should display:
#
#     Your function returned: Hello, Reverend Bayes!
#
# and not:
#
#     Hello, Reverend Bayes!
#     Your function returned: None

message = hello("Reverend Bayes")
print("Your function returned: " + str(message))


I am working on the file "Day6/code/intro_to_notebook.ipynb".
The first task is:
Running the above cell should work, because a has now been declared. To see what variables have been declared, you can use the %whos command:
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_notebook.ipynb".
The first task is:
The issue is that the variable a does not exist. Modify the cell above so that a is declared first (for example, you could set the value of a to 1 -- or pick whatever value you want). Once you have modified the above cell, you should be able to run the following cell (if you haven't modified the above cell, you'll get the same error!):
Can you write Python code for it?


I am working on the file "Day6/code/intro_to_notebook.ipynb".
The first task is:
You can tell what the type of a cell is by selecting the cell, and looking at the toolbar at the top of the page. For example, try clicking on this cell. You should see the cell type menu displaying "Markdown", like this:

Command mode and edit mode
In the notebook, there are two modes: edit mode and command mode. By default the notebook begins in command mode. In order to edit a cell, you need to be in edit mode.

When you are in command mode, you can press enter to switch to edit mode. The outline of the cell you currently have selected will turn green, and a cursor will appear.

When you are in edit mode, you can press escape to switch to command mode. The outline of the cell you currently have selected will turn gray, and the cursor will disappear.

Markdown cells
For example, a markdown cell might look like this in command mode (Note: the following few cells are not actually cells -- they are images and just look like cells! This is for demonstration purposes only.)

Then, when you press enter, it will change to edit mode:

Now, when we press escape, it will change back to command mode:

However, you'll notice that the cell no longer looks like it did originally. This is because IPython will only render the markdown when you tell it to. To do this, we need to "run" the cell by pressing Ctrl-Enter, and then it will go back to looking like it did originally:

Code cells
For code cells, it is pretty much the same thing. This is what a code cell looks like in command mode (again, the next few cells LOOK like cells, but are just images):

If we press enter, it will change to edit mode:

And pressing escape will also go back to command mode:

If we were to press Ctrl-Enter like we did for the markdown cell, this would actually run the code in the code cell:

Executing cells
Code cells can contain any valid Python code in them. When you run the cell, the code is executed and any output is displayed.
<div...
Can you write Python code for it?

print("Printing cumulative sum from 1-10:")
total = 0
for i in range(1, 11):
    total += i
    print("Sum of 1 to " + str(i) + " is: " + str(total))
print("Done printing numbers.")


I am working on the file "Day6/code/k-means_clustering.ipynb".
The first task is:
Also, take a look at the imported functions k_means:
Can you write Python code for it?


I am working on the file "Day6/code/k-means_clustering.ipynb".
The first task is:
Finally, we can visualize this result against the true assignments using the helper function plot_final:
Can you write Python code for it?

plot_final(X, cluster_assignments, updated_centers, new_object, assign_new_object)


I am working on the file "Day6/code/k-means_clustering.ipynb".
The first task is:
If the functions you completed above are working properly, you should see a figure containing a subplot of the output from steps (1) and (2) for four iterations of the algorithm. This plot should give you a sense of how the algorithm progresses over time. The data points are each assigned to one of three colors corresponding to their current cluster label. The cluster centers are plotted as stars.

Part D (1 point)
Now that we have assigned cluster labels to each datapoint, let's investigate how we should classify a new object (which we can see is a Shih-Tzu):

Complete the function template in `assign_new_object` to determine the appropriate cluster label for this new object.

**Note**: To complete the function, you will need to compute the distance between each cluster center and the new observation. Use the `distance` function from Part A.

Can you write Python code for it?

def assign_new_object(new_object, updated_centers):
    """
    Returns the cluster label (number) for new_object using k-means
    clustering.

    Parameters
    ----------
    new_object : numpy array of shape (2,)
        The (x,y) coordinates of a new object to be classified

    updated_centers : numpy array of shape (num_clusters,2)
        An array containing the updated (x,y) coordinates for
        each cluster center

    Returns
    -------
    label : int
       The cluster label assignment for new_object. This is a
       number between 0 and and (num_clusters - 1).
    """
    ### BEGIN SOLUTION
    return np.array([distance(new_object, c) for c in updated_centers]).argmin()
    ### END SOLUTION

# add your own test cases here!

"""Check assign_new_object computes the correct values"""
from nose.tools import assert_equal

# validate update_assignments using different values
centers1 = np.array([[ 3.17014624,  2.42738134], [ 2.90932354,  4.26426491]])
assert_equal(assign_new_object(np.array([0, 1]), centers1), 0)
assert_equal(assign_new_object(np.array([1, 0]), centers1), 0)
assert_equal(assign_new_object(np.array([3, 2]), centers1), 0)
assert_equal(assign_new_object(np.array([2, 4]), centers1), 1)

centers2 = np.array([[ 3.170146,  2.427381], [ 3.109456,  1.902395], [ 2.964183,  1.827484]])
assert_equal(assign_new_object(np.array([0, 1]), centers2), 2)
assert_equal(assign_new_object(np.array([1, 0]), centers2), 2)
assert_equal(assign_new_object(np.array([3, 2]), centers2), 1)
assert_equal(assign_new_object(np.array([2, 4]), centers2), 0)

# check that it uses distance
old_distance = distance
del distance
try:
    update_assignments(2, X, np.array([[3, 2], [1, 4]]))
except NameError:
    pass
else:
    raise AssertionError("assign_new_object does not call distance")
finally:
    distance = old_distance
    del old_distance

print("Success!")


I am working on the file "Day6/code/k-means_clustering.ipynb".
The first task is:
In this problem we will use $k$-means clustering on a dataset consisting of observations of dogs, cats, and mops. An example observation from each type of object is presented below:

We assume each observation can be represented as a pair of ($x,y$) coordinates, i.e., each object is represented in two-dimensional space. Suppose we have observed some obserations from each type of object, but have lost the information as to which instance belongs to which type!
To try and recover this information we will use an unsupervised learning algorithm called k-means clustering. As you may recall from lecture, the $k$ here refers to how many types of clusters we think exist in the data, and the goal of the algorithm is to assign labels to the data points using their distance to the centers (or means) of the clusters. For this particular problem, we assume $k=3$. After randomly initializing cluster centers,
the algorithm can be broken down into two alternating steps:

Update the label assignments of the data points based on the nearest cluster centers
Update the positions of the cluster centers to reflect the updated assignments of data points.

Before you begin, load the data we will be using. For answering the questions in this problem set, use the centers loaded from the X.npz  file below (i.e., do NOT randomly initialize the values yourself - the autograder for this problem relies on a "stock" initialization).
Can you write Python code for it?

# NOTE: we use non-random initializations for the cluster centers
# to make autograding feasible; normally cluster centers would be
# randomly initialized.

data = np.load('data/X.npz')
X = data['X']
centers = data['centers']

print ('X: \n' + str(X))
print ('\ncenters: \n' + str(centers))


I am working on the file "Day6/code/k-means_clustering.ipynb".
The first task is:
Once you've implemented assign_new_object, give it a spin on the image of the Shih-Tzu:
Can you write Python code for it?

new_object = np.array([3.3, 3.5]) # image coordinates
label = assign_new_object(new_object, updated_centers)
print ('The new object was assigned to cluster: '+ str(label))


I am working on the file "Day6/code/k-means_clustering.ipynb".
The first task is:
Part B (1.5 points)

Now, we need to do the next step of the clustering algorithm: recompute the cluster centers based on which points are assigned to that cluster. Recall that the new centers are simply the two-dimensional means of each group of data points. A two-dimensional mean is calculated by simply finding the mean of the x coordinates and the mean of the y coordinates. Complete the `update_parameters` function to do this.

Can you write Python code for it?

def update_parameters(num_clusters, X, cluster_assignment):
    """
    Recalculates cluster centers running update_assignments.

    Parameters
    ----------
    num_clusters : int
        The number of disjoint clusters (i.e., k) in
        the X

    X : numpy array of shape (m, 2)
        An array of m data points in R^2

    cluster_assignment : numpy array of shape (m,)
        The array of cluster labels assigned to each data
        point as returned from update_assignments

    Returns
    -------
    updated_centers : numpy array of shape (num_clusters, 2)
        An array containing the new positions for each of
        the cluster centers
    """
    ### BEGIN SOLUTION
    updated_centers = []
    for i in np.unique(cluster_assignment):
        cluster_idx = np.argwhere(cluster_assignment == i).ravel()
        updated_centers.append(np.mean(X[cluster_idx,:], axis=0))
    return np.asarray(updated_centers)
    ### END SOLUTION

# add your own test cases here!

"""Check update_parameters computes the correct values"""
from nose.tools import assert_equal
from numpy.testing import assert_allclose

# load the data
data = np.load('data/X.npz')
X = data['X']

# validate update_assignments using different values
cluster_assignment1 = np.array([
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0])
actual = update_parameters(2, X, cluster_assignment1)
expected = np.array([[ 3.24286584,  2.71362623], [ 2.80577245,  4.07633606]])
assert_allclose(expected, actual)

cluster_assignment2 = np.array([0, 1, 2, 2, 0, 2, 1, 2, 2, 2, 0, 0, 0, 0, 0])
actual = update_parameters(3, X[:X.shape[0]/2], cluster_assignment2)
expected = np.array([[ 3.4914304 ,  2.79181724], [ 3.03095255,  2.02958778], [ 2.86686881,  1.76070598]])
assert_allclose(expected, actual, rtol=1e-6)

print("Success!")


I am working on the file "Day6/code/k-means_clustering.ipynb".
The first task is:
Part C
At this stage you are ready to run the $k$-means clustering algorithm! The k_means function below will call your functions from Part A and B to run the k-means algorithm on the data points in X. Note that for this problem we assume that $k = 3$.
Call the function as so:
Can you write Python code for it?

# load the edata
data = np.load('data/X.npz')
X = data['X']
centers = data['centers']

# run k-means
cluster_assignments, updated_centers = k_means(3, X, centers, update_assignments, update_parameters, n_iter=4)


I am working on the file "Day6/code/k-means_clustering.ipynb".
The first task is:
Part E (1.5 points)
Let's go ahead and rerun $k$-means, to make sure we have the correct variables set:
Can you write Python code for it?


I am working on the file "Day6/code/k-means_clustering.ipynb".
The first task is:
This is the function you will run in Part C once you have completed the helper functions in parts A and B.

Part A (2 points)
First, we will need a function that gives us the distance between two points. We can use Euclidean distance to compute the distance between two points ($x_1,y_1$) and ($x_2,y_2$). Recall that Euclidean distance in $\mathbb{R}^2$ is calculated as:
$$
distance((x_1,y_1),(x_2,y_2)) = \sqrt{(x_1 - x_2)^{2} + (y_1 - y_2)^{2}}
$$
<div class="alert alert-success">
Complete the `distance` function below to calculate the euclidean distance between two points in $\mathbb{R}^2$.
</div>
Can you write Python code for it?

def distance(a, b):
    """
    Returns the Euclidean distance between two points,
    a and b, in R^2.

    Parameters
    ----------
    a, b : numpy arrays of shape (2,)
        The (x,y) coordinates for two points, a and b,
        in R^2. E.g., a[0] is the x coordinate,
        and a[1] is the y coordinate.

    Returns
    -------
    distance : float
        The Euclidean distance between a and b
    """
    ### BEGIN SOLUTION
    return np.sqrt((a[0]-b[0])**2 + (a[1]-b[1])**2)
    ### END SOLUTION

# add your own test cases here!

"""Check distances computes the correct values"""
from numpy.testing import assert_allclose

assert_allclose(distance(np.array([0.0, 0.0]), np.array([0.0, 1.0])), 1.0)
assert_allclose(distance(np.array([3.0, 3.0]), np.array([4.3, 5.0])), 2.3853720883753127)
assert_allclose(distance(np.array([130.0, -25.0]), np.array([0.4, 15.0])), 135.63244449614552)

print("Success!")


I am working on the file "Day6/code/notebook_debugging.ipynb".
The first task is:
Using print Statements
An alternative technique for inspecting the behavior of your code is to check the values of local variables using print statements. The print command evaluates its argument and writes the result to the standard output. We could use a print statement to inspect the ax object in our plot_log function as follows:
Can you write Python code for it?

def plot_log():
    figure, axis = plt.subplots(2,1)
    x = np.linspace(1, 2, 10)
    print(axis)
    axis.plot(x, np.log(x))
    plt.show()
plot_log()  # Call the function, generate plot


I am working on the file "Day_00/01_Basics/01_basicsTraining.ipynb".
The first task is:
Backslashes ( \ ) start special (escape) characters:
\n = newline
\r = return
\t = tab
\a = bell

String literals are defined with double (") or single quotes ('). <br>
The outermost type of quotation mark cannot be used inside a string -- UNLESS it's escaped with a backslash.
See http://docs.python.org/reference/lexical_analysis.html#string-literals
Can you write Python code for it?

# Raw strings don't recognize escape characters

print r'This is a raw string ... newlines \n are ignored. So are returns \r and tabs \t.'

# Triple quotes are useful for multiple line strings

y = '''Four score and seven minutes ago,
    you folks all learned some basic mathy stuff with Python
    and boy were you blown away!'''
print y

# Prepending 'u' makes a string "unicode"

print u"\N{BLACK HEART SUIT}"

# You can concatenate strings with the '+' sign

s = "spam" ; e = "eggs"

print s + e

print s + " and " + e

print "green " + e + " and " + s

# You can do multiple concatenations with the '*' sign

print s*3 + e

print "*"*50

# Strings can be compared

print "spam" == "good"

# s comes before z in the alphabet

"spam" < "zoo"

# 's' comes before 'spam' in the dictionary

"s" < "spam"

# 'spaa' comes before 'spam' alphabetically

"spaaaaaaaaaaaam" < "spam"

print 'I want ' + 3 + ' eggs and no ' + s

# We have to cast the 3 (an int) as a string

print 'I want ' + str(3) + ' eggs and no ' + s

pi = 3.14159     # There are easier ways to call pi, which we'll see later
print 'I want ' + str(pi) + ' eggs and no ' + s

print str(True) + ':' + ' I want ' + str(pi) + ' eggs and no ' + s

print s
print len(s)               # len() tells you the length of a string (or, more generally, an array)

print len("eggs\n")        # The newline \n counts as ONE character
print len("")              # empty string

# Strings act like arrays. We'll see more about arrays later.
s = "SPAM"

print s
print s[0]               # Python uses zero-based indexing; i.e., it starts counting at zero
print s[1]
print s[-1]
print s[-2]

# Take slices of strings
print s
print s[0:1]
print s[1:4]
print s[0:100]            # Python doesn't warn you. Be careful!


I am working on the file "Day_00/01_Basics/01_basicsTraining.ipynb".
The first task is:
Defining and using variables
Can you write Python code for it?

t = 1.0                       # declare a variable t (time)
accel = 9.8                   # acceleration in units of m/s^2
dist = 0.5*accel*t*t          # distance traveled in time t seconds is 1/2*a*t^2
print dist                    # this is the distance in meters

dist1 = accel*(t**2)/2        # note: t^2 means something very different!
print dist1

dist2 = 0.5*accel*pow(t,2)
print dist2


I am working on the file "Day_00/01_Basics/01_basicsTraining.ipynb".
The first task is:
IPython Notebook automatically converts tabs into spaces, but some programs do not. Be careful not to mix these up! Be consistent in your programming.
If you're working within the Python interpreter (not the IPython Notebook), you'll see this:
>>> x = 1
>>> if x > 0:
...     print "yo"
... else:
...     print "dude"
... print "ok"
...
yo
ok
Can you write Python code for it?

# You can mix indentations between different blocks ... but this is ugly and people will judge you

x = 1
if x > 0:
    print "yo"
else:
    print "dude"
    print "dude"

# You can put everything on one line

print "yo" if x > 0 else "dude"

# Multiple cases

x = -100
if x < -10:
    print "yo"
elif x > 10:             # 'elif' is short for 'else if'
    print "dude"
else:
    print "sup"

for x in range(5,50,10):
    print x**2

for x in ("all","we","wanna","do","is","eat","your","brains"):
    print x

x = 0
while x < 5:
    print pow(2,x)
    x += 1             # don't forget to increment x!

# Multiple levels
for x in range(1,10):
    if x % 2 == 0:
        print str(x) + " is even."
    else:
        print str(x) + " is odd."

# Blocks cannot be empty

x = "fried goldfish"
if x == "spam for dinner":
    print "I will destroy the universe"
else:
    # Nothing here.

# Use a 'pass' statement, which indicates 'do nothing'

x = "fried goldfish"
if x == "spam for dinner":
    print "I will destroy the universe"
else:
    pass

# Use a 'break' statement to escape a loop

x = 0
while True:
    print x**2
    if x**2 >= 100:
        break
    x +=1


I am working on the file "Day_00/01_Basics/01_basicsTraining.ipynb".
The first task is:
Python as a calculator
There are four distinct numeric types in Python:
   - int (32 bit, equivalent to long in C)
   - long (unlimited precision)
   - float (equivalent to double in C)
   - complex (real + imaginary parts, both floats)
Can you write Python code for it?

print 2 + 2

# Spaces between characters don't matter

print 2+2

2 + 2

print "2 + 2"

print 2.1 + 2            # The most precise value is a float.

(3.*10. - 26.)/5.

(3*10 - 26)/5.0

# Since our most precise value is an int, python spits out the solution as an int

(3*10 - 26)/5

# Rounding errors can creep in
2.1 + 2 == 4.0999999999999996      # two 'equals' signs asks whether something is equal

complex(1,2)

# note that python uses j to denote the imaginary part

1+2j

a = 1+2j
print a.real, a.imag

1+2j-2j


I am working on the file "Day_00/01_Basics/01_basicsTraining.ipynb".
The first task is:
Running a Program from Command Line
This is a good of place as any to introduce the show that you can run you don't HAVE to use the notebook.
Everyone should have a file called temperature.py, open the file in your text editor (emacs, vim, notepad) of choice at look at its content.
You will see it the same code as from the previous example. Let's try running it with the execfile command.
Can you write Python code for it?


I am working on the file "Day_00/01_Basics/01_basicsTraining.ipynb".
The first task is:
Some more mathy operators
Can you write Python code for it?

# Integer division prints the floor; i.e., it only takes the integer digits

print 6/5

# modulo operator
6 % 5

# bitwise operators: shift left
# 1 in binary is '1', shifting left by two bits gives '100' = 4

1 << 2

# bitwise operators: shift right
# 5 in binary is '101', shifting right by one bit gives '10' = 2

5 >> 1

x = 2 ; y = 3        # multiple commands on the same line, separated by a semicolon
x | y                # bitwise OR
                     # x in binary is '10', y in binary is '11', x | y is '11' -> 3

x ^ y                # exclusive OR ('10' ^ '11' = '01' = 1)

x & y                # bitwise AND ('10' & '11' = '10' = 2)

x = x ^ y ; print x   # x has been reassigned

x += 3 ; print x      # 'x += 3' is the same as saying 'x = x+3'
                      # the equivalent holds from -, *, /


I am working on the file "Day_00/01_Basics/01_basicsTraining.ipynb".
The first task is:
Truthiness
Can you write Python code for it?

0 == False       # False is equivalent to 0, and other things

1 == True        # True is equivalent to 1, and other things

not False

1 == False


I am working on the file "Day_00/01_Basics/01_basicsTraining.ipynb".
The first task is:
With python as in other languages you can separate scripts and programs containing python code that can be used independently or with other python programs.  The notebook is excellent for code development and presentations but not the best for production level code.
Loops and branches in python
Python has the usual control flow statements:
- if, else, elif
- for loops
- while loops
- break, continue, pass
Indentation in Python defines where blocks begin and end.
Can you write Python code for it?

x = 1
    print x

x = 1
if x > 0:               # colons indicate the beginning of a control statement
    print "yo"
else:                   # unindenting tells Python to move to the next case
    print "dude"
print "ok"              # unindenting also tells Python the control statement is done


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
A very short, whirlwind tour of regular expressions
Scanning
Can you write Python code for it?

kaiju_truth = 'Godzilla is the King of the Monsters. Ebirah is also a monster, but looks like a giant lobster.'
re.findall('Godz', kaiju_truth)

print re.findall('(^.+) is the King', kaiju_truth)


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
Advanced features
decode()/encode()/translate()
Used to convert strings to/from Unicode and other systems. Rarely used in science code.
String formatting
Similar to formatting in C, FORTRAN, etc.. There is a lot more to this than I am showing here.
Can you write Python code for it?

kaiju = 'Godzilla'
age = 60
print '%s is %d years old.' % (kaiju, age)


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
Changing
Can you write Python code for it?

some_kaiju = 'Godzilla, Space Godzilla, Mechagodzilla'
print re.sub('Godzilla', 'Gamera', some_kaiju)
print re.sub('(?i)Godzilla', 'Gamera', some_kaiju)


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
Concatenation
Can you write Python code for it?

kaiju1 = 'Godzilla'
kaiju2 = 'Mothra'
kaiju1 + ' versus ' + kaiju2


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
For simple searches like this, using in() is typically easier.
Regexps are by default case-sensitive.
Can you write Python code for it?

print re.findall('\. (.+) is also', kaiju_truth)

print re.findall('(.+) is also a (.+)', kaiju_truth)[0]
print re.findall('\. (.+) is also a (.+),', kaiju_truth)[0]


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
If the conversion cannot be done, an exception is thrown.
Can you write Python code for it?


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
Note the print() call shows no quotes, while the simple variable name did. That is a Python output convention. Just entering the name will call the repr() method, which displays the value of the argument as Python would see it when it reads it in, not as the user wants it.
Can you write Python code for it?


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
Repetition
Can you write Python code for it?


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
String functions
len()
Can you write Python code for it?


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
String information
isXXX()
Can you write Python code for it?

print('Godzilla'.isalnum())
print('*Godzilla*'.isalnum())
print('Godzilla123'.isalnum())

print('Godzilla'.isalpha())
print('Godzilla123'.isalpha())

print('Godzilla'.isdigit())
print('60'.isdigit())

print('SpaceGodzilla'.isspace())
print('   '.isspace())

print('Godzilla'.islower())
print('godzilla'.islower())

print('Godzilla'.isupper())
print('GODZILLA'.isupper())

print('Godzilla vs Mothra'.istitle())
print('Godzilla X Mothra'.istitle())


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
String literals
A "literal" is essentially a string constant, already spelled out for you. Python uses either on output, but that's just for formatting simplicity.
Can you write Python code for it?


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
String methods
Remember - methods are functions attached to objects, accessed via the 'dot' notation.
Basic formatting and manipulation
capitalize()/lower()/upper()/swapcase()/title()
Can you write Python code for it?

kaiju.capitalize()

kaiju.lower()

kaiju.upper()

kaiju.swapcase()

'godzilla, king of the monsters'.title()


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
String objects
String objects are just the string variables you create in Python.
Can you write Python code for it?


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
Strings and numbers
Can you write Python code for it?


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
The string module
The string module is the Python equivalent of "junk DNA" in living organisms. It's been around since the beginning, but many of its functions have been superseded by evolution. But some ancient code still relies on it, so they leave the old parts in....
For modern code, the string module does have some useful constants and functions.
Can you write Python code for it?

import string

print string.ascii_letters
print string.ascii_lowercase
print string.ascii_uppercase

print string.digits
print string.hexdigits
print string.octdigits

print string.letters
print string.lowercase
print string.uppercase

print string.printable
print string.punctuation
print string.whitespace


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
Triple quotes (''')
Triple quotes are a special form of quoting used for documenting your Python files (docstrings). We won't discuss that type here.
Raw strings
Raw strings don't use any escape character interpretation. Use them when you have a complicated string that you don't want to clutter with lots of backslashes. Python puts them in for you.
Can you write Python code for it?

print('This is a\ncomplicated string with newline escapes in it.')

print(r'This is a\ncomplicated string with newline escapes in it.')


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
You can also do this with hexadecimal and octal numbers, or any other base, for that matter.
Can you write Python code for it?


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
center()/ljust()/rjust()
Can you write Python code for it?

kaiju.center(20, '*')

kaiju.ljust(20, '*')

kaiju.rjust(20, '*')


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
expandtabs()
Can you write Python code for it?

tabbed_kaiju = '\tGodzilla'
print('[' + tabbed_kaiju + ']')

print('[' + tabbed_kaiju.expandtabs(16) + ']')


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
join()
Can you write Python code for it?

' vs '.join(['Godzilla', 'Hedorah'])

','.join(['Godzilla', 'Mothra', 'King Ghidorah'])


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
replace()
Can you write Python code for it?

battle = 'Godzilla vs Mothra'
battle.replace('Mothra', 'Anguiras')

battle = 'Godzilla vs a monster and another monster'
battle.replace('monster', 'kaiju', 2)

battle = 'Godzilla vs a monster and another monster and yet another monster'
battle.replace('monster', 'kaiju', 2)


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
split()/rsplit()
Can you write Python code for it?

battle = 'Godzilla vs King Ghidorah vs Mothra'
battle.split(' vs ')

kaijus = 'Godzilla,Mothra,King Ghidorah'
kaijus.split(',')

kaijus = 'Godzilla Mothra King Ghidorah'
kaijus.split()

kaijus = 'Godzilla,Mothra,King Ghidorah,Megalon'
kaijus.rsplit(',', 2)


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
splitlines()
Can you write Python code for it?

kaijus_in_lines = 'Godzilla\nMothra\nKing Ghidorah\nEbirah'
print(kaijus_in_lines)

kaijus_in_lines.splitlines()

kaijus_in_lines.splitlines(True)


I am working on the file "Day_00/02_Strings_and_FileIO/00 Strings in Python.ipynb".
The first task is:
zfill()
Can you write Python code for it?

age_of_Godzilla = 60
age_string = str(age_of_Godzilla)
print(age_string, age_string.zfill(5))


I am working on the file "Day_00/02_Strings_and_FileIO/01 File Input and Output.ipynb".
The first task is:
(The comma at the end suppresses extra newline). Can also use the object-oriented interface.
Can you write Python code for it?

f = file('kaiju_movies.dat')
for movie in f:
    print movie,
f.close()


I am working on the file "Day_00/02_Strings_and_FileIO/01 File Input and Output.ipynb".
The first task is:
Binary files
Open and close similar to text files, but use read() and write().
Can you write Python code for it?

f = open('nikki.jpg', 'rb')
my_dog = f.read()
f.close()
# Do arbitrary stuff with data.
f = open('new_nikki.jpg', 'wb')
f.write(my_dog)
f.close()


I am working on the file "Day_00/02_Strings_and_FileIO/01 File Input and Output.ipynb".
The first task is:
Format-specific binary I/O is available through standard modules, like Image.
Can you write Python code for it?

from IPython.display import Image
puppeh = Image(filename = 'nikki.jpg')
puppeh


I am working on the file "Day_00/02_Strings_and_FileIO/01 File Input and Output.ipynb".
The first task is:
Pickels!
The pickle is an internal Python format for writing arbitrary data to a file in a way that allows it to be read in again, intact.
Can you write Python code for it?

movies = [{'title': 'Godzilla', 'year': 1954}, {'title': 'Godzilla 2000: Millennium', 'year': 1999}]
import pickle
f = open('pickled_kaiju.pkl', 'wb')
pickle.dump(movies, f)
f.close()

f = open('pickled_kaiju.pkl', 'rb')
pickled_movies = pickle.load(f)
f.close()
print pickled_movies


I am working on the file "Day_00/03_Functions/00 Functions Modules.ipynb".
The first task is:
An Example
Can you write Python code for it?

def int(x,y):
    return x + y

type(1) == int

print addnums(0x1f,3.3)

print addnums("a","b")

print addnums("cat",23232)


I am working on the file "Day_00/03_Functions/00 Functions Modules.ipynb".
The first task is:
Built-In-Modules

  sys: exposes interpreter stuff & interactions

  os: exposes platform-specific OS functions

  math: basic mathematical functions & constants

  argparse: command line parser

Breakout Session: Exploring Some Modules

 Create and edit a new file called age.py

 Within age.py, import the datetime module

 use datetime.datetime() to create a variable representing when you were born

 subtract the two, forming a new variable, which will be a datetime.timedelta() object.
          Print that variable.

 How many days have you been alive? How many hours?

 What will be the date in 1000 days from now?

Can you write Python code for it?

import datetime
y = datetime.datetime.now()
print y

x = datetime.datetime(2016,6,13)
y-x

help(datetime)


I am working on the file "Day_00/03_Functions/00 Functions Modules.ipynb".
The first task is:
Import Statements
Can you write Python code for it?

from numfun2 import x, numop2

x == 2

numop2(2,3,2,greetings=None)

numfun2.s

numfun2.x

from numfun2 import s as my_fav_food
from numfun2 import numop2 as awesome_adder

print my_fav_food

print numfun2.s

awesome_adder(2,3,1)

import numfun2

from numfun2 import *

from numfun2 import x


I am working on the file "Day_00/03_Functions/00 Functions Modules.ipynb".
The first task is:
Python has it’s own local variables list.
x is not modified globally
...unless you specify that it’s a global variable
Can you write Python code for it?

def numop(x,y):
    x *= 3.14
    global a
    a += 1
    return x + y, a

a = 2
numop(1,1)
numop(1,1)


I am working on the file "Day_00/03_Functions/00 Functions Modules.ipynb".
The first task is:
Scope of a Function
Can you write Python code for it?

def numop(x,y):
    x *= 3.14
    return x + y
x = 2
print numop(x, 8)
print x


I am working on the file "Day_00/03_Functions/00 Functions Modules.ipynb".
The first task is:
Unspecified args and keywords
Can you write Python code for it?

def cheeseshop(kind, *arguments, **keywords):
    print "-- Do you have any", kind, "?"
    print "-- I'm sorry, we're all out of", kind
    for arg in arguments:
        print arg
    print "-" * 40
    keys = keywords.keys()
    keys.sort()
    for kw in keys:
        print kw, ":", keywords[kw]

cheeseshop("Limburger",
           "It's very runny, sir.",
           "It's really very, VERY runny, sir.",
           shopkeeper='Michael Palin',
           client="John Cleese",
           sketch="Cheese Shop Sketch")


I am working on the file "Day_00/03_Functions/00 Functions Modules.ipynb".
The first task is:
What is a Module?
<UL>
<LI> A Python object with arbitrarily named attributes that you can bind and reference.
<LI> A file consisting of Python code.
<LI> Allows you to logically organize your Python code.
<LI> Makes the code easier to understand and use.
<LI> Can define functions, classes and variables.
<LI> Can also include runnable code.
</UL>
<B> Any file ending in .py is treated as a module. </B>
Can you write Python code for it?

# %load "numfun1.py"
#!/usr/env python

"""
small demo of modules
"""
def numop1(x,y,multiplier=1.0,greetings="Thank you for your inquiry."):
    """
    numop1 -- this does a simple operation on two numbers.
              We expect x,y are numbers and return x + y times the multiplier.
              multiplier is also a number (a float is preferred) and is
              optional. It defaults to 1.0.
              You can also specify a small greeting as a string.
    """
    if greetings is not None:
       print greetings
    return (x + y)*multiplier

# %load "numfun1.py"
#!/usr/env python

"""
small demo of modules
"""
def numop1(x,y,multiplier=1.0,greetings="Thank you for your inquiry."):
    """
    numop1 -- this does a simple operation on two numbers.
              We expect x,y are numbers and return x + y times the multiplier.
              multiplier is also a number (a float is preferred) and is
              optional. It defaults to 1.0.
              You can also specify a small greeting as a string.
    """
    if greetings is not None:
       print greetings
    return (x + y)*multiplier

numfun1.numop1(2,3,2,greetings=None)

numop1(2,3,2,greetings=None)

# %load "numfun2.py"
#!/usr/env python

"""
small demo of modules
"""

print "numfun2 in the house"
x  = 2
s  = "spamm"

def numop2(x,y,multiplier=1.0,greetings="Thank you for your inquiry."):
    """
    Purpose: does a simple operation on two numbers.

    Input: We expect x,y are numbers
           multiplier is also a number (a float is preferred) and is optional.
           It defaults to 1.0. You can also specify a small greeting as a string.

    Output: return x + y times the multiplier
    """
    if greetings is not None:
       print greetings
    return (x + y)*multiplier

print numfun2.x, numfun2.py

s = "eggs"
print s, numfun2.s

numfun2.s = 'jack'
print s, numfun2.s


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
A function can take any number of parameters as input.

The number of arguments passed to the function through the parameters generally matches the number of parameters.
Try passing fewer or more than 2 arguments to add().
 What happens?
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
And call it with arguments 1 and 2:
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Compare your working result with your neighbor's.
 How did you each treat the odd slopes?
 What might be advantages or disadvantages to your various solutions?
Breakout session
You want to calculate the overlap of two circles given the position of their centers (x1,y1) and (x2,y2) and their radii r1 and r2.
You know that the overlap is 0 if the distance between the two circles is greater than the sum of their radii.

You also know the overlap is the area of the smaller circle if one circle is contained within the other.
Your colleague wrote some code to calculate the area of the intersection, if the two circles overlap but don't fall in one of the special cases just mentioned (no overlap or fully contained). This is great! Unfortunately your colleague wasn't a fan of functions, so you know only that the area of intersection is:
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Functions can also call other functions!

Recall the equation for a line: y=mx+b

Write a function called line() that calls add() and returns y, given x.
Can you write Python code for it?

def line(x):
    '''return the y value of a line, given the x value'''
    m = 2
    b = 0
    y=add(m*x,b)
    return y

line(1)


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
It's comprised of 3 parts:
 - the header, which defines the function
 - the comment, which lets users know what the function does
 - and the body, which actually does the task required.
What does spam do when we call it?
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Note the error message when you first try to evaluate d: python doesn't know what math is. <br>
Math turns out to be one of those useful libraries of functions (and variables like pi). You can import them within the scope of a variable you call (as) math with:
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Note which version of import your colleague used in their code.
Hint! Part 2:
A. By definition in Part 1, the maximum area of overlap is when one circle is fully contained within the other. Thus the fractional overlap is defined as the intersection area divided by the minimum of the radii.
B. There are several ways to convert the given error measurement (x1Err, y1Err) into a radius. Consider the relative sizes and their impact on the final fraction calculated. Optionally, allow the user to select which option to use!
Uncompleted parts make good homework!
More useful exercises:
Try writing your functions in a file and running the code from the terminal.

Hint: Calling "python filename.py" will execute the code in filename.py.
Write functions to read in the data from files.
Write a wrapper function that takes the data read in from the files and calls the fractional overlap functions.

candidates.txt contains the first sources' information, including position, position error, extension, and extension error (all in degrees, and incidentally in the RA & Dec coordinate system; just assume cartesian geometry!).

flare.txt contains the second sources' information, including position and position error radius (also in degrees and RA,Dec).
Write the results to the terminal and then to a file.
How else might you display the results?
Use matplotlib to display the interesting results!
Use the following data* within the notebook to test your functions with:
*(extracted from candidates.txt and flare.txt)
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Notice the different parts of the header in spam.
What are the minimum parts necessary to define the function?
Clearly we typically want to do more than just print out something predefined though!
Let's return to our intial addition example.

Write a function add1() that takes one input parameter, adds 1 to it, and returns the result.
Can you write Python code for it?

def add1(param):
    '''add 1 to the input param'''
    y=param+1
    return y


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Now try executing it:
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Part 1: Interarea()
Define a function called interarea() which takes as input the positions and radii of the two circles and returns the area of the intersection.
Feel free to rename your colleague's variables if a different convention makes more sense to you.
Write at least 3 test cases to be sure your functions work for all scenarios!
(There are hints at the end of the exercise's parts.)
Can you write Python code for it?

import math as math
def interarea(x1,y1,r1,x2,y2,r2) :
    '''return the overlap area of two circles given the center points (x1, y1) and (x2, y2) and their radii: r1 and r2'''
    d = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
    rr1=r1**2
    rr2=r2**2
    if d<abs(r2-r1) :
        print "100% overlap!"
        return math.pi*min(r1,r2)**2
    elif d>=r1+r2:
        print "No overlap!"
        return 0
    else :
        phi = (math.acos((rr1 + (d ** 2) - rr2) / (2 * r1 * d))) * 2
        theta = (math.acos((rr2 + (d ** 2) - rr1) / (2 * r2 * d))) * 2
        area1 = 0.5 * theta * rr2 - 0.5 * rr2 * math.sin(theta)
        area2 = 0.5 * phi * rr1 - 0.5 * rr1 * math.sin(phi)
        area= area1 + area2
        print "partial overlap: area = ", area
        return area


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Suppose you want evenSlopedLine() to just work. Use add1() to make it so.
Why might this be good? <br>
What are some potential drawbacks?
Can you write Python code for it?

def evenSlopedLine(x, m=0, b=0) :
    '''return the y value for a line with input x and optinal m and b'''
    if m%2==0 :
        y=add(m*x, b)
        return y
    else :
        m=add1(m)
        return add(m*x, b)


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Test it with m = 2 and m = 3:
Can you write Python code for it?

def test(x, vals):
    """Return the values of the function evenSlopedLine given x and an array of slopes."""
    result=[]
    for n in vals :
        result.append(evenSlopedLine(x, n))
    return result

x=1
m=[2,3]
test(x, m)


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
Typically we import modules like math globally. <br>
It's also possible to import from the math module just the particular function desired:
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
What output did you get?
Compare to your neighbor's add1 function.
What does the second part of your function (ie the comment) say?
Why do we care about commenting our code?
Now make a function called 'add' that takes two numbers as input parameters, adds them, and returns that number.
Can you write Python code for it?

def add(a,b) :
    '''how to add to numbers! ... or two stings'''
    return a+b


I am working on the file "Day_00/03_Functions/Functions.ipynb".
The first task is:
ipython has some built in functions, like the addition operator, symbolized as the plus sign, and 'print'.
Much of the rest of these lectures will be focused on learning to use the many libraries of code developed by others to do some pretty spectacular things.
Before then, let's explore how we actually write functions!
Here's an example of a function:
Can you write Python code for it?

def spam() :
    """print eggs!"""
    print "Eggs!"
    return 0


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
And call it with arguments 1 and 2:
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
It's comprised of 3 parts:
 - the header, which defines the function
 - the comment, which lets users know what the function does
 - and the body, which actually does the task required.
What does spam do when we call it?
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
NB: candidatesData and flaresData are dictionaries. The data in dicts is accessible via the keys, eg candidatesData[candidateName][variableName] :
Can you write Python code for it?

candidatesData['SNR357.0-1.0']['raErr']

from IPython.display import Image
overlapImage1 = Image(filename = 'SNR347.3-00.5_GeVext_overlap.png')
overlapImage1

overlapImage2 = Image(filename = 'SNR111.7-02.1_GeVext_overlap.png')
overlapImage2


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
Note the error message when you first try to evaluate d: python doesn't know what math is. <br>
Math turns out to be one of those useful libraries of functions (and variables like pi). You can import them within the scope of a variable you call (as) math with:
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
Note which version of import your colleague used in their code.
Hint! Part 2:
A. By definition in Part 1, the maximum area of overlap is when one circle is fully contained within the other. Thus the fractional overlap is defined as the intersection area divided by the minimum of the radii.
B. There are several ways to convert the given error measurement (x1Err, y1Err) into a radius. Consider the relative sizes and their impact on the final fraction calculated. Optionally, allow the user to select which option to use!
Uncompleted parts make good homework!
More useful exercises:
Try writing your functions in a file and running the code from the terminal.

Hint: Calling "python filename.py" will execute the code in filename.py.
Write functions to read in the data from files.
Write a wrapper function that takes the data read in from the files and calls the fractional overlap functions.

candidates.txt contains the first sources' information, including position, position error, extension, and extension error (all in degrees, and incidentally in the RA & Dec coordinate system; just assume cartesian geometry!).

flare.txt contains the second sources' information, including position and position error radius (also in degrees and RA,Dec).
Write the results to the terminal and then to a file.
How else might you display the results?
Use matplotlib to display the interesting results!
Use the following data* within the notebook to test your functions with:
*(extracted from candidates.txt and flare.txt)
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
Now try executing it:
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
Part 1: Interarea()
Define a function called interarea() which takes as input the positions and radii of the two circles and returns the area of the intersection.
Feel free to rename your colleague's variables if a different convention makes more sense to you.
Write at least 3 test cases to be sure your functions work for all scenarios!
(There are hints at the end of the exercise's parts.)
Can you write Python code for it?

#################################
# Find the intersection of 2 circles w centers at (x1, y1) and (x2, y2) and radii r1 and r2.
# Arguments:
#   x1, y1 the centre point of the first circle
#   r1 radius of the first circle
#   x1, y1 the centre point of the second circle
#   r2 radius of the second circle
# Returns:
#   zero if the circles do not intersect or just in one point
#   area of the intersection of the  !first! circle
#   If R1> R2, returns area of circle 1 interacting with Circle 2
#   If R1< R2, returns area of circle 1
def interarea(x1,y1,r1,x2,y2,r2):

    d = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)

    if (d >= r1 + r2):
        # Circles don't intersect or intersect in just one point
        return 0
    elif ( d <= abs(r1 - r2) and r2>r1):
        # Circle 1 is fully contained, return its area
        return math.pi*r1**2
    elif ( d <= abs(r1 - r2) and r2<r1):
        # Circle 1 is fully contained, returning its area
        #print "d=",d,"r1=", r1,"r2=", r2
        return math.pi*r2**2
    else:
        # Circle 1 and 2 are intersecting
        rr1=r1**2
        rr2=r2**2
        phi = (math.acos((rr1 + (d ** 2) - rr2) / (2 * r1 * d))) * 2
        theta = (math.acos((rr2 + (d ** 2) - rr1) / (2 * r2 * d))) * 2
        area1 = 0.5 * theta * rr2 - 0.5 * rr2 * math.sin(theta)
        area2 = 0.5 * phi * rr1 - 0.5 * rr1 * math.sin(phi)
        # Return area of intersection
        return area1 + area2


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
Redefine line() with user-defined slope and intercept.
Can you write Python code for it?

def line(x,m=2,b=0):
    """Return the y value of a line given x, the slope m, and the intercept b"""
    y = add(m*x,b)
    return y

line(1, 2, 0)


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
Suppose you want evenSlopedLine() to just work. Use add1() to make it so.
Why might this be good?

What are some potential drawbacks?
Rewrite test() as evenSlopesYs, taking an optional x value and returning the array of y values for slopes in the array: m = [1,2,3,4,5,6,7,8,9,10] .

Hint: try different orders for the optional and required parameters!
Can you write Python code for it?

def evenSlopesYs(vals,x=1):
    """Return the values of the function evenSlopedLine given x and an array of slopes."""
    result=[]
    for n in vals :
        result.append(evenSlopedLine(x, n))
    return result

#x=1
m = [1,2,3,4,5,6,7,8,9,10]
evenSlopesYs(m)


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
We can also rename it as:
Can you write Python code for it?

from math import sqrt as squareroot

squareroot(25)


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
What happens when you call line with only one, two, or three parameters? <br>
What's the minimum number of parameters required?
Can you write Python code for it?


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
What if we the slope and intercept to be default values of 2 and 0, respectively, but changable sometimes?
Hint: try setting the parameters to equal the default values!
Can you write Python code for it?

def line(x,m=2,b=0):
    """Return the y value of a line given x and optionally the slope m and intercept b"""
    y = add(m*x,b)
    return y


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
What if you only want to allow the function to work on slopes that are even? <br>
Write evenSlopedLine() such that a naieve user can call it and learn something useful about how to use the function if they give it an odd slope value.
Can you write Python code for it?

def evenSlopedLine(x,m=2,b=0):
    """Return the y value of a line for slopes which are even.
    Let the user know why it failed and how to make it work if the slope is odd."""
    if m%2==0 :
        return line(x,m,b)
    else :
        return "evenSlopedLine only works for even slopes. Please put in a slope that's divisible by 2!"


I am working on the file "Day_00/03_Functions/FunctionsSolutions.ipynb".
The first task is:
What output did you get?
Compare to your neighbor's add1 function.
What does the second part of your function (ie the comment) say?
Why do we care about commenting our code?
Now make a function called 'add' that takes two numbers as input parameters, adds them, and returns that number.
Can you write Python code for it?

def add(a, b):
    """add two numbers together"""
    n = a+b
    return n


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
Above we saw how to index arrays with single numbers and slices, just like Python lists.  But arrays allow for a more sophisticated kind of indexing which is very powerful: you can index an array with another array, and in particular with an array of boolean values.  This is particluarly useful to extract information from an array that matches a certain condition.
Can you write Python code for it?

redshifts =  np.array((0.2, 1.56, 6.3, 0.003, 0.9, 4.54, 1.1))

print redshifts

close = redshifts < 1
print close
print redshifts[close]

far = np.where(redshifts > 2)
print far[0][0]
print redshifts[far]

middle = np.where( (redshifts >= 1) & (redshifts <= 2) )
print middle
print redshifts[middle]

print (redshifts >= 1) & (redshifts <= 2)


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
Create an identity array:
Can you write Python code for it?

print np.eye(5, dtype=int)    # default data type is 'float'


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
If you know the size of the array you want, you can create an array of ones or zeros or an empty array:
Can you write Python code for it?

b = np.ones(3)
print b
print b.shape

c = np.zeros((1,3), int)
print c
print type(c)
print c.dtype

print [3, 5, 6]
print (3, 5, 6)

a = [3,5,6]
b = (3,5,6)
b[0] = 1
print b

d = np.zeros(3, complex)
print d
print d.dtype

# slightly faster, but be careful to fill it with actual values!

f = np.empty(4)
f.fill(3.14)
f[-1] = 0
print f


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
In general, mathematical operations on arrays are done element-by-element:
Can you write Python code for it?

arr1 = np.arange(4)
arr2 = np.arange(10,14)
print arr1
print arr2

print arr1 + arr2
print arr1 - arr2
print arr1 * arr2
print arr1 / arr2


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
In the last example, numpy understood the command to be "add 3.5 to every element in arr1." That is, it converts the scalar 3.5 into an array of the appropriate size. Since the new array is filled with floats, and arr1 is filled with ints, the summed array is an array of floats.
Broadcasting with numpy
The broadcasting rules allow numpy to:

create new dimensions of length 1 (since this doesn't change the size of the array)
'stretch' a dimension of length 1 that needs to be matched to a dimension of a different size.

So in the above example, the scalar 1.5 is effectively:

first 'promoted' to a 1-dimensional array of length 1
then, this array is 'stretched' to length 4 to match the dimension of arr1.

After these two operations are complete, the addition can proceed as now both operands are one-dimensional arrays of length 4.
This broadcasting behavior is in practice enormously powerful, especially because when numpy broadcasts to create new dimensions or to 'stretch' existing ones, it doesn't actually replicate the data.  In the example above the operation is carried as if the 1.5 was a 1-d array with 1.5 in all of its entries, but no actual array was ever created.  This can save lots of memory in cases when the arrays in question are large and can have significant performance implications.
Can you write Python code for it?

arr1 += 10
print arr1

arr1.fill(0)
print arr1

print arr2
print np.mean(arr2), arr2.mean()
print np.sum(arr2), arr2.sum()
print np.min(arr2), arr2.min()


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
Multidimensional arrays work like you might expect:
Can you write Python code for it?

newArray = np.array([ [3, 8, 0, 1],
                      [4, 0, 0, 9],
                      [2, 2, 7, 1],
                      [5, 1, 0, 8]] )
print newArray
print newArray.shape

print newArray[:,-1]

print newArray[1,3]

print newArray[3,0:2]

print newArray[:,0]   # print out an individual column


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
None of these manipulations have modified the original newArray:
Can you write Python code for it?

print newArray

reshapedArray = newArray.reshape(2,8)
print reshapedArray
print newArray


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
Notice how matplotlib automatically assigned different colors to the two data sets. We can finetune what these look like:
Can you write Python code for it?

plt.plot(x, np.sin(x), linewidth=3, color='red', linestyle='--')

plt.plot(x, np.sin(x), 'o--', markersize=5, color='k')

### logarithmic plots
x = np.linspace(-5, 5)
y = np.exp(-x**2)
plt.semilogy(x,y, lw=2, color='0.1')

plt.loglog(x,y)
plt.xlim(1e-1, 1e1)
plt.ylim(1e-2, 1e1)

# A more complicated example

mu, sigma = 5.9, 0.2
measurements = mu + sigma * np.random.randn(10000)

# the histogram of the data
plt.hist(measurements, 50, normed=False, facecolor='g', alpha=0.75, label='hist')

plt.xlabel('Height (feet)')
plt.ylabel('Number of people')
plt.title('Height distribution')
# This will put a text fragment at the position given:
plt.text(5.1, 600, r'$\mu=100$, $\sigma=15$', fontsize=14)
plt.axis([5,6.7,0,750])
plt.grid(True)
plt.legend()

### Error bars and subplots

# generate some fake data, with errors in y
x = np.arange(0.2, 4, 0.5)
y = np.exp(-x)
errors = 0.1 + 0.1*np.sqrt(x)

fig = plt.figure()

noErrs = fig.add_subplot(121)
plt.plot(x, y)
plt.xticks((0,1,2,3,4))
noErrs.set_title('No errors')
noErrs.set_ylim(-0.3,1)

withErrs = fig.add_subplot(122)
withErrs.errorbar(x, y, yerr=errors)
withErrs.set_title('With errors')
plt.ylim(-0.3,1)
plt.xticks(np.arange(0,4.5,0.5))

fig.suptitle('fake data', size=16, weight='bold')

# normal distribution center at x=0 and y=5
x = np.random.randn(100000)
y = np.random.randn(100000)+5

# let's make this plot bigger
plt.figure(figsize=(12,8))
plt.hist2d(x, y, bins=40)
plt.colorbar()


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
Notice in particular that multiplication is element-wise and is NOT a dot product or regular matrix multiplication.
Can you write Python code for it?


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
Number generation
Here are a few ways to generate uniformly spaced numbers over an interval (including both endpoints), similar to range():
Can you write Python code for it?

print np.arange(-5, 5, 0.5)    # excludes upper endpoint

print np.linspace(-3, 3, 21)     # includes both endpoints

print np.logspace(1, 4, 4)


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
Some examples of random number generation using numpy
Can you write Python code for it?

print np.random.rand(10)
print np.random.rand(2,2)

print np.()

print np.random.randint(2,100,5)

print np.random.normal(10, 3, (2,4))

print np.random.randn(5)    # samples 5 times from the standard normal distribution

print np.random.normal(3, 1, 5)    # samples 5 times from a Gaussian with mean 3 and std dev 1


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
What is Matplotlib?
The matplotlib library is a powerful tool capable that can quickly produce simple plots for data visualization as well as complex publication-quality figures with fine layout control. Here, we will only provide a minimal introduction, but Google is your friend when it comes to creating the perfect plots.  The pyplot tutorial (http://matplotlib.org/users/pyplot_tutorial.html) is a great place to get started, and the matplotlib gallery (http://matplotlib.org/gallery.html) has tons of information as well.
Just as we typically use the shorthand np for Numpy, we will use plt for the matplotlib.pyplot module where the plotting functions reside.
Can you write Python code for it?

import matplotlib.pyplot as plt
%matplotlib inline


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
Working with and manipulating arrays
Can you write Python code for it?

print newArray

print newArray.reshape(2,7)

print newArray.reshape(-1,2)


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
You can access elements of an array in the same way you access elements of a list:
Can you write Python code for it?

print myArray

print myArray[-2]

print myArray[1]
print myArray[1:2]


I am working on the file "Day_00/04_Numpy_Matplotlib/szhu_NumpyMatplotlib.ipynb".
The first task is:
You can also convert a list or tuple of elements into an array:
Can you write Python code for it?

myList = [0.2, 0.4, 0.6]
myArray = np.array(myList)
print myList
print myArray

print type(myArray)

print myArray.dtype


I am working on the file "Day_01/00_Lists/00_ListDictionaryTuples.ipynb".
The first task is:
Dictionary

 An array with with text indices
         (keys, even user-defined objects can be indices!)

 Also called hash or associative array in other languages

 Can store 'anything'

 The text index is called key

Initializing a Dictionary
Can you write Python code for it?

d = {}    # empty dictionary

value1 = 'Python BootCamp'
value2 = 2016
oxford = { 'key1' : value1, 'key2' : value2 }
oxford = dict(key1=value1, key2=value2)

d['list']  = [1.2, 2.5]
d['tuple'] = 'a','b','c'
d['pi']    = 3.14159265359
print d


I am working on the file "Day_01/00_Lists/00_ListDictionaryTuples.ipynb".
The first task is:
Add Elements to a List
Can you write Python code for it?

myList.insert(0,"Yes")
len(myList)

myList.append(["a", "true"])
len(myList)

myList.extend(["statement", "for", "sure"])
print myList
len(myList)


I am working on the file "Day_01/00_Lists/00_ListDictionaryTuples.ipynb".
The first task is:
Common Operations
Can you write Python code for it?

d['pi']         # extract item corresp. to key 'mass’
d.keys()          # return copy of list of keys
len(d)            # the number of items
d.get('pi',1.0) # return 1.0 if 'mass' is not a key
d.has_key('pi') # does d have a key 'mass'?
d.values()        # return a list of all values in the dictionary
d.items()         # return list of (key,value) tuples
d.copy()          # create a copy of the dictionary
del d['pi']     # delete an item
d.clear()         # remove all key/value pair from the dictionary


I am working on the file "Day_01/00_Lists/00_ListDictionaryTuples.ipynb".
The first task is:
Operations on Tuples
Can you write Python code for it?

print len(t3)

print "c" in t3

for x in t3:
    print x


I am working on the file "Day_01/00_Lists/00_ListDictionaryTuples.ipynb".
The first task is:
Problem 1
Consider the list:
Can you write Python code for it?


I am working on the file "Day_01/00_Lists/00_ListDictionaryTuples.ipynb".
The first task is:
Python List Operators
Can you write Python code for it?

# Combine two lists
print myList
myList = myList + ["sure"]
print myList

myList += ["."]
print myList

# Repeat the list
myList *= 2
print myList


I am working on the file "Day_01/00_Lists/00_ListDictionaryTuples.ipynb".
The first task is:
Search the Lists and find Elements
Can you write Python code for it?

# which element in the list is 'revolves'?
myList.index("revolves")

# which element in the list is 'true'?
myList.index("true")

myList.index(["a", "true"])

"sun" in myList

"true" in myList


I am working on the file "Day_01/00_Lists/00_ListDictionaryTuples.ipynb".
The first task is:
Tuple = constant list; items cannot be modified
Can you write Python code for it?


I am working on the file "Day_01/00_Lists/01_ListDictionaryTuples-Solutions.ipynb".
The first task is:
Tuples
Problem 2
Consider the tuple:
Can you write Python code for it?

julia = ("Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia")


I am working on the file "Day_01/00_Lists/01_ListDictionaryTuples-Solutions.ipynb".
The first task is:
Use operations on lists to manipulate oldList and create the list [[1,2],[4,6]]
Solution 1
Can you write Python code for it?

a = oldList[0]
print a
a.remove(3)
b = oldList[1]
print b
b.remove(5)
newList = []
newList.append(a)
print newList
newList = newList.append(b)
print newList


I am working on the file "Day_01/01_Advanced_Python/01_ListComprehensions.ipynb".
The first task is:
<u>Problem 1</u>
Can you write Python code for it?

# We want to simulate a series of coin tosses where 0 is heads and 1 is tails.
from random import random
n = 10
results = []
for x in range(n):
    results.append(int(round(random())))


I am working on the file "Day_01/01_Advanced_Python/01_ListComprehensions.ipynb".
The first task is:
A working example:
Can you write Python code for it?

old_list = ['bob','loblaw','law blog']
new_list = []
for i in old_list:
    if 'law' in i:
       new_list.append(i.strip('law'))

print new_list


I am working on the file "Day_01/01_Advanced_Python/01_ListComprehensions.ipynb".
The first task is:
Examples
Can you write Python code for it?

x = [i for i in range(10)]
print x

# You can either use loops:
squares = []

for x in range(10):
    squares.append(x**2)

print squares

# Or you can use list comprehensions to get the same result:
squares = [x**2 for x in range(10)]

print squares

string = "Hello 12345 World"
numbers = [x for x in string if x.isdigit()]
print numbers

# Create a function and name it double:
def double(x):
  return x*2

# If you now just print that function with a value in it, it should look like this:
print double(10)

# You can apply function to a list
A = [double(x) for x in range(10)]
print A

# You can put in conditions:
B = [double(x) for x in range(10) if x%2==0]
print B

# You can add more arguments:
C = [x+y for x in [10,30,50] for y in [20,40,60]]
print C

#List comprehensions can contain complex expressions and nested functions:
from math import pi
D = [str(round(pi, i)) for i in range(1, 6)]
print D

# Calculation of the prime numbers between 1 and 100 using the sieve of Eratosthenes:
n = 100

# Without List Comprehension
#---------------------------
noprimes = []
for i in range(2, 8):
    for j in range(i*2, n, i):
        noprimes.append(j)
primes = []
for x in range(2, n):
    if x not in noprimes:
        primes.append(x)

# With List Comprehension
#------------------------
noprimes = [j for i in range(2, 8) for j in range(i*2, n, i)]
primes = [x for x in range(2, n) if x not in noprimes]
print primes


I am working on the file "Day_01/01_Advanced_Python/01_ListComprehensions.ipynb".
The first task is:
Here is what it does:
Can you write Python code for it?

new_list = [expression(i) for i in old_list if filter(i)]


I am working on the file "Day_01/01_Advanced_Python/01_ListComprehensions.ipynb".
The first task is:
Syntax
The list comprehension starts with a <B>'[' and ']'</B>, to help you remember that the
result is going to be a list.
The basic syntax is
Can you write Python code for it?

[ expression for item in list if conditional ]


I am working on the file "Day_01/01_Advanced_Python/01_ListComprehensions.ipynb".
The first task is:
Use a list comprehension to make it more concise.
<u>Problem 2</u>
Use list comprehension to remove all the vowels from the sentence
Can you write Python code for it?

sentence = "The GFSC Python Bootcamp is a great opportunity to learn Python programming."
vowels = 'aeiou'


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
<UL>
<LI> An exception is an error that happens during the execution of a program.
<LI> Exceptions are known to non-programmers as instances that do not conform to a general rule.
<LI> Exception handling is a construct to handle or deal with errors automatically.
<LI> The code, which harbours the risk of an exception, is embedded in a try block.
</UL>

Simple example
Can you write Python code for it?

def enter_number0():
    n = int(raw_input("Please enter a number: "))

enter_number0()

def enter_number1():
    while True:
        try:
           n = raw_input("Please enter an integer: ")
           n = int(n)
           break
        except ValueError:
            print "No valid integer! Please try again ..."
    print "Great, you successfully entered an integer!"

enter_number1()


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
<u>Problem 1</u>
Can you write Python code for it?

words = 'The quick brown fox jumps over the lazy dog'.split()
print words
stuff = []
for w in words:
    stuff.append([w.upper(), w.lower(), len(w)])

for i in stuff:
    print i


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
<u>Problem 2</u>
Use the filter function to remove all the vowels from the sentence
Can you write Python code for it?

sentence = "It's a myth that there are no words in English without vowels."
vowels = 'aeiou'


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
<u>Problem 3</u>
Use the reduce function to find the product of all the entries in the list
[47,11,42,102,13]
Can you write Python code for it?

print reduce(lambda x,y: x*y, [47,11,42,102,13])

# note that you can improve the speed of the calculation using built-in functions
# or better still: using the numpy module
from operator import mul
import numpy as np
a = range(1, 101)

print "reduce(lambda x, y: x * y, a)"
%timeit reduce(lambda x, y: x * y, a)   # (1)

print "reduce(mul, a)"
%timeit reduce(mul, a)                  # (2)

print "np.prod(a)"
a = np.array(a)
%timeit np.prod(a)                      # (3)


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
Clean-up Actions (try ... finally)
Can you write Python code for it?

def inverse_number2():
    try:
        x = float(raw_input("Your number: "))
        inverse = 1.0 / x
    finally:
        print "There may or may not have been an exception."
    print "The inverse: ", inverse

inverse_number2()

def inverse_number3():
    try:
        x = float(raw_input("Your number: "))
        inverse = 1.0 / x
    except ValueError:
        print "You should have given either an int or a float"
    except ZeroDivisionError:
        print "Infinity"
    finally:
        print "There may or may not have been an exception."

inverse_number3()


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
Examples
Can you write Python code for it?

A = reduce(lambda x,y: x+y, [47,11,42,13])
print A

# Determining the maximum of a list of numerical values by using reduce
f = lambda a,b: a if (a > b) else b
B = reduce(f, [47,11,42,102,13])
print B

# Calculating the sum of the numbers from 1 to n:
n = 300
C = reduce(lambda x, y: x+y, range(1,n+1))
print C

def x100y(x,y):
    return 100*x+y

reduce(x100y, [13])
reduce(x100y, [2, 5, 9])
reduce(x100y, [2, 5, 9], 7)


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
Lambda as macro
Can you write Python code for it?

line1 = "A cat, a dog  "
line2 = "  a bird, a mountain"

# Use X as an alias for two methods.
x = lambda s: s.strip().upper()

# Call the lambda to shorten the program's source.
line1b = x(line1)
line2b = x(line2)

print(line1b)
print(line2b)


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
Map Function
map() is a function with two arguments:
Can you write Python code for it?


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
Raising Exceptions
Can you write Python code for it?

def achilles_arrow(x):
    if abs(x - 1) < 1e-3:
        raise StopIteration
    x = 1 - (1-x)/2.
    return x

x=0.0

while True:
    try:
        x = achilles_arrow(x)
    except StopIteration:
        break

print "x = ", x


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
Reduce Function
The function <B>reduce(func, seq)</B> continually applies the function func() to the sequence seq. It returns a single value.
Syntax
Can you write Python code for it?

def reduce( aFunction, aSequence, init= 0 ):
    r= init
    for s in aSequence:
        r= aFunction( r, s )
    return r


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
Some Exception Errors

 IOError: The file cannot be opened

 ImportError: Python cannot find the module

 ValueError: A built-in operation or function receives an argument that has the
right type but an inappropriate value.

 KeyboardInterrupt: The user hits the interrupt key (normally Control-C or Delete)

 EOFError: One of the built-in functions (input() or raw_input()) hits an
end-of-file condition (EOF) without reading any data.

  OverflowError, ZeroDivisionError, FloatingPointError:

An exhaustive list of built-in exceptions can be found here:
https://docs.python.org/2/library/exceptions.html
else...
Can you write Python code for it?

def inverse_number0():
    try:
        x = float(raw_input("Your number: "))
        inverse = 1.0 / x
    except ValueError:
        print "You should have given either an int or a float"
    except ZeroDivisionError:
        print "Infinity"
    else:
        print "OK"

inverse_number0()

# import module sys to get the type of exception
import sys

def inverse_number1():
    while True:
        try:
            x = int(raw_input("Enter an integer: "))
            r = 1/x
            break
        except:
            print "Oops!",sys.exc_info()[0],"occured."
            print "Please try again."
            print
    print "The reciprocal of",x,"is",r

inverse_number1()


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
The first argument func is the name of a function and
the second a sequence (e.g. a list) seq. map() applies
the function func to all the elements of the sequence seq.
It returns a new list with the elements changed by func
Can you write Python code for it?

def fahrenheit(T):
    return ((float(9)/5)*T + 32)
def celsius(T):
    return (float(5)/9)*(T-32)
temp = (36.5, 37, 37.5,39)

F = map(fahrenheit, temp)
print F
C = map(celsius, F)
print C

# map() can be applied to more than one list.
# The lists have to have the same length.
a = [1,2,3,4]
b = [17,12,11,10]
c = [-1,-4,5,9]
map(lambda x,y:x+y, a,b)

map(lambda x,y,z:x+y+z, a,b,c)

map(lambda x,y,z:x+y-z, a,b,c)


I am working on the file "Day_01/01_Advanced_Python/02_LambdaFunction.ipynb".
The first task is:
Use list comprehension and lambda/map function to define stuff.
Filter Function
The function filter(func, iterableType) offers an elegant way to
filter out all the elements of any iterable type (list, tuple, string, etc.), for which the function func returns True.
Can you write Python code for it?

fib = [0,1,1,2,3,5,8,13,21,34,55]
result = filter(lambda x: x % 2, fib)
print result

result = filter(lambda x: x % 2 == 0, fib)
print result


I am working on the file "Day_01/01_Advanced_Python/03_LambdaFunction-Solutions.ipynb".
The first task is:
<u>Problem 2</u>
Use the filter function to remove all the vowels from the sentence
Can you write Python code for it?

sentence = "It's a myth that there are no words in English without vowels."
vowels = 'aeiou'

result = filter(lambda x: x not in vowels, sentence)
print result


I am working on the file "Day_01/01_Pandas/1. Introduction to Pandas.ipynb".
The first task is:
$$corr(x,y) = \frac{cov(x,y)}{(n-1)s_x s_y} = \frac{\sum_i (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_i (x_i - \bar{x})^2 \sum_i (y_i - \bar{y})^2}}$$
Can you write Python code for it?

baseball.hr.corr(baseball.X2b)

baseball.ab.corr(baseball.h)

baseball.corr()


I am working on the file "Day_01/01_Pandas/1. Introduction to Pandas.ipynb".
The first task is:
A useful summarization that gives a quick snapshot of multiple statistics for a Series or DataFrame is describe:
Can you write Python code for it?