I am working on the file "Assignment2/ConvolutionalNetworks.ipynb".
The first task is:
Fast layers
Making convolution and pooling layers fast can be challenging. To spare you the pain, we've provided fast implementations of the forward and backward passes for convolution and pooling layers in the file cs231n/fast_layers.py.
The fast convolution implementation depends on a Cython extension; to compile it you need to run the following from the cs231n directory:
bash
python setup.py build_ext --inplace
The API for the fast versions of the convolution and pooling layers is exactly the same as the naive versions that you implemented above: the forward pass receives data, weights, and parameters and produces outputs and a cache object; the backward pass recieves upstream derivatives and the cache object and produces gradients with respect to the data and weights.
NOTE: The fast implementation for pooling will only perform optimally if the pooling regions are non-overlapping and tile the input. If these conditions are not met then the fast pooling implementation will not be much faster than the naive implementation.
You can compare the performance of the naive and fast versions of these layers by running the following:
Can you write Python code for it?

from cs231n.fast_layers import conv_forward_fast, conv_backward_fast
from time import time

x = np.random.randn(100, 3, 31, 31)
w = np.random.randn(25, 3, 3, 3)
b = np.random.randn(25,)
dout = np.random.randn(100, 25, 16, 16)
conv_param = {'stride': 2, 'pad': 1}

t0 = time()
out_naive, cache_naive = conv_forward_naive(x, w, b, conv_param)
t1 = time()
out_fast, cache_fast = conv_forward_fast(x, w, b, conv_param)
t2 = time()

print 'Testing conv_forward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'Fast: %fs' % (t2 - t1)
print 'Speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'Difference: ', rel_error(out_naive, out_fast)

t0 = time()
dx_naive, dw_naive, db_naive = conv_backward_naive(dout, cache_naive)
t1 = time()
dx_fast, dw_fast, db_fast = conv_backward_fast(dout, cache_fast)
t2 = time()

print '\nTesting conv_backward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'Fast: %fs' % (t2 - t1)
print 'Speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'dx difference: ', rel_error(dx_naive, dx_fast)
print 'dw difference: ', rel_error(dw_naive, dw_fast)
print 'db difference: ', rel_error(db_naive, db_fast)

from cs231n.fast_layers import max_pool_forward_fast, max_pool_backward_fast

x = np.random.randn(100, 3, 32, 32)
dout = np.random.randn(100, 3, 16, 16)
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

t0 = time()
out_naive, cache_naive = max_pool_forward_naive(x, pool_param)
t1 = time()
out_fast, cache_fast = max_pool_forward_fast(x, pool_param)
t2 = time()

print 'Testing pool_forward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'fast: %fs' % (t2 - t1)
print 'speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'difference: ', rel_error(out_naive, out_fast)

t0 = time()
dx_naive = max_pool_backward_naive(dout, cache_naive)
t1 = time()
dx_fast = max_pool_backward_fast(dout, cache_fast)
t2 = time()

print '\nTesting pool_backward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'dx difference: ', rel_error(dx_naive, dx_fast)


I am working on the file "Assignment2/ConvolutionalNetworks.ipynb".
The first task is:
Gradient check
After the loss looks reasonable, use numeric gradient checking to make sure that your backward pass is correct. When you use numeric gradient checking you should use a small amount of artifical data and a small number of neurons at each layer.
Can you write Python code for it?

num_inputs = 2
input_dim = (3, 16, 16)
reg = 0.0
num_classes = 10
X = np.random.randn(num_inputs, *input_dim)
y = np.random.randint(num_classes, size=num_inputs)

model = ThreeLayerConvNet(num_filters=3, filter_size=3,
                          input_dim=input_dim, hidden_dim=7,
                          dtype=np.float64)
loss, grads = model.loss(X, y)
for param_name in sorted(grads):
    f = lambda _: model.loss(X, y)[0]
    param_grad_num = eval_numerical_gradient(f, model.params[param_name], verbose=False, h=1e-6)
    e = rel_error(param_grad_num, grads[param_name])
    print '%s max relative error: %e' % (param_name, rel_error(param_grad_num, grads[param_name]))


I am working on the file "Assignment2/ConvolutionalNetworks.ipynb".
The first task is:
Max pooling: Naive backward
Implement the backward pass for the max-pooling operation in the function max_pool_backward_naive in the file cs231n/layers.py. You don't need to worry about computational efficiency.
Check your implementation with numeric gradient checking by running the following:
Can you write Python code for it?

x = np.random.randn(3, 2, 8, 8)
dout = np.random.randn(3, 2, 4, 4)
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

dx_num = eval_numerical_gradient_array(lambda x: max_pool_forward_naive(x, pool_param)[0], x, dout)

out, cache = max_pool_forward_naive(x, pool_param)
dx = max_pool_backward_naive(dout, cache)

# Your error should be around 1e-12
print 'Testing max_pool_backward_naive function:'
print 'dx error: ', rel_error(dx, dx_num)


I am working on the file "Assignment2/ConvolutionalNetworks.ipynb".
The first task is:
Max pooling: Naive forward
Implement the forward pass for the max-pooling operation in the function max_pool_forward_naive in the file cs231n/layers.py. Again, don't worry too much about computational efficiency.
Check your implementation by running the following:
Can you write Python code for it?


I am working on the file "Assignment2/ConvolutionalNetworks.ipynb".
The first task is:
Overfit small data
A nice trick is to train your model with just a few training samples. You should be able to overfit small datasets, which will result in very high training accuracy and comparatively low validation accuracy.
Can you write Python code for it?

num_train = 100
small_data = {
  'X_train': data['X_train'][:num_train],
  'y_train': data['y_train'][:num_train],
  'X_val': data['X_val'],
  'y_val': data['y_val'],
}

model = ThreeLayerConvNet(weight_scale=1e-2)

solver = Solver(model, small_data,
                num_epochs=10, batch_size=50,
                update_rule='adam',
                optim_config={
                  'learning_rate': 1e-3,
                },
                verbose=True, print_every=1)
solver.train()


I am working on the file "Assignment2/ConvolutionalNetworks.ipynb".
The first task is:
Spatial batch normalization: backward
In the file cs231n/layers.py, implement the backward pass for spatial batch normalization in the function spatial_batchnorm_backward. Run the following to check your implementation using a numeric gradient check:
Can you write Python code for it?

N, C, H, W = 2, 3, 4, 5
x = 5 * np.random.randn(N, C, H, W) + 12
gamma = np.random.randn(C)
beta = np.random.randn(C)
dout = np.random.randn(N, C, H, W)

bn_param = {'mode': 'train'}
fx = lambda x: spatial_batchnorm_forward(x, gamma, beta, bn_param)[0]
fg = lambda a: spatial_batchnorm_forward(x, gamma, beta, bn_param)[0]
fb = lambda b: spatial_batchnorm_forward(x, gamma, beta, bn_param)[0]

dx_num = eval_numerical_gradient_array(fx, x, dout)
da_num = eval_numerical_gradient_array(fg, gamma, dout)
db_num = eval_numerical_gradient_array(fb, beta, dout)

_, cache = spatial_batchnorm_forward(x, gamma, beta, bn_param)
dx, dgamma, dbeta = spatial_batchnorm_backward(dout, cache)
print 'dx error: ', rel_error(dx_num, dx)
print 'dgamma error: ', rel_error(da_num, dgamma)
print 'dbeta error: ', rel_error(db_num, dbeta)


I am working on the file "Assignment2/ConvolutionalNetworks.ipynb".
The first task is:
Three-layer ConvNet
Now that you have implemented all the necessary layers, we can put them together into a simple convolutional network.
Open the file cs231n/cnn.py and complete the implementation of the ThreeLayerConvNet class. Run the following cells to help you debug:
Sanity check loss
After you build a new network, one of the first things you should do is sanity check the loss. When we use the softmax loss, we expect the loss for random weights (and no regularization) to be about log(C) for C classes. When we add regularization this should go up.
Can you write Python code for it?

model = ThreeLayerConvNet()

N = 50
X = np.random.randn(N, 3, 32, 32)
y = np.random.randint(10, size=N)

loss, grads = model.loss(X, y)
print 'Initial loss (no regularization): ', loss, np.log(10)

model.reg = 0.5
loss, grads = model.loss(X, y)
print 'Initial loss (with regularization): ', loss, np.log(10)


I am working on the file "Assignment2/ConvolutionalNetworks.ipynb".
The first task is:
Visualize Filters
You can visualize the first-layer convolutional filters from the trained network by running the following:
Can you write Python code for it?

from cs231n.vis_utils import visualize_grid

grid = visualize_grid(model.params['W1'].transpose(0, 2, 3, 1))
plt.imshow(grid.astype('uint8'))
plt.axis('off')
plt.gcf().set_size_inches(5, 5)
plt.show()

print 'Final accuracy on test data using adadelta: %f' % solver.check_accuracy(data['X_test'], data['y_test'])

plt.subplot(2, 1, 1)
plt.plot(solver.loss_history, '-')
plt.xlabel('iteration')
plt.ylabel('loss')

plt.subplot(2, 1, 2)
plt.plot(solver.train_acc_history, '-')
plt.plot(solver.val_acc_history, '-')
plt.legend(['train', 'val'], loc='upper left')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.show()


I am working on the file "Assignment2/Dropout.ipynb".
The first task is:
Fully-connected nets with Dropout
In the file cs231n/classifiers/fc_net.py, modify your implementation to use dropout. Specificially, if the constructor the the net receives a nonzero value for the dropout parameter, then the net should add dropout immediately after every ReLU nonlinearity. After doing so, run the following to numerically gradient-check your implementation.
Can you write Python code for it?

N, D, H1, H2, C = 2, 15, 20, 30, 10
X = np.random.randn(N, D)
y = np.random.randint(C, size=(N,))

for dropout in [0, 0.25, 0.5]:
  print 'Running check with dropout = ', dropout
  model = FullyConnectedNet([H1, H2], input_dim=D, num_classes=C,
                            weight_scale=5e-2, dtype=np.float64,
                            dropout=dropout, seed=123)

  loss, grads = model.loss(X, y)
  print 'Initial loss: ', loss

  for name in sorted(grads):
    f = lambda _: model.loss(X, y)[0]
    grad_num = eval_numerical_gradient(f, model.params[name], verbose=False, h=1e-5)
    print '%s relative error: %.2e' % (name, rel_error(grad_num, grads[name]))
  print


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
"Sandwich" layers
There are some common patterns of layers that are frequently used in neural nets. For example, affine layers are frequently followed by a ReLU nonlinearity. To make these common patterns easy, we define several convenience layers in the file cs231n/layer_utils.py.
For now take a look at the affine_relu_forward and affine_relu_backward functions, and run the following to numerically gradient check the backward pass:
Can you write Python code for it?

from cs231n.layer_utils import affine_relu_forward, affine_relu_backward

x = np.random.randn(2, 3, 4)
w = np.random.randn(12, 10)
b = np.random.randn(10)
dout = np.random.randn(2, 10)

out, cache = affine_relu_forward(x, w, b)
dx, dw, db = affine_relu_backward(dout, cache)

dx_num = eval_numerical_gradient_array(
    lambda x: affine_relu_forward(x, w, b)[0], x, dout)
dw_num = eval_numerical_gradient_array(
    lambda w: affine_relu_forward(x, w, b)[0], w, dout)
db_num = eval_numerical_gradient_array(
    lambda b: affine_relu_forward(x, w, b)[0], b, dout)

print 'Testing affine_relu_forward:'
print 'dx error: ', rel_error(dx_num, dx)
print 'dw error: ', rel_error(dw_num, dw)
print 'db error: ', rel_error(db_num, db)


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
Affine layer: foward
Open the file cs231n/layers.py and implement the affine_forward function.
Once you are done you can test your implementaion by running the following:
Can you write Python code for it?

# Test the affine_forward function

num_inputs = 2
input_shape = (4, 5, 6)
output_dim = 3

input_size = num_inputs * np.prod(input_shape)
weight_size = output_dim * np.prod(input_shape)

x = np.linspace(-0.1, 0.5, num=input_size).reshape(num_inputs, *input_shape)
w = np.linspace(
    -0.2, 0.3, num=weight_size).reshape(np.prod(input_shape), output_dim)
b = np.linspace(-0.3, 0.1, num=output_dim)

out, _ = affine_forward(x, w, b)
correct_out = np.array([[1.49834967, 1.70660132, 1.91485297],
                        [3.25553199, 3.5141327, 3.77273342]])

# Compare your output with ours. The error should be around 1e-9.
print 'Testing affine_forward function:'
print 'difference: ', rel_error(out, correct_out)


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
As another sanity check, make sure you can overfit a small dataset of 50 images. First we will try a three-layer network with 100 units in each hidden layer. You will need to tweak the learning rate and initialization scale, but you should be able to overfit and achieve 100% training accuracy within 20 epochs.
Can you write Python code for it?

# TODO: Use a three-layer Net to overfit 50 training examples.

num_train = 50
small_data = {
    'X_train': data['X_train'][:num_train],
    'y_train': data['y_train'][:num_train],
    'X_val': data['X_val'],
    'y_val': data['y_val'],
}

weight_scale = 1
learning_rate = 1e-5
model = FullyConnectedNet(
    [100, 100], weight_scale=weight_scale, dtype=np.float64)
solver = Solver(
    model,
    small_data,
    print_every=10,
    num_epochs=20,
    batch_size=1,
    update_rule='sgd',
    optim_config={'learning_rate': learning_rate, })
solver.train()

plt.plot(solver.loss_history, 'o')
plt.title('Training loss history')
plt.xlabel('Iteration')
plt.ylabel('Training loss')
plt.show()


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
Multilayer network
Next you will implement a fully-connected network with an arbitrary number of hidden layers.
Read through the FullyConnectedNet class in the file cs231n/classifiers/fc_net.py.
Implement the initialization, the forward pass, and the backward pass. For the moment don't worry about implementing dropout or batch normalization; we will add those features soon.
Initial loss and gradient check
As a sanity check, run the following to check the initial loss and to gradient check the network both with and without regularization. Do the initial losses seem reasonable?
For gradient checking, you should expect to see errors around 1e-6 or less.
Can you write Python code for it?

N, D, H1, H2, C = 2, 15, 20, 30, 10
X = np.random.randn(N, D)
y = np.random.randint(C, size=(N, ))

for reg in [0, 3.14]:
    print 'Running check with reg = ', reg
    model = FullyConnectedNet(
        [H1, H2],
        input_dim=D,
        num_classes=C,
        reg=reg,
        weight_scale=5e-2,
        dtype=np.float64)

    loss, grads = model.loss(X, y)
    print 'Initial loss: ', loss

    for name in sorted(grads):
        f = lambda _: model.loss(X, y)[0]
        grad_num = eval_numerical_gradient(
            f, model.params[name], verbose=False, h=1e-5)
        print '%s relative error: %.2e' % (name, rel_error(grad_num,
                                                           grads[name]))


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
Once you have debugged your RMSProp and Adam implementations, run the following to train a pair of deep networks using these new update rules:
Can you write Python code for it?

learning_rates = {'rmsprop': 1e-4, 'adam': 1e-3}
for update_rule in ['adam', 'rmsprop']:
    print 'running with ', update_rule
    model = FullyConnectedNet([100, 100, 100, 100, 100], weight_scale=5e-2)

    solver = Solver(
        model,
        small_data,
        num_epochs=5,
        batch_size=100,
        update_rule=update_rule,
        optim_config={'learning_rate': learning_rates[update_rule]},
        verbose=True)
    solvers[update_rule] = solver
    solver.train()
    print

plt.subplot(3, 1, 1)
plt.title('Training loss')
plt.xlabel('Iteration')

plt.subplot(3, 1, 2)
plt.title('Training accuracy')
plt.xlabel('Epoch')

plt.subplot(3, 1, 3)
plt.title('Validation accuracy')
plt.xlabel('Epoch')

for update_rule, solver in solvers.iteritems():
    plt.subplot(3, 1, 1)
    plt.plot(solver.loss_history, 'o', label=update_rule)

    plt.subplot(3, 1, 2)
    plt.plot(solver.train_acc_history, '-o', label=update_rule)

    plt.subplot(3, 1, 3)
    plt.plot(solver.val_acc_history, '-o', label=update_rule)

for i in [1, 2, 3]:
    plt.subplot(3, 1, i)
    plt.legend(loc='upper center', ncol=4)
plt.gcf().set_size_inches(15, 15)
plt.show()


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
RMSProp and Adam
RMSProp [1] and Adam [2] are update rules that set per-parameter learning rates by using a running average of the second moments of gradients.
In the file cs231n/optim.py, implement the RMSProp update rule in the rmsprop function and implement the Adam update rule in the adam function, and check your implementations using the tests below.
[1] Tijmen Tieleman and Geoffrey Hinton. "Lecture 6.5-rmsprop: Divide the gradient by a running average of its recent magnitude." COURSERA: Neural Networks for Machine Learning 4 (2012).
[2] Diederik Kingma and Jimmy Ba, "Adam: A Method for Stochastic Optimization", ICLR 2015.
Can you write Python code for it?


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
ReLU layer: forward
Implement the forward pass for the ReLU activation function in the relu_forward function and test your implementation using the following:
Can you write Python code for it?

# Test the relu_forward function

x = np.linspace(-0.5, 0.5, num=12).reshape(3, 4)

out, _ = relu_forward(x)
correct_out = np.array([[
    0.,
    0.,
    0.,
    0.,
], [
    0.,
    0.,
    0.04545455,
    0.13636364,
], [
    0.22727273,
    0.31818182,
    0.40909091,
    0.5,
]])

# Compare your output with ours. The error should be around 1e-8
print 'Testing relu_forward function:'
print 'difference: ', rel_error(out, correct_out)


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
Solver
In the previous assignment, the logic for training models was coupled to the models themselves. Following a more modular design, for this assignment we have split the logic for training models into a separate class.
Open the file cs231n/solver.py and read through it to familiarize yourself with the API. After doing so, use a Solver instance to train a TwoLayerNet that achieves at least 50% accuracy on the validation set.
Can you write Python code for it?

model_data = {
    'X_train': data['X_train'],
    'y_train': data['y_train'],
    'X_val': data['X_val'],
    'y_val': data['y_val']
}

C = np.max(model_data['y_train']) + 1
N = np.prod(model_data['X_train'].shape[1:])
H = 100
std = 1e-3

model = TwoLayerNet(input_dim=N, hidden_dim=H, num_classes=C, weight_scale=std)
solver = Solver(
    model,
    model_data,
    update_rule='sgd',
    optim_config={'learning_rate': 1e-3, },
    lr_decay=0.95,
    num_epochs=30,
    batch_size=100,
    print_every=200)

solver.train()

# Run this cell to visualize training loss and train / val accuracy

plt.subplot(2, 1, 1)
plt.title('Training loss')
plt.plot(solver.loss_history, 'o')
plt.xlabel('Iteration')

plt.subplot(2, 1, 2)
plt.title('Accuracy')
plt.plot(solver.train_acc_history, '-o', label='train')
plt.plot(solver.val_acc_history, '-o', label='val')
plt.plot([0.5] * len(solver.val_acc_history), 'k--')
plt.xlabel('Epoch')
plt.legend(loc='lower right')
plt.gcf().set_size_inches(15, 12)
plt.show()


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
Test you model
Run your best model on the validation and test sets. You should achieve above 50% accuracy on the validation set.
Can you write Python code for it?

y_test_pred = np.argmax(best_model.loss(X_test), axis=1)
y_val_pred = np.argmax(best_model.loss(X_val), axis=1)
print 'Validation set accuracy: ', (y_val_pred == y_val).mean()
print 'Test set accuracy: ', (y_test_pred == y_test).mean()


I am working on the file "Assignment2/FullyConnectedNets.ipynb".
The first task is:
Update rules
So far we have used vanilla stochastic gradient descent (SGD) as our update rule. More sophisticated update rules can make it easier to train deep networks. We will implement a few of the most commonly used update rules and compare them to vanilla SGD.
SGD+Momentum
Stochastic gradient descent with momentum is a widely used update rule that tends to make deep networks converge faster than vanilla stochstic gradient descent.
Open the file cs231n/optim.py and read the documentation at the top of the file to make sure you understand the API. Implement the SGD+momentum update rule in the function sgd_momentum and run the following to check your implementation. You should see errors less than 1e-8.
Can you write Python code for it?


I am working on the file "Assignment2MonoLake.ipynb".
The first task is:
Graph the results
Now you should make two plots of the variables whose time series you have saved. We set an example with the first one.
Can you write Python code for it?

#Make a plot for lake elevation
plt.plot(year, elev, '-')
plt.xlabel('Year')
plt.ylabel('Elevation, m')
plt.title('Historical and projected lake level')

#Make a plot for salinity


I am working on the file "Assignment2MonoLake.ipynb".
The first task is:
Model calculations
Now we get to the heart of the model. The heart of the model consists of a for-loop, which calculates all of the variables that we are interested in, on a year-by-year basis. Each new calculation of volume takes the volume solved for in the previous year and adds to it the difference between inflows and outflows, multiplied by the one-year time step. (Whenever I am coding, I find it helpful to keep track of the units in the comments.)
Before we get to this for loop, there are a few other things that we need to do to set it up. For example, we need to tell it that the "old" volume we should use for the first calculation is the initial condition, 'V_init.' Secondly, we need to establish that we want to save the whole history of lake levels and salinities (in other words, after we run this code, we don't just want the LAST timestep; we want the whole time series so that we can plot it!). This is called 'initialization.'
Initilization
Can you write Python code for it?

V_old = V_init #The first value of V_old that we'll use in the calculations is V_init
max_lake_elev = 0 #Maximum lake elevation attained through the whole modeled time series.At the
#outset, we don't know, so we set it equal to zero.
elev = np.zeros(len(year)) #This initializes our array of lake elevations that we want to save with
#as many entries as there are years.
salinity = np.zeros(len(year)) #Just another array of zeros, where we will save salinity.


I am working on the file "Assignment2MonoLake.ipynb".
The first task is:
Now let's put the real calculations into the for loop. I will get it started with the calculation of lake depth, area and a few other quantities. You should complete the for loop with the other important calculations that are columns on the spreadsheet model. I have created 'pseudocode' for you (in other words, outlining what you need to do in the comments). Your task is to complete the code and produce plots of lake level and salinity over time. Remember that you can find all of the original mathematical formulas in the Mono Lake water balance spreadsheet.
Note the syntax of the "if" statement. You will need to use similar syntax when writing a formula for diversions (the hardest part of this assignment!). Diversions, precipitation, and ET all are estimated differently before and after 2011. You can incorporate the calculation of ET into the if statement I have started below. Note that you will need multiple nested if statements for the diversions calculation, and you that you need to solve for diversions AFTER solving for runoff (so that you know how much water is available to take in the sustainable period).
Another important note: It is rare that you will write code correctly on the first time. Fortunately, Python lets you know where the error is with an arrow and gives you an inkling of what the error is. When I am debugging a code, I find it useful to insert the print() command into the code so that I can make sure my variables look as I expect them to. Note that when you re-execute the for loop after running it once, you will need to rerun the initialization cell, otherwise you will get incorrect values!
Can you write Python code for it?

for i in range(len(year)):
    depth = (V_old/9496.7)**0.3333 #meters. Note here the syntax for the exponent, **
    elev[i] = (depth+1866.8)*3.28 #meters.
    max_lake_elev = max(elev) #Figure out the maximum lake elevation we've seen
    area = np.pi*(depth*95.24)**2 #meters squared. The function that calculates pi
    #comes from the numpy package (which we imported as np), so we use np to call it.

    #Solve for precipitation rate in the Mono Lake basin and ET (you do the ET part)
    if year[i]<=2011: #If the current year is less than or equal to 2011...
        rain_mono = nearby_rain[i]*0.37 #Scale the precipitation measured at a nearby gauge

    else: #Otherwise, if this is the "future..." (well, if it's beyond 2011)
        rain_mono = input_precip*0.37 #Use the pre-specified input precipitation, scaled

    #Solve for the precipitation flux.

    #Solve for the groundwater flux.

    #Solve for runoff.

    #Solve for evapotranspiration flux in m^3/yr

    #Now solve for diversions

    #Solve for the 'net' inflow (see formula in column Q of spreadsheet)

    #Solve for salinity

    #Now solve for the new lake volume!
    V_new = V_old + (net_inflow-ET_flux)*1 #m^3. The 1 isn't really necessary, but it is included for completeness. It represents 1 year.
    V_old = V_new #Not to sound philosophical, but all that is new shall become old.


I am working on the file "Assignment2MonoLake.ipynb".
The first task is:
We imported the whole model (i.e., the whole spreadsheet) out of convenience, but we're going to pretend we don't already know the answer, and that all we know is the starting lake volume, the record of diversions, and the precipitation at nearby stations, for the 1941-2011 period. For convenience, we will define separate arrays for each of these variables.
Defining arrays of input variables
One of the nice things about using data frames is that it is easy to refer to specific columns. We just do so by name!
Can you write Python code for it?

#Define an array for year
year = df['Year']
#Take a look at how this new variable, *year*, is formatted. Notice that the index of the first
#element in the array is 0. This is different from Matlab, which indexes arrays beginning with
#1!
year


I am working on the file "Assignment3/ImageGeneration.ipynb".
The first task is:
Deep feature reconstruction
Reconstructing images using features from deeper layers of the network tends to give interesting results. In the cell below, try to reconstruct the best image you can by inverting the features after 7 layers of convolutions. You will need to play with the hyperparameters to try and get a good result.
HINT: If you read the paper by Mahendran and Vedaldi, you'll see that reconstructions from deep features tend not to look much like the original image, so you shouldn't expect the results to look like the reconstruction above. You should be able to get an image that shows some discernable structure within 1000 iterations.
Can you write Python code for it?

filename = 'kitten.jpg'
layer = 6 # layers start from 0 so these are features after 7 convolutions
img = imresize(imread(filename), (64, 64))

plt.imshow(img)
plt.gcf().set_size_inches(3, 3)
plt.title('Original image')
plt.axis('off')
plt.show()

# Preprocess the image before passing it to the network:
# subtract the mean, add a dimension, etc
img_pre = preprocess_image(img, data['mean_image'])

# Extract features from the image
feats, _ = model.forward(img_pre, end=layer)

# Invert the features
# You will need to play with these parameters.
kwargs = {
  'num_iterations': 1000,
  'learning_rate': 0,
  'l2_reg': 0,
  'show_every': 100,
  'blur_every': 0,
}
X = invert_features(feats, layer, model, **kwargs)


I am working on the file "Assignment3/ImageGeneration.ipynb".
The first task is:
Feature Inversion
In an attempt to understand the types of features that convolutional networks learn to recognize, a recent paper [1] attempts to reconstruct an image from its feature representation. We can easily implement this idea using image gradients from the pretrained network.
Concretely, given a image $I$, let $\phi_\ell(I)$ be the activations at layer $\ell$ of the convolutional network $\phi$. We wish to find an image $I^*$ with a similar feature representation as $I$ at layer $\ell$ of the network $\phi$ by solving the optimization problem
$$
I^* = \arg\min_{I'} \|\phi_\ell(I) - \phi_\ell(I')\|_2^2 + R(I')
$$
where $\|\cdot\|_2^2$ is the squared Euclidean norm. As above, $R$ is a (possibly implicit) regularizer. We can solve this optimization problem using gradient descent, computing gradients with respect to the generated image. We will use (explicit) L2 regularization of the form
$$
R(I') + \lambda \|I'\|_2^2
$$
together with implicit regularization by periodically blurring the image, as recommended by [2].
Implement this method in the function below.
[1] Aravindh Mahendran, Andrea Vedaldi, "Understanding Deep Image Representations by Inverting them", CVPR 2015
[2] Yosinski et al, "Understanding Neural Networks Through Deep Visualization", ICML 2015 Deep Learning Workshop
Can you write Python code for it?

def invert_features(target_feats, layer, model, **kwargs):
  """
  Perform feature inversion in the style of Mahendran and Vedaldi 2015, using
  L2 regularization and periodic blurring.

  Inputs:
  - target_feats: Image features of the target image, of shape (1, C, H, W);
    we will try to generate an image that matches these features
  - layer: The index of the layer from which the features were extracted
  - model: A PretrainedCNN that was used to extract features

  Keyword arguments:
  - learning_rate: The learning rate to use for gradient descent
  - num_iterations: The number of iterations to use for gradient descent
  - l2_reg: The strength of L2 regularization to use; this is lambda in the
    equation above.
  - blur_every: How often to blur the image as implicit regularization; set
    to 0 to disable blurring.
  - show_every: How often to show the generated image; set to 0 to disable
    showing intermediate reuslts.

  Returns:
  - X: Generated image of shape (1, 3, 64, 64) that matches the target features.
  """
  learning_rate = kwargs.pop('learning_rate', 10000)
  num_iterations = kwargs.pop('num_iterations', 500)
  l2_reg = kwargs.pop('l2_reg', 1e-7)
  blur_every = kwargs.pop('blur_every', 1)
  show_every = kwargs.pop('show_every', 50)

  X = np.random.randn(1, 3, 64, 64)
  for t in xrange(num_iterations):
    ############################################################################
    # TODO: Compute the image gradient dX of the reconstruction loss with      #
    # respect to the image. You should include L2 regularization penalizing    #
    # large pixel values in the generated image using the l2_reg parameter;    #
    # then update the generated image using the learning_rate from above.      #
    ############################################################################
    pass
    ############################################################################
    #                             END OF YOUR CODE                             #
   ...


I am working on the file "Assignment3/ImageGeneration.ipynb".
The first task is:
Generate some images!
Try and generate a cool-looking DeepDeam image using the pretrained network. You can try using different layers, or starting from different images. You can reduce the image size if it runs too slowly on your machine, or increase the image size if you are feeling ambitious.
Can you write Python code for it?

def read_image(filename, max_size):
  """
  Read an image from disk and resize it so its larger side is max_size
  """
  img = imread(filename)
  H, W, _ = img.shape
  if H >= W:
    img = imresize(img, (max_size, int(W * float(max_size) / H)))
  elif H < W:
    img = imresize(img, (int(H * float(max_size) / W), max_size))
  return img

filename = 'kitten.jpg'
max_size = 256
img = read_image(filename, max_size)
plt.imshow(img)
plt.axis('off')

# Preprocess the image by converting to float, transposing,
# and performing mean subtraction.
img_pre = preprocess_image(img, data['mean_image'], mean='pixel')

out = deepdream(img_pre, 7, model, learning_rate=2000)


I am working on the file "Assignment3/ImageGeneration.ipynb".
The first task is:
Shallow feature reconstruction
After implementing the feature inversion above, run the following cell to try and reconstruct features from the fourth convolutional layer of the pretrained model. You should be able to reconstruct the features using the provided optimization parameters.
Can you write Python code for it?

filename = 'kitten.jpg'
layer = 3 # layers start from 0 so these are features after 4 convolutions
img = imresize(imread(filename), (64, 64))

plt.imshow(img)
plt.gcf().set_size_inches(3, 3)
plt.title('Original image')
plt.axis('off')
plt.show()

# Preprocess the image before passing it to the network:
# subtract the mean, add a dimension, etc
img_pre = preprocess_image(img, data['mean_image'])

# Extract features from the image
feats, _ = model.forward(img_pre, end=layer)

# Invert the features
kwargs = {
  'num_iterations': 400,
  'learning_rate': 5000,
  'l2_reg': 1e-8,
  'show_every': 100,
  'blur_every': 10,
}
X = invert_features(feats, layer, model, **kwargs)


I am working on the file "Assignment3/ImageGeneration.ipynb".
The first task is:
TinyImageNet and pretrained model
As in the previous notebook, load the TinyImageNet dataset and the pretrained model.
Can you write Python code for it?

data = load_tiny_imagenet('cs231n/datasets/tiny-imagenet-100-A', subtract_mean=True)
model = PretrainedCNN(h5_file='cs231n/datasets/pretrained_model.h5')


I am working on the file "Assignment3/ImageGradients.ipynb".
The first task is:
Fooling Images
We can also use image gradients to generate "fooling images" as discussed in [2]. Given an image and a target class, we can perform gradient ascent over the image to maximize the target class, stopping when the network classifies the image as the target class. Implement the following function to generate fooling images.
[2] Szegedy et al, "Intriguing properties of neural networks", ICLR 2014
Can you write Python code for it?

def make_fooling_image(X, target_y, model):
    """
    Generate a fooling image that is close to X, but that the model classifies
    as target_y.

    Inputs:
    - X: Input image, of shape (1, 3, 64, 64)
    - target_y: An integer in the range [0, 100)
    - model: A PretrainedCNN

    Returns:
    - X_fooling: An image that is close to X, but that is classifed as target_y
      by the model.
    """

    N, _, _, _ = X.shape
    num_iter = 200
    lr_rate = 1e2
    X_fooling = np.copy(X)

    y = np.zeros((N), dtype=np.int32)
    y[:] = target_y

    for i in np.arange(num_iter):
        scores, cache = model.forward(X_fooling, mode='test')
        print scores[:, target_y]

        if np.argmax(scores, axis=1) == y:
            print 'Fooled yeah in %d iterations' % i
            break

        _, dscores = softmax_loss(scores, y)
        dX, _ = model.backward(dscores, cache)

        X_fooling -= lr_rate * dX

    return X_fooling


I am working on the file "Assignment3/ImageGradients.ipynb".
The first task is:
Once you have completed the implementation in the cell above, run the following to visualize some class saliency maps on the validation set of TinyImageNet-100-A.
Can you write Python code for it?

def show_saliency_maps(mask):
  mask = np.asarray(mask)
  X = data['X_val'][mask]
  y = data['y_val'][mask]

  saliency = compute_saliency_maps(X, y, model)

  for i in xrange(mask.size):
    plt.subplot(2, mask.size, i + 1)
    plt.imshow(deprocess_image(X[i], data['mean_image']))
    plt.axis('off')
    plt.title(data['class_names'][y[i]][0])
    plt.subplot(2, mask.size, mask.size + i + 1)
    plt.title(mask[i])
    plt.imshow(saliency[i])
    plt.axis('off')
  plt.gcf().set_size_inches(10, 4)
  plt.show()

# Show some random images
mask = np.random.randint(data['X_val'].shape[0], size=5)
show_saliency_maps(mask)

# These are some cherry-picked images that should give good results
show_saliency_maps([128, 3225, 2417, 1640, 4619])


I am working on the file "Assignment3/ImageGradients.ipynb".
The first task is:
Pretrained model
We have trained a deep CNN for you on the TinyImageNet-100-A dataset that we will use for image visualization. The model has 9 convolutional layers (with spatial batch normalization) and 1 fully-connected hidden layer (with batch normalization).
To get the model, run the script get_pretrained_model.sh from the cs231n/datasets directory. After doing so, run the following to load the model from disk.
Can you write Python code for it?


I am working on the file "Assignment3/ImageGradients.ipynb".
The first task is:
Pretrained model performance
Run the following to test the performance of the pretrained model on some random training and validation set images. You should see training accuracy around 90% and validation accuracy around 60%; this indicates a bit of overfitting, but it should work for our visualization experiments.
Can you write Python code for it?

batch_size = 100

# Test the model on training data
mask = np.random.randint(data['X_train'].shape[0], size=batch_size)
X, y = data['X_train'][mask], data['y_train'][mask]
y_pred = model.loss(X).argmax(axis=1)
print 'Training accuracy: ', (y_pred == y).mean()

# Test the model on validation data
mask = np.random.randint(data['X_val'].shape[0], size=batch_size)
X, y = data['X_val'][mask], data['y_val'][mask]
y_pred = model.loss(X).argmax(axis=1)
print 'Validation accuracy: ', (y_pred == y).mean()


I am working on the file "Assignment3/ImageGradients.ipynb".
The first task is:
Saliency Maps
Using this pretrained model, we will compute class saliency maps as described in Section 3.1 of [1].
As mentioned in Section 2 of the paper, you should compute the gradient of the image with respect to the unnormalized class score, not with respect to the normalized class probability.
You will need to use the forward and backward methods of the PretrainedCNN class to compute gradients with respect to the image. Open the file cs231n/classifiers/pretrained_cnn.py and read the documentation for these methods to make sure you know how they work. For example usage, you can see the loss method. Make sure to run the model in test mode when computing saliency maps.
[1] Karen Simonyan, Andrea Vedaldi, and Andrew Zisserman. "Deep Inside Convolutional Networks: Visualising
Image Classification Models and Saliency Maps", ICLR Workshop 2014.
Can you write Python code for it?

def compute_saliency_maps(X, y, model):
    """
    Compute a class saliency map using the model for images X and labels y.

    Input:
    - X: Input images, of shape (N, 3, H, W)
    - y: Labels for X, of shape (N,)
    - model: A PretrainedCNN that will be used to compute the saliency map.

    Returns:
    - saliency: An array of shape (N, H, W) giving the saliency maps for the input
      images.
    """
    N, _, H, W = X.shape

    scores, cache = model.forward(X, mode='test')

    dscores = np.zeros_like(scores)
    dscores[np.arange(N), y] = 1
    dX, _ = model.backward(dscores, cache)

    saliency = np.max(dX, axis=1)

    return saliency


I am working on the file "Assignment3/ImageGradients.ipynb".
The first task is:
TinyImageNet-100-A classes
Since ImageNet is based on the WordNet ontology, each class in ImageNet (and TinyImageNet) actually has several different names. For example "pop bottle" and "soda bottle" are both valid names for the same class. Run the following to see a list of all classes in TinyImageNet-100-A:
Can you write Python code for it?

for i, names in enumerate(data['class_names']):
  print i, ' '.join('"%s"' % name for name in names)


I am working on the file "Assignment3/ImageGradients.ipynb".
The first task is:
Visualize Examples
Run the following to visualize some example images from random classses in TinyImageNet-100-A. It selects classes and images randomly, so you can run it several times to see different images.
Can you write Python code for it?

# Visualize some examples of the training data
classes_to_show = 7
examples_per_class = 5

class_idxs = np.random.choice(len(data['class_names']), size=classes_to_show, replace=False)
for i, class_idx in enumerate(class_idxs):
  train_idxs, = np.nonzero(data['y_train'] == class_idx)
  train_idxs = np.random.choice(train_idxs, size=examples_per_class, replace=False)
  for j, train_idx in enumerate(train_idxs):
    img = deprocess_image(data['X_train'][train_idx], data['mean_image'])
    plt.subplot(examples_per_class, classes_to_show, 1 + i + classes_to_show * j)
    if j == 0:
      plt.title(data['class_names'][class_idx][0])
    plt.imshow(img)
    plt.gca().axis('off')

plt.show()


I am working on the file "Assignment6StreamflowOrovilleDam.ipynb".
The first task is:
5) Now use the Gumbel method to produce a plot of return period vs. peak discharge. What is the return period of this year's storm according to this method? [1 pt]
Can you write Python code for it?

#Code for Gumbel Probability Analysis goes here


I am working on the file "Assignment6StreamflowOrovilleDam.ipynb".
The first task is:
Importing data
Unfortunately, the USGS does not have any data from this year available from the Feather River or its tributaries. (For this river, it only makes data available after staff hydrologists have had a chance to review it.) I was, however, able to find recent data available from a station maintained by the CA Department of Water Resources (DWR) on this website. The station is the Feather River at Gridley, which is just downstream of Oroville. However, the period of record only goes back to 1984 (in contrast to some of the USGS stations with periods of record back to the early 1900s.) The DWR data is downloadable as a CSV, in which the columns represent data collected on the hour over the 24 hours of the day, and the rows represent days in the period of record. Let's import it and begin to work with it. First, make sure the data file is saved to your computer.
Can you write Python code for it?

# Use pd.read_csv() to read in the data and store in a DataFrame
fname = '/Users/lglarsen/Desktop/Laurel Google Drive/Terrestrial hydrology Spr2017/Assignments/Assignment 6/FeatherRiveratGridley.csv'
df = pd.read_csv(fname)
df.head()


I am working on the file "Assignment6StreamflowOrovilleDam.ipynb".
The first task is:
Note that discharge is in units of cfs, or cubic feet per second. You'll notice that missing data has been assigned the value -9998. Below we will correct the blanks to nan and then convert the discharge data to an array using the values function in the pandas package.
Can you write Python code for it?


I am working on the file "Assignment6StreamflowOrovilleDam.ipynb".
The first task is:
Now we will embark on a sophisticated approach for removing blank values.  We'll interpolate them using pandas' interpolate function, but only when half the day's data values are valid. Finally, we will add all of the hourly values to create a daily value. If there were more than 12 blanks in a day, the daily value will end up as an NaN.
So that you can see how this loop works, I suggest changing the first line to for i in range(2): and uncommenting the two print statements at the end of the code. The two questions below require you to do this.

Concept check: In the first row of the data table, how many NaNs do you end up with after the blank interpolation procedure? Why? [1/3 pt]

Concept check: What is the big difference in the daily sum between the first and second rows of the data table? Why? [1/3 pt]
Can you write Python code for it?

#First, initialize the daily discharge array
dailyQ = np.zeros(np.size(hoursbyday,0)) #This makes the daily discharge array have the same length
#as the number of rows in hoursbyday.

for i in range(np.size(hoursbyday,0)): #This means to iterate over the number of rows in hoursbyday
    dayvals = hoursbyday[i,:] #Grab the row of hourly values for each day you are iterating over.

    #Now we're going to pad this array with the last 12 values from the day before and the first 12 values from the day after.
    #This is in case we need to get rid of blanks at the beginning or end of the day.

    if i>0:
        dayvals = np.append(hoursbyday[i-1,12:23], dayvals)
    else: #If this is the first row, pad the values with blanks
        dayvals = np.append(np.ones(12)*-9998, dayvals)
    if i==np.size(hoursbyday,0)-1: #if this is the last line in the data file
        dayvals = np.append(dayvals, np.ones(12)*-9998)
    else: #If this is not the last row
        dayvals = np.append(dayvals, hoursbyday[i+1,0:11])

    #Now we interpolate the missing values.
    dayvals[dayvals<0]=np.nan #Convert blanks to NaN values
    pandas_dayvals = pd.Series(dayvals)#I'm converting back to a pandas object because pandas has a useful interpolate function

    #Linearly interpolate across the blanks, but only if there are no more than 12 blanks in a row
    pandas_dayvals = pandas_dayvals.interpolate(limit=12)

    #Last, we'll convert back to a numpy array and just grab the original set of data for that day.
    dayvals = pandas_dayvals[12:35].values
   # print(dayvals)

    #Finally, add all of the values to acquire a daily discharge.
    dailyQ[i] = np.sum(dayvals)
   # print(dailyQ[i])

np.size(hoursbyday,0)


I am working on the file "Assignment6StreamflowOrovilleDam.ipynb".
The first task is:
So now let's go through and find the maximimum value of discharge per year.
3) Concept check: What does the first line after the for loop below do? [1/3 pt]
Can you write Python code for it?

these_years = np.unique(year) #This creates an array of "unique" years in the 'year' array (i.e.,
#with no repeats)
#Initialize the maximum value per year:
maxQ = np.zeros(len(these_years)-1) #One peak discharge per year. Subtracted 1 because the last
#year is a row of NaNs.

for i in range(len(these_years)-1): #Loop over unique years
    these_Q = dailyQ[year==these_years[i]]
    maxQ[i] = max(these_Q[~np.isnan(these_Q)])

plt.plot(these_years[0:len(these_years)-1], maxQ)

plt.hist(maxQ,10, normed=True) #creates a 10-bin, normalized histogram.
plt.title('Annual peak discharge')
plt.xlabel('Discharge, cfs')
plt.ylabel('Probability density')


I am working on the file "Assignment6StreamflowOrovilleDam.ipynb".
The first task is:
So you can see that the recent high flow coming out of the Oroville Dam, while pretty darn high, was not the highest on record. Let's now look at the data another way...
Can you write Python code for it?

plt.hist(dailyQ[~np.isnan(dailyQ)],20, normed=True) #creates a 20-bin, normalized histogram.
plt.title('Daily discharge')
plt.xlabel('Discharge, cfs')
plt.ylabel('Probability density')


I am working on the file "Assignment_02.ipynb".
The first task is:
I have decided to go with graphical represantion for this problem to give an easier way to interpret the data provided by the function <i>exercise4</i>. In the function, I am incrementing the base value (1.0) by the amount given in the function call, by x times, which is also given in the function call.
Data is then saved into an array which can be used to represent the data in graphical form to point of any relations between the study points and values for eps.
From the graph we can come up with couple of conclusions:
- Value for eps is roughly between 2.15e-16 and 1.11e-16.
- The value for eps decreases gradually until it reaches it's lowest value.
- After reaching it's lowest value, eps starts to decrease from it's highest point
- As the amount of study point increases, the amount of study points required for each cycle is roughly doubled.
If we take a look at the Wikipedia arcticle "Machine Epsilon" (https://en.wikipedia.org/wiki/Machine_epsilon), we can find some relation to the numbers we've seen in our graph. At the table, "Values for standard hardware floating point arithmetics", we can see the machine epsilon value for double precision being either 1.11e-16 or 2.22e-16, depending on which Professor you ask.
So based on this, I would say the code example above gives us an approximation that epsilon is somewhere in between 1.11e-16 and 2.22e-16, but is depended on the actual value which is being used to calculate the value. The search algorithm used in the process in "Linear Search"
Problem 5
The derivative of $f(x) = 1/(1 - 3x^2)$ is given by:
$$f'(x) = \frac{6x}{(1-3x^2)^2}$$
Do you expect to have difficulties evaluating this function at x = 0.577? How does the value of the derivative change when you increase the significant figures in your calculations. Try using from 3-digit to 16- digit arithmetics. Start with the following code:
Can you write Python code for it?

from decimal import *

def exercise5(x, prec):
    getcontext().prec = prec

    x = Decimal(x)

    x = 6*x / ((1-3*x**2)**2)

    return x

i = 3
while i <= 16:
    print("Value with {} decimals is {}".format(i, exercise5(0.577, i)))
    i += 1


I am working on the file "Assignment_02.ipynb".
The first task is:
In this particular case, the smallest number to differ from 0.0 would be either 5e-324 or -5e-324. I've decided to break the while-loop in case x/2.0 is 0.0. If the value becomes 0.0, I would not be able to multiply it to receive the previous value the same way I did in the previous exericse.
There are two function calls with negative and positive starting values to demonstrate the difference being the same, despite of the starting value.
Problem 4
How does the (epsilon) change, when you increase or decrease the study point (e.g. 1.0 or 0.0 in the previous exercise)? What is the relationship of the study point and epsilon? Use values that differ several decades
Can you write Python code for it?

%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
import pylab
from matplotlib.pyplot import *
from numpy import *

# Create an array for the results to be saved
data = []

def exercise4(increment, count):
    y = 1.0

    i = 1
    while i <= count:
        eps = y
        while eps + 1.0 > 1.0:
            eps = eps/2.0

        eps = 2.0 * eps
        #print("Round {}, y = {}: Value for epsilon is {}".format(i, y, eps))
        i += 1
        y += increment

        data.append(eps)

exercise4(0.1,75)

# Create chart
fig, ax = plt.subplots()

# Set labels
ax.set_title('Value of Epsilon')
ax.set_ylabel('Epsilon')
ax.set_xlabel('Study Point')

# Draw chart
plt.plot(data)
plt.show()


I am working on the file "Assignment_03.ipynb".
The first task is:
Results without specifying single precision (float32):

End result is 1.082323233710861, true percent relative error 2.558289554517808e-11 %

End result is 1.0823232337108049, true percent relative error 3.077333064776834e-11 %

In order to present the results in single precision, we need to cast the values to numpy.float32. By default the calculations are performed with double precision (numpy.float64), which means the results are more accurate. With single precision the results are less accurate and as the true percent relative error value is low enough, it's not shown when using single precision.
Problem 01
Obtain graphs of the following functions. Locate the roots, local minimums, and local maximums graphically within two significant numbers and mark them into the graphs.
(a) $y = 3x^3 - x^2 2x + 1$, when $-2 \leq x \leq 2$

(b) $y = x^4 + \frac{x^3}{3} - \frac{5x^2}{2} + x - 1$, when $ -3 \leq x \leq 2$

(c) $y = x^5 - x^2 + 2$, when $-2 \leq x \leq 2$
Can you write Python code for it?

%matplotlib inline
def problem01(selection):

    if selection == "a":
        print("Matti")
    elif selection == "b":
        print("Teppo")
    elif selection == "c":

        # Create graph based on
        x = np.arange(-2, 2, 0.001)

        y = x**5 - x**2 + 2

        # Create the graph
        plt.plot(x, y, marker=".", linestyle=":")

        # Mark known root
        plt.plot(0, 2.0, 'ro:')

        # Mark known minima
        plt.plot(0.737007, 1.67427, 'yo:')

        plt.grid()
        plt.show()
    else:
        print("Invalid selection")

#problem01("a")
#problem01("b")
problem01("c")


I am working on the file "Assignment_03.ipynb".
The first task is:
Actual intersection:

x = 1.17951

y = 1.64098

Calculated values:

x = 1.1795

y = 1.64094428

The drawn point is slighly off the intersection but falls within the three significant figures of accuracy.
Ref: https://stackoverflow.com/questions/28766692/intersection-of-two-graphs-in-python-find-the-x-value
Problem 04
Draw the following rational functions. State any asymptotes and draw or mark them in the graphs.

(a) $f(x) = \frac{(2x + 1)}{(x - 3)} -4 \leq x \leq 4$

(b) $g(s) = \frac{s}{(s + 1)} -3 \leq x \leq 3$
Can you write Python code for it?

%matplotlib inline
def problem03(selection):

    if selection == "a":

        x = np.arange(-4, 4, 0.01)
        y = (2 * x + 1)/(x - 3)

        # Infinites in graph, limit y
        ylim([-50,50])
        xlim([-4, 4])
        plt.plot(x, y)

        # Draw asymptones
        plt.plot((3, 3), (-100, 100), 'r-')
        plt.plot((-100, 100), (2, 2), 'r-')

        plt.grid()
        plt.show()
    elif selection == "b":
        print("No calculations")
    else:
        print("Invalid selection")

problem03("a")
#problem03("b")


I am working on the file "Assignment_03.ipynb".
The first task is:
Problem 08
The infinite series
$$f(n) = \sum_{k=1}^{n}\frac{1}{k^4}$$
converges on a value of $f(n) = \pi^4/90$ as <i>n</i> approaches infinity. Write a program in single precision to calculate $f(n)$ for $n = 10,000$ by computing the sum from $k = 1$ to 10,000. Then repeat the calculation but reverse the order – that is, from k = 10, 000 to 1 using increments of −1. In each case, compute the true percent relative error. Explain the results.
Can you write Python code for it?

def problem08(start, max_value, addition):

    true_val = pi**4/90
    total = 0

    i = start
    while i > max_value or i < max_value:

        # Always add float32 (single prec. value to total)
        total += np.float32(1/(i**4))

        i += addition

    # Numerical true error
    enum = np.float32(true_val) - np.float32(total)

    # True percent relative error
    eper = enum / true_val

    print("End result is {}, true percent relative error {} %".format(np.float32(total), eper * 100))

problem08(1, 10000, 1)
problem08(10000, 0, -1)


I am working on the file "Assignment_03.ipynb".
The first task is:
We can analyse the graph provided and find out the equation has only one root (y = 2.0)
Local maxima:

y = 1.67427, x = 0.737007
Local minima:

y = 2.0, x = -0.000997453
The local minima is essentially the root value so it's not plotted.
Problem 02
(a) Draw $y = x^3$ and $y = 4 − 2x$ using the same axes. Note the x coordinate of the point of intersection within three significant figures accuracy.

(b) Draw $y = x^3 + 2x − 4$. Note the coordinate of the point where the curve cuts the x axis. Compare your answer with that from (a). Explain your findings.
Can you write Python code for it?

%matplotlib inline
def problem02(selection):

    if selection == "a":
        x = np.arange(-1, 3, 0.0001)

        y1 = x ** 3
        y2 = 4 - 2 * x

        plt.plot(x, y1)
        plt.plot(x, y2)

        idx = np.argwhere(np.diff(np.sign(y1 - y2)) != 0).reshape(-2) + 0
        plt.plot(x[idx], y1[idx], 'ro')

        print(x[idx])
        print(y1[idx])

        plt.grid()
        plt.show()
    elif selection == "b":
        x = np.arange(-1, 3, 0.001)

        y = x ** 3 + 2 * x - 4

        plt.plot(x, y)
        plt.grid()
        plt.show()
    else:
        print("Invalid selection")

problem02("a")
#problem02("b")


I am working on the file "Assignment_03_notebook.ipynb".
The first task is:
Results without specifying single precision (float32):

End result is 1.082323233710861, true percent relative error 2.558289554517808e-11 %

End result is 1.0823232337108049, true percent relative error 3.077333064776834e-11 %

In order to present the results in single precision, we need to cast the values to numpy.float32. By default the calculations are performed with double precision (numpy.float64), which means the results are more accurate. With single precision the results are less accurate and as the true percent relative error value is low enough, it's not shown when using single precision.
Problem 01
Obtain graphs of the following functions. Locate the roots, local minimums, and local maximums graphically within two significant numbers and mark them into the graphs.
(a) $y = 3x^3 - x^2 2x + 1$, when $-2 \leq x \leq 2$

(b) $y = x^4 + \frac{x^3}{3} - \frac{5x^2}{2} + x - 1$, when $ -3 \leq x \leq 2$

(c) $y = x^5 - x^2 + 2$, when $-2 \leq x \leq 2$
Can you write Python code for it?

def problem01(selection):

    if selection == "a":
        print("Matti")
    elif selection == "b":
        print("Teppo")
    elif selection == "c":

        # Create graph based on
        x = np.arange(-2, 2, 0.001)

        y = x**5 - x**2 + 2

        # Create the graph
        plt.plot(x, y, marker=".", linestyle=":")

        # Mark known root
        plt.plot(0, 2.0, 'ro:')

        # Mark known minima
        plt.plot(0.737007, 1.67427, 'yo:')

        plt.grid()
        plt.show()
    else:
        print("Invalid selection")

#problem01("a")
#problem01("b")
problem01("c")


I am working on the file "Assignment_03_notebook.ipynb".
The first task is:
Actual intersection:

x = 1.17951

y = 1.64098

Calculated values:

x = 1.1795

y = 1.64094428

The drawn point is slighly off the intersection but falls within the three significant figures of accuracy.
Ref: https://stackoverflow.com/questions/28766692/intersection-of-two-graphs-in-python-find-the-x-value
Problem 04
Draw the following rational functions. State any asymptotes and draw or mark them in the graphs.

(a) $f(x) = \frac{(2x + 1)}{(x - 3)} -4 \leq x \leq 4$

(b) $g(s) = \frac{s}{(s + 1)} -3 \leq x \leq 3$
Can you write Python code for it?

def problem03(selection):

    if selection == "a":

        x = np.arange(-4, 4, 0.01)
        y = (2 * x + 1)/(x - 3)

        # Infinites in graph, limit y
        ylim([-50,50])
        xlim([-4, 4])
        plt.plot(x, y)

        # Draw asymptones
        plt.plot((3, 3), (-100, 100), 'r-')
        plt.plot((-100, 100), (2, 2), 'r-')

        plt.grid()
        plt.show()
    elif selection == "b":
        print("No calculations")
    else:
        print("Invalid selection")

problem03("a")
#problem03("b")


I am working on the file "Assignment_03_notebook.ipynb".
The first task is:
Problem 08
The infinite series
$$f(n) = \sum_{k=1}^{n}\frac{1}{k^4}$$
converges on a value of $f(n) = \pi^4/90$ as <i>n</i> approaches infinity. Write a program in single precision to calculate $f(n)$ for $n = 10,000$ by computing the sum from $k = 1$ to 10,000. Then repeat the calculation but reverse the order – that is, from k = 10, 000 to 1 using increments of −1. In each case, compute the true percent relative error. Explain the results.
Can you write Python code for it?


I am working on the file "Assignment_04.ipynb".
The first task is:
Create variables for the exercise 4a and call function <i>gaussian(a, b)</i>
Can you write Python code for it?


I am working on the file "Assignment_04.ipynb".
The first task is:
Exercise 04
Given equation system
$$10.0x_1 + 2.0x_2 − x_3 = 27.0$$
$$−3.0x_1 − 6.0x_2 + 2.0x_3 = −61.5$$
$$x_1 + x_2 + 5.0x_3 = −21.5$$
(a) Solve by Gaussian elimination<br />
(b) Substitute your solutions into the original equations in order to
check answers.
In order to solve the given problems, we first need to create a function for Gaussian elimination. For that, I am using the function created in class.
Can you write Python code for it?

def gaussian(a, b):

    # Copy variables
    a = a.copy()
    b = b.copy()

    n = len(b)
    x = zeros(n)

    # Step 1: Forward elimination
    for k in range(0, n - 1):

        # Pivot the rows
        r = argmax(abs(a[k:, k])) + k

        if r != k:
            temp = a[k, :].copy()
            a[k, :] = a[r, :]
            a[r, :] = temp

            temp = b[k].copy()

            b[k] = b[r]
            b[r] = temp

        for i in range(k + 1, n):
            factor = a[i, k] / a[k, k]
            for j in range(n):
                a[i, j] = a[i, j] - factor * a[k, j]

            b[i] = b[i] - factor * b[k]

    # Step 2: Backward substitution
    x[n - 1] = b[n - 1] / a[n - 1, n - 1]
    for i in range(n - 2, -1, -1):
        sum = b[i]
        for j in range(i + 1, n):
            sum = sum - a[i, j] * x[j]
        x[i] = sum / a[i, i]

    return (x)


I am working on the file "Assignment_04.ipynb".
The first task is:
Exercise 06
Solve the equations Ax = b by Gauss elimination, where
$$A =
 \begin{pmatrix}
  0.0 & 0.0 & 2.0 & 1.0 & 2.0 \
  0.0 & 1.0 & 0.0 & 2.0 & -1.0 \
  1.0 & 2.0 & 0.0 & -2.0 & 0.0 \
  0.0 & 0.0 & 0.0 & -1.0 & 1.0 \
  0.0 & 1.0 & -1.0 & 1.0 & -1.0
 \end{pmatrix}
$$
and
$$b =
\begin{pmatrix}
1.0 \
1.0 \
-4.0 \
-2.0 \
-1.0
\end{pmatrix}$$
Hint: You need to reorder the equations before solving.
We can solve the exercise by using the gaussian function. Reordering the equation is done by pivoting the rows.
Can you write Python code for it?


I am working on the file "Assignment_04.ipynb".
The first task is:
How do we know if the values are correct? We can solve the equation by using the numpy.linalg package and dot function to get the dot-product of the matrix.
Can you write Python code for it?

from numpy.linalg import *
x = dot(inv(a,), b)

# Check if the arrays match by using the all() method
if result.all() == x.all():
    print("Correct!")
else:
    print("Incorrect!")


I am working on the file "Assignment_05.ipynb".
The first task is:
If you want to invert matrices with small determinant, the solution is to ensure the tolerances are low enough so that the inv() function can invert the matrix.
Problem 9
Use the Gauss-Seidel with relaxation to solve $Ax = b$, where
$$A = \begin{bmatrix}
4 & -1 & 0 & 0 \
-1 & 4 & -1 & 0 \
0 & -1 & 4 & -1 \
0 & 0 & -1 & 3
\end{bmatrix}
B = \begin{bmatrix}
15 \
10 \
10 \
10 \
\end{bmatrix}$$
Take $x_i = b_i/A_{ii}$ as the starting vector, and use $ω = 1.1$ for the relaxation
factor.
Solution
We can use the sample code created during class as a baseline for the exercise. We need to make couple of modifications to the source code in order to take the value of omega into the account. We also want to stop iterating once good enough accuracy is achieved. Accuracy is defined in the tol -variable.
The tolerance needed is calculated by taking dot product of xOld (taken before iteration) and x (current iteration) and comparing it against the tol variable. If the difference is less than the tol variable, it means the value of x has not changed more than the tolerence, which indicates we are close to the level of accuracy we need.
Expected result is: [ 5.  5.  5.  5.]
Can you write Python code for it?

def gaussSeidel(A, b):

    omega = 1.1
    # Amount of iterations
    p = 1000

    # Define tolerance
    tol = 1.0e-9

    n = len(b)
    x = np.zeros(n)

    # Generate array based on starting vector
    for y in range(n):
        x[y] = b[y]/A[y, y]

    # Iterate p times
    for k in range(p):
        xOld = x.copy()
        for i in range(n):
            s = 0
            for j in range(n):
                if j != i:
                    s = s + A[i, j] * x[j]
            x[i] = omega/A[i, i] * (b[i] - s) + (1 - omega)*x[i]

        # Break execution if we are within the tolerance needed
        dx = math.sqrt(np.dot(x-xOld,x-xOld))
        if dx < tol: return x
    return x

A = np.array(([4.0, -1, 0, 0],
               [-1, 4, -1, 0],
               [0, -1, 4, -1],
               [0, 0, -1, 3]))
b = np.array(([15.0, 10, 10, 10]))

x = gaussSeidel(A, b)

print("Result = {}".format(x))


I am working on the file "Assignment_05.ipynb".
The first task is:
Problem 3
Write a Python program that solves $Ax = b$ using LU decomposition. Use the functions lu_factor and lu_solve from scipy.linalg package.
$$ A = \begin{bmatrix}
1 & 4 & 1 \
1 & 6 & -1 \
2 & -1 & 2
\end{bmatrix}B = \begin{bmatrix}
7 \
13 \
5
\end{bmatrix}$$
Solution
We can use the functions lu_factor and lu_solve from scipy.linalg to solve the problem by using LU decomposition. Function lu_factor returns lu (N,N) which is a matrix containing U in its upper triangle, and L in its lower triangle. The function also returns piv (N,) which i representing the permutation matrix P.
In function lu_decomp1(A, b), the result of lu_factor is saved into a variable (PLU) which is later referred in lu_solve(PLU, b) function call, which gives us the result of $Ax = b$.
An alternative method for solving the problem would be to use "lu"-function, which unpacks the matrices into separate variables, which could be useful if you need to modify the variables or you don't want to use lu_solve to calculate the end result.
The expected result is: $[5.5,0.9,-2.1]$
Can you write Python code for it?

from scipy.linalg import lu_factor, lu_solve

# Create a function which can be used later if needed
def lu_decomp1(A, b):

    # Solve by using lu_factor and lu_solve
    PLU = lu_factor(A)
    x = lu_solve(PLU, b)

    return x

# Create variables
A = np.matrix(((1, 4, 1),
               (1, 6, -1),
               (2, -1, 2)))
b = np.array(([7, 13, 5]))

x = lu_decomp1(A, b)
print(dot(inv(A), b))

print("Result = {}".format(x))


I am working on the file "Assignment_05.ipynb".
The first task is:
Problem 6
Invert the following matrices with any method
$$ A = \begin{bmatrix}
5 & -3 & -1 & 0 \
-2 & 1 & 1 & 1 \
3 & -5 & 1 & 2 \
0 & 8 & -4 & -3
\end{bmatrix} B = \begin{bmatrix}
1 & 3 & -9 & 6 & 4 \
2 & -1 & 6 & 7 & 1 \
3 & 2 & -3 & 15 & 5 \
8 & -1 & 1 & 4 & 2 \
11 & 1 & -2 & 18 & 7
\end{bmatrix}$$
Comment on the reliability of the results.
Solution
Probably the simplest way to inverse the given matrices is to use inv() function from the numpy.linalg package. Inv() function returns inverse of the matrix given as a parameter in the function call.
Can you write Python code for it?


I am working on the file "Assignment_06.ipynb".
The first task is:
From the graph we can see to get acceleration of $0.0 m/s^2$ the angle must be roughly 31.7457 degrees. We can confirm this by using the functions previously created.
Can you write Python code for it?

angle = deg2rad(31.7457)
a = calculate_acc(angle)
calculate_t(angle)

print("Value of a is:", a)
print("Values for t are:\nt1 = {}\nt2 = {}\nt3 = {}".format(t[0], t[1], t[2]))


I am working on the file "Assignment_06.ipynb".
The first task is:
Now that we have values for a, t1, t2 and t3 with the angle of 45$^{\circ}$, we can start to iterate to find out the angle in which is the application is in balance. The application is in balance when acceleration equals 0.0 (e.g. it doesn't move one way or another).
Can you write Python code for it?

%matplotlib notebook
iterations = 100
step = -1

theta = np.deg2rad(45.0)

# Create empty arrays to store values into
x = []
y = []

for i in range(iterations):

    aOld = a.copy

    # Increment theta by step
    theta = degrees(theta)
    theta += step
    theta = deg2rad(theta)

    # Perform calculations with new theta
    aOld = calculate_acc(theta)
    t = calculate_t(theta)

    x.append(degrees(theta))
    y.append(aOld)

    # Break if we go under 0.0
    if aOld <= 0.0:
        break
    a = aOld

print("Value of a at break point is: {}".format(a))
print("Values for t are:\nt1 = {}\nt2 = {}\nt3 = {}".format(t[0], t[1], t[2]))

# Create plot
figure('Problem 1')

plt.plot(x, y)
plt.ylabel("Acceleration (m/s^2)")
plt.xlabel("Angle (degrees)")

plt.grid()
plt.show()


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
1c. The modulus operator
There is one operator (like the ones for multiplication and subtraction) that we did not discuss yet, namely the modulus operator %. Could you figure out by yourself what it does when you place it between two numbers (e.g. 113 % 9)? (PS: Try to figure it out by yourself first, by trying multiple combinations of numbers. If you do not manage, it's OK to use online resources...)
You don't need this operator all that often, but when you do, it comes in really handy! Also, it is important to learn how to figure out what operators/methods/functions you have never seen before do by playing with code, googling or reading documentation.
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
4a. Counting vowels
Count how many of each vowel (a,e,i,o,u) there are in the text string in the next cell. Print the count for each vowel with a single formatted string. Remember that vowels can be both lower and uppercase.
Can you write Python code for it?

text = """But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born
and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the
truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is
pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are
extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is
pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure.
To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage
from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying
consequences, or one who avoids a pain that produces no resultant pleasure? On the other hand, we denounce with
righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment,
so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs
to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil
and pain."""

# your code here


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
4c. Passwords
Write a program that asks a user for a password and checks some simple requirements of a password. If necessary, print out the following warnings (use if-statements):

Your password should contain at least 6 characters.
Your password should contain no more than 12 characters.
Your password only contains alphabetic characters! Please also use digits and/or special characters.
Your password only contains digits! Please also use alphabetic and/or special characters.
Your password should contain at least one special character.
Your password contains only lowercase letters! Please also use uppercase letters.
Your password contains only uppercase letters! Please also use lowercase letters.
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
5b. Alarm clock
Write code to set you alarm clock! Given the day of the week and information about whether you are currently on vacation or not, your code should print the time you want to be woken up following these constraints:
Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".
Encode the weeks days as ints in the following way: 0=Sun, 1=Mon, 2=Tue, ...6=Sat. Encode the vacation infromation as boolean. Your code should assign the correct time to a variable as a string (following this format: "7:00") and print it.
Note: Encoding the days as an integer helps you with defining conditions. You can check whether the week day is in a certain interval (instead of writing code for every single day).
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
Assignment 1: Calculation, Strings, Boolean Expressions and Conditions
Deadline: Friday, September 9, 2021 before 3pm (submit via Canvas: Block I/Assignment 1)
This assignment is not graded, but it is mandatory to submit a version that shows you have given it a serious try. We will check your assignments to monitor the progress and get an impression of how the course is going so far. You will receive feedback and solutions on Monday September 13. Please note that the material taught in this block is essential for all subsequent blocks.

Please name your notebook with the following naming convention: ASSIGNMENT_1_FIRSTNAME_LASTNAME.ipynb
If you have questions about this topic, please contact cltl.python.course@gmail.com.

In this block, we covered the following chapters:

Chapter 1 - Getting Started with Variables and Values
Chapter 2 - Basic Data Types (Integers and Floats)
Chapter 3 - Strings
Chapter 4 - Boolean Expressions and Conditions

In this assignment, you will be asked to show what you have learned from the topics above!
Looking things up
Don't worry - you do not have to know things by heart yet - feel free to have all Chapters of block 1 open while you work on this. This also goes for all future assignments.
Finding solutions online
Very often, you can find good solutions online. We encourage you to use online resources when you get stuck. However, please always try to understand the code you find and indicate that it is not your own. Use the following format to mark code written by someone else:
Taken from [link] [date]
[code]
###
Please use a similar format to indicate that you have worked with a classmate (e.g. mention the name instead of the link).
Indicating online resources and collaboration is mandatory! Please stick to this strategy for all course assignments.
Being stuck and getting help
It is very normal to get stuck every now and then. Sometimes you find what is wrong within minutes, sometimes this takes longer. If you get stuck, please apply the strategies described in the readme....
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
Exercise 2: Printing and user input
2a. Difference between "," and "+"
What is the difference between using + and , in a print statement? Illustrate by using both in each of the following:

calling the print() fuction with multiple strings
printing combinations of strings and integers
concatenating multiple strings and assign to one single variable
concatenating strings and integers and assign to one single variable

2b. Small Talk
Write a program to have a little conversation with someone. First ask them for their name and their age, and then say something about your own age compared to theirs. Your code should result in a conversation following this example:

Hello there! What is your name?
-- Emily.
Nice to meet you, Emily. How old are you?
-- 23
I'm 25 years old, so I'm 2 years older than you.

Also account for situations where the other person is older or the same age. You will need to use if-else-statements!
Can you write Python code for it?

name = input("Hello there! What is your name? ")
# finish this code


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
Exercise 3: String Art
3a. Drawing figures
We start with some repetition of the theory about strings:
| Topic | Explanation |
|-----------|--------|
| quotes |    A string is delimited by single quotes ('...') or double quotes ("...") |
| special characters |    Certain special characters can be used, such as "\n" (for newline) and "\t" (for a tab)     |
| printing special characters |   To print the special characters, they must be preceded by a backslash (\)   |
| continue on next line | A backslash (\) at the end of a line is used to continue a string on the next line      |
| multi-line strings |    A multi-line print statement should be enclosed by three double or three single quotes ("""...""" of '''...''')  |
Please run the code snippet below and observe what happens:
Can you write Python code for it?

print('hello\n')
print('To print a newline use \\n')
print('She said: \'hello\'')
print('\tThis is indented')
print('This is a very, very, very, very, very, very \
long print statement')
print('''
This is a multi-line print statement
First line
Second line
''')


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
Exercise 4: String methods
Remember that you can see all methods of the class str by using dir(). You can ignore all methods that start with one or two underscores.
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
Exercise 5: Boolean Logic and Conditions
5a. Speeding
Write code to solve the following scenario:
You are driving a little too fast, and a police officer stops you. Write code to compute and print the result, encoded as a string: 'no ticket', 'small ticket', 'big ticket'. If speed is 60 or less, the result is 'no ticket'. If speed is between 61 and 80 inclusive, the result is 'small ticket'. If speed is 81 or more, the result is 'big ticket'. Unless it is your birthday -- on that day, your speed can be 5 higher in all cases.
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
Help the cashier
Can you use the modulus operator you just learned about to solve the following task? Imagine you want to help cashiers to return the change in a convenient way. This means you do not want to return hands full of small coins, but rather use bills and as few coins as possible.
Write code  that classifies a given amount of money into smaller monetary units. Given a specific amout of dollars, your program should report the maximum number of dollar bills,  quarters, dimes, nickels, and pennies.
Set the amount variable to 11.67. You code should output a report listing the monetary equivalent in dollars, quarters, dimes, nickels, and pennies (one quarter is equivalent to 25 cents; one dime to 10 cents; one nickle to 5 cents and a pennie to 1 cent). Your program should report the maximum number of dollars, then the number of quarters, dimes, nickels, and pennies, in this order, to result in the minimum number of coins. Here are the steps in developing the program:

Convert the amount (11.67) into cents (1167).
First get the amount of cents that you would get after subtracting the maximum amount of dollars (100 cents) using the modulus operator (67 cents).
Then subtract the remainder (67 cents) from the total amount of cents (1167 cents) and divide this by 100 to find the number of dollars.
Use the modulus operator again to find out the remainder after subtracting the maximum amount of quarters (17 cents).
Subtract this remainder (17 cents) from the previous remainder (67 cents) and divide this by 25 to find out the number of quarters.
Follow the same steps for the dimes, nickels and pennies.
Display the result for your cashier! (the amount of dollars, quarters, dimes, nickels and pennies that (s)he would have to give back)`
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_1.ipynb".
The first task is:
Now write a program that asks users for their favorite color. Create the following output (assuming "red" is the chosen color). Use "+" and "*".
It should work with any color name though.
xml
red red red red red red red red red red
red                                 red
red                                 red
red red red red red red red red red red
Can you write Python code for it?

color = input('what is your favorite color? ')

print(color)
print(color)
print(color)
print(color)


I am working on the file "Assignments-colab/ASSIGNMENT_2.ipynb".
The first task is:
Exercise 2: list methods
In this exercise, we will focus on the following list methods:
a.) append
b.) count
c.) index
d.) insert
e.) pop
For each of the aforementioned list methods:

explain the positional parameters
explain the keyword parameters
you can exclude self from your explanation
explain what the goal of the method is and what data type it returns, e.g., string, list, set, etc.
give a working example. Provide also an example by providing a value for keyword parameters (assuming the method has one or more keyword parameters).

Exercise 3: set methods
In this exercise, we will focus on the following set methods:
* update
* pop
* remove
* clear
For each of the aforementioned set methods:

explain the positional parameters
explain the keyword parameters
you can exclude self from your explanation
explain what the goal of the method is and what data type it returns, e.g., string, list, set, etc.
give a working example. Provide also an example by providing a value for keyword parameters (assuming the method has one or more keyword parameters).

Please fill in your answers here:
Exercise 4: Analyzing vocabulary using sets
Please consider the following two texts:
These stories were copied from here.
Can you write Python code for it?

a_story = """In a far away kingdom, there was a river. This river was home to many golden swans. The swans spent most of their time on the banks of the river. Every six months, the swans would leave a golden feather as a fee for using the lake. The soldiers of the kingdom would collect the feathers and deposit them in the royal treasury.
One day, a homeless bird saw the river. "The water in this river seems so cool and soothing. I will make my home here," thought the bird.
As soon as the bird settled down near the river, the golden swans noticed her. They came shouting. "This river belongs to us. We pay a golden feather to the King to use this river. You can not live here."
"I am homeless, brothers. I too will pay the rent. Please give me shelter," the bird pleaded. "How will you pay the rent? You do not have golden feathers," said the swans laughing. They further added, "Stop dreaming and leave once." The humble bird pleaded many times. But the arrogant swans drove the bird away.
"I will teach them a lesson!" decided the humiliated bird.
She went to the King and said, "O King! The swans in your river are impolite and unkind. I begged for shelter but they said that they had purchased the river with golden feathers."
The King was angry with the arrogant swans for having insulted the homeless bird. He ordered his soldiers to bring the arrogant swans to his court. In no time, all the golden swans were brought to the King’s court.
"Do you think the royal treasury depends upon your golden feathers? You can not decide who lives by the river. Leave the river at once or you all will be beheaded!" shouted the King.
The swans shivered with fear on hearing the King. They flew away never to return. The bird built her home near the river and lived there happily forever. The bird gave shelter to all other birds in the river. """
print(a_story)

another_story = """Long time ago, there lived a King. He was lazy and liked all the comforts of life. He never carried out his duties as a King. "Our King does not take care...


I am working on the file "Assignments-colab/ASSIGNMENT_2.ipynb".
The first task is:
Exercise 4a: preprocessing text
Before analyzing the two texts, we are first going to preprocess them.
Please use a particular string method multiple times to replace the following characters by empty strings in both a_story and another_story:
* newlines: '\n'
* commas: ','
* dots: '.'
* quotes: '"'
Please assign the processed texts to the variables cleaned_story and cleaned_another_story.
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_2.ipynb".
The first task is:
Exercise 4c: from a list to a vocabulary (a set)
Please create a set for the words in each text by adding each word to a set. In the end, you should have two variables vocab_a_story and vocab_another_story, each containing the unique words in each story. Please use the output of Exercise 4b as the input for this exercise.
Can you write Python code for it?

vocab_a_story = set()
for word in list_cleaned_story:
    # insert your code here


I am working on the file "Assignments-colab/ASSIGNMENT_2.ipynb".
The first task is:
Exercise 4d: analyzing vocabularies
Please analyze the vocabularies by using set methods to determine:
* which words occur in both texts
* which words only occur in a_story
* which words only occur in another_story
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_2.ipynb".
The first task is:
Exercise 5: counting
Below you find a list called words, which is a list of strings.
a.) Please create a dictionary in which the key is the word, and the value is the frequency of the word.
Exclude all words which meet at least one of the following requirements:

end with the letter e
start with the letter t
start with the letter c and end with the letter w (both conditions must be met)
have six or more letters

You are not allowed to use the collections module to do this.
Can you write Python code for it?

words = ['there',
 'was',
 'a',
 'village',
 'near',
 'a',
 'jungle',
 'the',
 'village',
 'cows',
 'used',
 'to',
 'go',
 'up',
 'to',
 'the',
 'jungle',
 'in',
 'search',
 'of',
 'food.',
 'in',
 'the',
 'forest',
 'there',
 'lived',
 'a',
 'wicked',
 'lion',
 'he',
 'used',
 'to',
 'kill',
 'a',
 'cow',
 'now',
 'and',
 'then',
 'and',
 'eat',
 'her',
 'this',
 'was',
 'happening',
 'for',
 'quite',
 'sometime',
 'the',
 'cows',
 'were',
 'frightened',
 'one',
 'day',
 'all',
 'the',
 'cows',
 'held',
 'a',
 'meeting',
 'an',
 'old',
 'cow',
 'said',
 'listen',
 'everybody',
 'the',
 'lion',
 'eats',
 'one',
 'of',
 'us',
 'only',
 'because',
 'we',
 'go',
 'into',
 'the',
 'jungle',
 'separately',
 'from',
 'now',
 'on',
 'we',
 'will',
 'all',
 'be',
 'together',
 'from',
 'then',
 'on',
 'all',
 'the',
 'cows',
 'went',
 'into',
 'the',
 'jungle',
 'in',
 'a',
 'herd',
 'when',
 'they',
 'heard',
 'or',
 'saw',
 'the',
 'lion',
 'all',
 'of',
 'them',
 'unitedly',
 'moo',
 'and',
 'chased',
 'him',
 'away',
 'moral',
 'divided',
 'we',
 'fall',
 'united',
 'we',
 'stand']


I am working on the file "Assignments-colab/ASSIGNMENT_2.ipynb".
The first task is:
Exercise 6: Functions
Exercise 6a: the beersong
Please write a function that prints the beersong when it is called.
The function:
* is called print_beersong
* has one positional parameter start_number (this is 99 in the original song)
* prints the beer song
Can you write Python code for it?

def print_beersong(start_number):
    """
    """


I am working on the file "Assignments-colab/ASSIGNMENT_2.ipynb".
The first task is:
Exercise 6d: preprocessing text in a more general way
Please write a function that replaces all characters that the user provides by empty spaces.
The function is:
* is called clean_text_general
* has one positional parameter text
* has one keyword parameter chars_to_remove, which is a set (set the default to {'\n', ',', '.', '"'})
* return a string, e.g., the cleaned text
When the user provides a different value to chars_to_remove, e.g., {'a'}, then only those characters should be replaced by empty spaces in the text.
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_2.ipynb".
The first task is:
b.) Analyze your dicitionary by printing:
* how many keys it has
* what the highest word frequency is
* the sum of all values.
c.) In addition, print the frequencies of the following words using your dictionary (if the word does not occur in the dictionary, print 'WORD does not occur')
* up
* near
* together
* lion
* cow
Can you write Python code for it?

for word in ['up', 'near' , 'together', 'lion', 'cow']:
    # print frequency


I am working on the file "Assignments-colab/ASSIGNMENT_2.ipynb".
The first task is:
do the same for the other text
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_3a.ipynb".
The first task is:
Analyzing text with nltk
Exercise 5
Building a simple NLP pipeline
For this exercise, you will need NLTK. Don't forget to import it.
Write a function called tag_text, which takes raw text as input and returns the tagged text. To do this, make sure you follow the steps below:

Tokenize the text.

Perform part-of-speech tagging on the list of tokens.

Return the tagged text

Then test your function using the text snipped below (test_text) as input.
Please note that the tags may not be correct and that this is not a mistake on your end, but simply NLP tools not being perfect.
Can you write Python code for it?

test_text = """Shall I compare thee to a summer's day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date:"""

# your code here


I am working on the file "Assignments-colab/ASSIGNMENT_3a.ipynb".
The first task is:
Assignment 3a: Revision of block 3
Due: Friday the 1st of October 2021 14:30.

Please submit your assignment (notebooks of parts 3a and 3b + Python modules) as a single .zip file using Canvas (Assignments --> Assignment 3). Please put the notebooks for Assignment 3a and 3b as well as the Python modules (files ending with .py) in one folder, which you call ASSIGNMENT_3_FIRSTNAME_LASTNAME. Please zip this folder and upload it as your submission.

Please name your zip file with the following naming convention: ASSIGNMENT_3_FIRSTNAME_LASTNAME.zip

IMPORTANTE NOTE:
* The students who follow the Bachelor version of this course, i.e., the course Introduction to Python for Humanities and Social Sciences (L_AABAALG075) as part of the minor Digital Humanities, do not have to do Exercises 3 and 4 of Assignment 3b
* The other students, i.e., who follow the Master version of  course, which is Programming in Python for Text Analysis (L_AAMPLIN021), are required to do Exercises 3 and 4 of Assignment 3b
If you have questions about this topic, please contact us (cltl.python.course@gmail.com). Questions and answers will be collected on Piazza, so please check if your question has already been answered first.
In this block, we covered a lot of ground:

Chapter 12 - Importing external modules
Chapter 13 - Working with Python scripts
Chapter 14 - Reading and writing text files
Chapter 15 - Off to analyzing text

In this assignment, you will first complete a number of small exercises about each chapter to make sure you are familiar with the most important concepts. In the second part of the assignment, you will apply your newly acquired skills to write your very own text processing program (ASSIGNMENT-3b) :-). But don't worry, there will be instructions and hints along the way.
Can I use external modules other than the ones treated so far?
For now, please try to avoid it. All the exercises can be solved with what we have covered in block I, II, and III.
Functions & scope
Excercise 1:
Define a function called split_sort_text...
Can you write Python code for it?


I am working on the file "Assignments-colab/ASSIGNMENT_3a.ipynb".
The first task is:
Working with external modules
Exercise 2
NLTK offers a way of using WordNet in Python. Do some research (using google, because quite frankly, that's what we do very often) and see if you can find out how to import it. WordNet is a computational lexicon which organizes words according to their senses (collected in synsets). See if you can print all the synset definitions of the lemma dog.
Make sure you have run the following cell to make sure you have installed WordNet:
Can you write Python code for it?

import nltk
# uncomment the following line to download material including WordNet
# nltk.download('book')

# your code here


I am working on the file "Assignments-colab/ASSIGNMENT_3b.ipynb".
The first task is:
2.b) Store the function in the Python module utils.py. Import it in analyze.py.
Edit analyze.py so that:
* you first call the function get_paths
* create an empty dictionary called book2stats, i.e., book2stats = {}
* Loop over the list of txt files (the output from get_paths) and call the function get_basic_stats on each file
* print the output of calling the function get_basic_stats on each file.
* update the dictionary book2stats with each iteration of the for loop.
Tip: book2stats is a dictionary mapping a book name (the key), e.g., ‘AnnaKarenina’, to a dictionary (the value) (the output from get_basic_stats)
Tip: please use the following code snippet to obtain the basename name of a file path:
Can you write Python code for it?

import os
basename = os.path.basename('../Data/books/HuckFinn.txt')
book = basename.strip('.txt')
print(book)


I am working on the file "Assignments-colab/ASSIGNMENT_3b.ipynb".
The first task is:
Assignment 3b: Writing your own nlp program
Due: Friday the 1st of October 2021 14:30.

Please submit your assignment (notebooks of parts 3a and 3b + additional files) as a single .zip file using Canvas (Assignments --> Assignment 3)

Please name your zip file with the following naming convention: ASSIGNMENT_3_FIRSTNAME_LASTNAME.zip

IMPORTANTE NOTE:
* The students who follow the Bachelor version of this course, i.e., the course Introduction to Python for Humanities and Social Sciences (L_AABAALG075) as part of the minor Digital Humanities, do not have to do Exercises 3 and 4 of Assignment 3b
* The other students, i.e., who follow the Master version of  course, which is Programming in Python for Text Analysis (L_AAMPLIN021), are required to do Exercises 3 and 4 of Assignment 3b
If you have questions about this topic, please contact us (cltl.python.course@gmail.com). Questions and answers will be collected on Piazza, so please check if your question has already been answered first.
In this part of the assignment, we will carry out our own little text analysis project. The goal is to gain some insights into longer texts without having to read them all in detail.
This part of the assignment builds on some notions that have been revised in part A of the assignment. Please feel free to go back to part A and reuse your code whenever possible.
The goals of this part are:

divide a problem into smaller sub-problems and test code using small examples
doing text analysis and writing results to a file
combining small functions into bigger functions

Tip: The assignment is split into four steps, which are divided into smaller steps. Instead of doing everything step by step, we highly recommend you read all sub-steps of a step first and then start coding. In many cases, the sub-steps are there to help you split the problem into manageable sub-problems, but it is still good to keep the overall goal in mind.
Preparation: Data collection
In the directory ../Data/books/, you should find three .txt files. If not, you can...
Can you write Python code for it?

# Downloading data - you get this for free :-)

import requests
import os

def download_book(url):
    """
    Download book given a url to a book in .txt format and return it as a string.
    """
    text_request = requests.get(url)
    text = text_request.text
    return text

book_urls = dict()
book_urls['HuckFinn'] = 'http://www.gutenberg.org/cache/epub/7100/pg7100.txt'
book_urls['Macbeth'] = 'http://www.gutenberg.org/cache/epub/1533/pg1533.txt'
book_urls['AnnaKarenina'] = 'http://www.gutenberg.org/files/1399/1399-0.txt'

if not os.path.isdir('../Data/books/'):
    os.mkdir('../Data/books/')

for name, url in book_urls.items():
    text = download_book(url)
    with open('../Data/books/'+name+'.txt', 'w', encoding='utf-8') as outfile:
        outfile.write(text)


I am working on the file "Assignments-colab/ASSIGNMENT_3b.ipynb".
The first task is:
Encoding issues with txt files
For Windows users, the file “AnnaKarenina.txt” gets the encoding cp1252.
In order to open the file, you have to add encoding='utf-8', i.e.,
python
a_path = 'some path on your computer.txt'
with open(a_path, mode='r', encoding='utf-8'):
    # process file
Exercise 1
Was the download successful? Let's start writing code! Please create the following two Python modules:

Python module analyze.py This module you will call from the command line
Python module utils.py This module will contain your helper functions

More precisely, please create two files, analyze.py and utils.py, which are both placed in the same directory as this notebook. The two files are empty at this stage of the assignment.
1.a) Write a function called get_paths and store it in the Python module utils.py
The function get_paths:
* takes one positional parameter called input_folder
* the function stores all paths to .txt files in the input_folder in a list
* the function returns a list of strings, i.e., each string is a file path
Once you've created the function and stored it in utils.py:
* Import the function into analyze.py, using from utils import get_paths
* Call the function inside analyze.py (input_folder="../Data/books")
* Assign the output of the function to a variable and print this variable.
* call analyze.py from the command line to test it
Exercise 2
2.a) Let's get a little bit of an overview of what we can find in each text. Write a function called get_basic_stats.
The function get_basic_stats:
* has one positional parameter called txt_path which is the path to a txt file
* reads the content of the txt file into a string
* Computes the following statistics:
    * The number of sentences
    * The number of tokens
    * The size of the vocabulary used (i.e. unique tokens)
    * the number of chapters/acts:
        * count occurrences of 'CHAPTER' in HuckFinn.txt
        * count occurrences of 'Chapter ' (with the space) in AnnaKarenina.txt
        * count occurrences of 'ACT' in Macbeth.txt
* return a...
Can you write Python code for it?

import nltk
nltk.download('punkt')

from nltk.tokenize import sent_tokenize, word_tokenize

text = 'Python is a programming language. It was created by Guido van Rossum.'
for sent in sent_tokenize(text):
    print('SENTENCE', sent)
    tokens = word_tokenize(sent)
    print('TOKENS', tokens)