I am working on the file "ML1/lab2_original.ipynb".
The first task is:
MNIST consists of small 28 by 28 pixel images of written digits (0-9). We split the dataset into a training, validation and testing arrays. The variables x_train, x_valid and x_test are $N \times M$ matrices, where $N$ is the number of datapoints in the respective set, and $M = 28^2 = 784$ is the dimensionality of the data. The second set of variables t_train, t_valid and t_test contain the corresponding $N$-dimensional vector of integers, containing the true class labels.
Here's a visualisation of the first 8 digits of the trainingset:
Can you write Python code for it?


I am working on the file "ML1/lab2_original.ipynb".
The first task is:
Part 1. Multiclass logistic regression
Scenario: you have a friend with one big problem: she's completely blind. You decided to help her: she has a special smartphone for blind people, and you are going to develop a mobile phone app that can do machine vision using the mobile camera: converting a picture (from the camera) to the meaning of the image. You decide to start with an app that can read handwritten digits, i.e. convert an image of handwritten digits to text (e.g. it would enable her to read precious handwritten phone numbers).
A key building block for such an app would be a function predict_digit(x) that returns the digit class of an image patch $\bx$. Since hand-coding this function is highly non-trivial, you decide to solve this problem using machine learning, such that the internal parameters of this function are automatically learned using machine learning techniques.
The dataset you're going to use for this is the MNIST handwritten digits dataset (http://yann.lecun.com/exdb/mnist/). You can download the data with scikit learn, and load it as follows:
Can you write Python code for it?

from sklearn.datasets import fetch_mldata
# Fetch the data
mnist = fetch_mldata('MNIST original')
data, target = mnist.data, mnist.target.astype('int')
# Shuffle
indices = np.arange(len(data))
np.random.seed(123)
np.random.shuffle(indices)
data, target = data[indices].astype('float32'), target[indices]

# Normalize the data between 0.0 and 1.0:
data /= 255.

# Split
x_train, x_valid, x_test = data[:50000], data[50000:60000], data[60000: 70000]
t_train, t_valid, t_test = target[:50000], target[50000:60000], target[60000: 70000]


I am working on the file "ML1/lab2_original.ipynb".
The first task is:
Part 2. Multilayer perceptron
You discover that the predictions by the logistic regression classifier are not good enough for your application: the model is too simple. You want to increase the accuracy of your predictions by using a better model. For this purpose, you're going to use a multilayer perceptron (MLP), a simple kind of neural network. The perceptron wil have a single hidden layer $\bh$ with $L$ elements. The parameters of the model are $\bV$ (connections between input $\bx$ and hidden layer $\bh$), $\ba$ (the biases/intercepts of $\bh$), $\bW$ (connections between $\bh$ and $\log q$) and $\bb$ (the biases/intercepts of $\log q$.
The conditional probability of the class label $j$ is given by:
$\log p(t = j \;|\; \bx, \bb, \bW) = \log q_j - \log Z$
where $q_j$ are again the unnormalized probabilities per class, and $Z = \sum_j q_j$ is again the probability normalizing factor. Each $q_j$ is computed using:
$\log q_j = \bw_j^T \bh + b_j$
where $\bh$ is a $L \times 1$ vector with the hidden layer activations (of a hidden layer with size $L$), and $\bw_j$ is the $j$-th column of $\bW$ (a $L \times 10$ matrix). Each element of the hidden layer is computed from the input vector $\bx$ using:
$h_j = \sigma(\bv_j^T \bx + a_j)$
where $\bv_j$ is the $j$-th column of $\bV$ (a $784 \times L$ matrix), $a_j$ is the $j$-th element of $\ba$, and $\sigma(.)$ is the so-called sigmoid activation function, defined by:
$\sigma(x) = \frac{1}{1 + \exp(-x)}$
Note that this model is almost equal to the multiclass logistic regression model, but with an extra 'hidden layer' $\bh$. The activations of this hidden layer can be viewed as features computed from the input, where the feature transformation ($\bV$ and $\ba$) is learned.
2.1 Derive gradient equations (20 points)
State (shortly) why $\nabla_{\bb} \mathcal{L}^{(n)}$ is equal to the earlier (multiclass logistic regression) case, and why $\nabla_{\bw_j} \mathcal{L}^{(n)}$ is almost equal to the earlier case.
Like in multiclass logistic regression, you should use...
Can you write Python code for it?

# Write all helper functions here
def qj(w, h, b, j):
    return np.exp((w.T)[j]*h + b[j])

def Z(q_j):
    np.sum(q_j)

# 2
def delta_qj(w, h, b, j, t_n):
    if j == t_n:
        I = 1
    else:
        I = 0

    q_j = qj(w, h, b, j)
    Z = Z(q_j)

    return I - q_j / Z

# 3
def partial_L_wij(w, h, b, j, t_n, i):
    return h[i] * delta_qj(w, h, b, j, t_n)

# 1
def delta_h(W, delta_q):
    return W * delta_q

#4
def partial_L_Vhi(h, x, W, delta_q, i, k):
    return h[i] * (1 - h[i]) * x[k] * delta_h(W, delta_q)[i]

#5
def partial_L_ai(h, W, delta_q, i):
    return h[i] * (1 - h[i]) * delta_h(W, delta_q)[i]

def gradient_L(W, w, h, x, t_n, j, i, k, delta_q):
    return [partial_L_wij(w, h, b, j, t_n, i), partial_L_Vhi(h, x, W, delta_q, i, k), partial_L_ai(h, W, delta_q, i)]

def w_t1(w_t, eta, gradient_L):
    return w_t - eta*gradient_L

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def dsig(s):
    return s * (1 - s)

def calc_h(x, v, a):
    return sigmoid(np.matmul(x, v) + a)

def logq_j(w_j, h, Z):
    return w_j.T*h + b_j - log(Z)

def logp_j(w_j, h, Z, q_j):
    return logq_j(w_j, h, Z) - Z(q_j)

# Write training code here:
# Plot the conditional loglikelihoods for the train and validation dataset after every iteration.
# Plot the weights of the first layer.

x_test = x_train[0:1,:]
t_test = t_train[0:1]

K = x_train.shape[1] # No. of input units
L = 20 # No. of hidden units
M = 10 # No. of output units

V = np.zeros((K, L))
W = np.zeros((L, M))
a = np.zeros((L,))
b = np.zeros((M,))

h_test = calc_h(x_test, V, a)

logreg_gradient(h_test, t_test, W, b)


I am working on the file "ML1/lab3.ipynb".
The first task is:
1. Sampling from the Gaussian process prior (30 points)
We will implement Gaussian process regression using the kernel function in Bishop Eqn. 6.63.
1.1 k_n_m( xn, xm, thetas ) (5 points)
To start, implement function k_n_m(xn, xm, thetas) that takes scalars $x_n$ and $x_m$, and a vector of $4$ thetas, and computes the kernel function Bishop Eqn. 6.63 (10 points).  NB: usually the kernel function will take $D$ by $1$ vectors, but since we are using a univariate problem, this makes things easier.
Can you write Python code for it?

def k_n_m(xn, xm, thetas):
    theta0, theta1, theta2, theta3 = thetas # Unpack thetas

    if(xn == xm):
        k = theta0 + theta2 + theta3*xn**2
    else:
        k = theta0 * np.exp(-(theta1/2)*(xn-xm)**2) + theta2 + theta3*xn*xm

    return k


I am working on the file "ML1/lab3.ipynb".
The first task is:
1.2 computeK( X1, X2, thetas ) (10 points)
Eqn 6.60 is the marginal distribution of mean output of $N$ data vectors: $p(\mathbf{y}) = \mathcal{N}(0, \mathbf{K})$.  Notice that the expected mean function is $0$ at all locations, and that the covariance is a $N$ by $N$ kernel matrix $\mathbf{K}$.  Write a function computeK(x1, x2, thetas)
that computes the kernel matrix. Use k_n_m as part of an inner loop (of course, there are more efficient ways of computing the kernel function making better use of vectorization, but that is not necessary) (5 points).
Can you write Python code for it?

def computeK(x1, x2, thetas):

    K = np.zeros(shape=(len(x1), len(x2))) # Create empty array

    for xn, row in zip(x1, range(len(x1))): # Iterate over x1
        for xm, column in zip(x2, range(len(x2))): # Iterate over x2
            K[row, column] = k_n_m(xn, xm, thetas) # Add kernel to matrix

    return K

x1 = [0, 0, 1]
x2 = [0, 0, 1]
thetas = [1, 2, 3, 1]
K = computeK(x1, x2, thetas)

### Test your function
x1 = [0, 1, 2]
x2 = [1, 2, 3, 4]
thetas = [1, 2, 3, 4]
K = computeK(x1, x2, thetas)

assert K.shape == (len(x1), len(x2)), "the shape of K is incorrect"


I am working on the file "ML1/lab3.ipynb".
The first task is:
1.3 Plot function samples (15 points)
Now sample mean functions at the x_test locations for the theta values in Bishop Figure 6.5, make a figure with a 2 by 3 subplot and make sure the title reflects the theta values (make sure everything is legible).  In other words, sample $\by_i \sim \mathcal{N}(0, \mathbf{K}_{\theta})$.  Make use of numpy.random.multivariate_normal().  On your plots include the expected value of $\by$ with a dashed line and fill_between 2 standard deviations of the uncertainty due to $\mathbf{K}$ (the diagonal of $\mathbf{K}$ is the variance of the model uncertainty) (15 points).
Can you write Python code for it?

import matplotlib.pyplot as plt

# The thetas
thetas0 = [1, 4, 0, 0]
thetas1 = [9, 4, 0, 0]
thetas2 = [1, 64, 0, 0]
thetas3 = [1, 0.25, 0, 0]
thetas4 = [1, 4, 10, 0]
thetas5 = [1, 4, 0, 5]

f, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, sharex='col', sharey='row') # Subplot setup

all_thetas = [thetas0, thetas1, thetas2, thetas3, thetas4, thetas5] # List of all thetas
all_plots = [ax1, ax2, ax3, ax4, ax5, ax6] # List of all plots

for subplot_, theta_ in zip(all_plots, all_thetas): # Iterate over all plots and thetas
    K = computeK(x_test, x_test, theta_) # Compute K

    # Fix numerical error on eigenvalues 0 that are slightly negative (<e^15)
    min_eig = np.min(np.real(np.linalg.eigvals(K)))
    if min_eig < 0:
        K -= 10*min_eig * np.eye(*K.shape)

    mean = np.zeros(shape=len(K)) # Generate Means

    random = numpy.random.multivariate_normal(mean, K) # Generate random datapoints from multivariate normal

    uncertainties = []
    for i in range(len(K)):
        uncertainties.append(K[i,i])

    subplot_.plot(random) # Plot random generated

    x = np.arange(0, 100) # 100 Steps
    subplot_.fill_between(x, random, random + uncertainties, alpha=0.3, color='pink') # Plot uncertainty
    subplot_.fill_between(x, random, random - uncertainties, alpha=0.3, color='pink') # Plot uncertainty

    subplot_.plot(y_test, 'g--') # Plot ground truth
    subplot_.legend(['predicted', 'ground truth']) # Add legend
    subplot_.set_title(theta_) # Set title

plt.show()


I am working on the file "ML1/lab3.ipynb".
The first task is:
2. Predictive distribution (35 points)
So far we have sampled mean functions from the prior.  We can draw actual data $\bt$ two ways.  The first way is generatively, by first sampling $\by | \mathbf{K}$, then sampling $\bt | \by, \beta$ (Eqns 6.60 followed by 6.59).  The second way is to integrate over $\by$ (the mean draw) and directly sample $\bt | \mathbf{K}, \beta$ using Eqn 6.61.    This is the generative process for $\bt$.  Note that we have not specified a distribution over inputs $\bx$;  this is because Gaussian processes are conditional models.  Because of this we are free to generate locations $\bx$ when playing around with the GP; obviously a dataset will give us input-output pairs.
Once we have data, we are interested in the predictive distribution (note: the prior is the predictive distribution when there is no data).  Consider the joint distribution for $N+1$ targets, given by Eqn 6.64.  Its covariance matrix is composed of block components $C_N$, $\mathbf{k}$, and $c$.  The covariance matrix $C_N$ for $\bt_N$ is $C_N = \mathbf{K}_N + \beta^{-1}\mathbf{I}_N$.  We have just made explicit the size $N$ of the matrix; $N$ is the number of training points.  The kernel vector $\mathbf{k}$ is a $N$ by $1$ vector of kernel function evaluations between the training input data and the test input vector.  The scalar $c$ is a kernel evaluation at the test input.
2.1 gp_predictive_distribution(...) (10 points)
Write a function gp_predictive_distribution(x_train, t_train, x_test, theta, beta, C=None) that computes  Eqns 6.66 and 6.67, except allow for an arbitrary number of test points (not just one) and now the kernel matrix is for training data.  By having C as an optional parameter, we can avoid computing it more than once (for this problem it is unimportant, but for real problems this is an issue).  The function should compute $\mathbf{C}$, $\mathbf{k}$, and return the mean, variance and $\mathbf{C}$.  Do not forget: the computeK function computes $\mathbf{K}$, not $\mathbf{C}$! (10 points)
Can you write Python code for it?

def gp_predictive_distribution(x_train, t_train, x_test, theta, beta, C=None):
    # YOUR CODE HERE
    raise NotImplementedError()

    return mean_test, var_test, C

### Test your function
N = 2
train_x = np.linspace(-1, 1, N)
train_t = 2*train_x
test_N = 3
test_x = np.linspace(-1, 1, test_N)
theta = [1, 2, 3, 4]
beta = 25
test_mean, test_var, C = gp_predictive_distribution(train_x, train_t, test_x, theta, beta, C=None)

assert test_mean.shape == (test_N,), "the shape of mean is incorrect"
assert test_var.shape == (test_N, test_N), "the shape of var is incorrect"
assert C.shape == (N, N), "the shape of C is incorrect"

C_in = np.array([[0.804, -0.098168436], [-0.098168436, 0.804]])
_, _, C_out = gp_predictive_distribution(train_x, train_t, test_x, theta, beta, C=C_in)

assert np.allclose(C_in, C_out), "C is not reused!"


I am working on the file "ML1/lab3.ipynb".
The first task is:
2.4 Plot the decision boundary (10 Points)
The decision boundary is fully specified by a (usually very small) subset of training samples, the support vectors. Make use of
\begin{align}
\bw &= \sum_{n=1}^N a_n t_n \mathbf{\phi}(\bx_n)\
b &= \frac{1}{N_S}\sum_{n \in S} (t_n - \sum_{m \in S} a_m t_m k(\bx_n, \bx_m)),
\end{align}
where $S$ denotes the set of indices of the support vectors, to calculate the slope and intercept of the decision boundary. Generate a last plot that contains the two subsets, support vectors and decision boundary.
Can you write Python code for it?

# YOUR CODE HERE
# raise NotImplementedError()

w_opt = np.squeeze(np.dot(a_opt.T, np.dot(np.diag(t), X)))
K_sv = computeK(X_sv)
N_sv = size(sv_ind)

atk_sv = np.dot(a_sv.T * t_sv, K_sv)
b = np.sum(t_sv - atk_sv)/N_sv

x_lim = np.array([1, 4])
y_lim = (-w_opt[0] * x_lim - b) / w_opt[1]

plt.figure()
plt.axis('equal')
plt.scatter(X1[:, 0], X1[:, 1], c='b', marker='o')
plt.scatter(X2[:, 0], X2[:, 1], c='r', marker='o')
plt.scatter(X_sv[:, 0], X_sv[:, 1], s=200, facecolors='none', edgecolors='lime', linewidth='3')
plt.plot(x_lim, y_lim, c='black')
plt.show()


I am working on the file "ML1/lab3.ipynb".
The first task is:
Next, we will rewrite the dual representation so that we can make use of computationally efficient vector-matrix multiplication. The objective becomes
\begin{align}
\min_{\ba} \frac{1}{2} \ba^T K' \ba - 1^T\ba,
\end{align}
subject to the constraints
\begin{align}
a_n &\geq 0, n=1,...,N,\
\bt^T\ba &= 0.
\end{align}
Where
\begin{align}
K'{nm} = t_n t_m k(\bx_n, \bx_m),
\end{align}
and in the special case of a linear kernel function,
\begin{align}
K'{nm} = t_n t_m k(\bx_n, \bx_m) = k(t_n \bx_n, t_m \bx_m).
\end{align}
To solve the quadratic programming problem we will use a python module called cvxopt. You first have to install the module in your virtual environment (you have to activate it first), using the following command:
conda install -c conda-forge cvxopt
The quadratic programming solver can be called as
cvxopt.solvers.qp(P, q[, G, h[, A, b[, solver[, initvals]]]])
This solves the following problem,
\begin{align}
\min_{\bx} \frac{1}{2} \bx^T P \bx + q^T\bx,
\end{align}
subject to the constraints,
\begin{align}
G\bx &\leq h,\
A\bx &= b.
\end{align}
All we need to do is to map our formulation to the cvxopt interface.
b) (10 points) Write a function compute_multipliers(X, t) that solves the quadratic programming problem using the cvxopt module and returns the lagrangian multiplier for every sample in the dataset.
Can you write Python code for it?

import cvxopt

def compute_multipliers(X, t):
    # YOUR CODE HERE
    # raise NotImplementedError()
    K = computeK(np.dot(np.diag(t), X))
    q = cvxopt.matrix(-np.ones_like(t, dtype='float'))
    G = cvxopt.matrix(np.diag(-np.ones_like(t, dtype='float')))
    A = cvxopt.matrix(t).T
    h = cvxopt.matrix(np.zeros_like(t, dtype='float'))
    b = cvxopt.matrix(0.0)

    P = cvxopt.matrix(K)
    sol = cvxopt.solvers.qp(P, q, G, h, A, b)
    a = np.array(sol['x'])
    return a

### Test your function
dim = 2
N_test = 3
X_test = np.arange(6).reshape((N_test, dim))
t_test = np.array([-1., 1., 1.])
a_test = compute_multipliers(X_test, t_test)

assert a_test.shape == (N_test, 1)


I am working on the file "ML1/lab3.ipynb".
The first task is:
Part 1: Gaussian Processes
For part 1 we will be refer to Bishop sections 6.4.2 and 6.4.3. You may also want to refer to Rasmussen's Gaussian Process text which is available online at http://www.gaussianprocess.org/gpml/chapters/ and especially to the project found at https://www.automaticstatistician.com/index/ by Ghahramani for some intuition in GP.  To understand Gaussian processes, it is highly recommended understand how marginal, partitioned Gaussian distributions can be converted into conditional Gaussian distributions.  This is covered in Bishop 2.3 and summarized in Eqns 2.94-2.98.
$\newcommand{\bt}{\mathbf{t}}$
$\newcommand{\bx}{\mathbf{x}}$
$\newcommand{\by}{\mathbf{y}}$
$\newcommand{\bw}{\mathbf{w}}$
$\newcommand{\ba}{\mathbf{a}}$
Periodic Data
We will use the same data generating function that we used previously for regression.
Can you write Python code for it?

def true_mean_function(x):
    return np.cos(2*pi*(x+1))

def add_noise(y, sigma):
    return y + sigma*np.random.randn(len(y))

def generate_t(x, sigma):
    return add_noise(true_mean_function(x), sigma)

sigma = 0.2
beta  = 1.0 / pow(sigma, 2)
N_test = 100

x_test = np.linspace(-1, 1, N_test)
mu_test = np.zeros(N_test)
y_test = true_mean_function(x_test)
t_test = add_noise(y_test, sigma)

plt.plot( x_test, y_test, 'b-', lw=2)
plt.plot( x_test, t_test, 'go')
plt.show()


I am working on the file "ML1/lab3.ipynb".
The first task is:
Part 2: Support Vector Machines (45 points)
As seen in Part 1: Gaussian Processes, one of the significant limitations of many such algorithms is that the kernel function $k(\bx_n , \bx_m)$ must be evaluated for all possible pairs $\bx_n$ and $\bx_m$ of training points, which can be computationally infeasible during training and can lead to excessive computation times when making predictions for new data points.
In Part 2: Support Vector Machines, we shall look at kernel-based algorithms that have sparse solutions, so that predictions for new inputs depend only on the kernel function evaluated at a subset of the training data points.
2.1 Generating a linearly separable dataset (15 points)
a) (5 points) First of all, we are going to create our own 2D toy dataset $X$. The dataset will consists of two i.i.d. subsets $X_1$ and $X_2$, each of the subsets will be sampled from a multivariate Gaussian distribution,
\begin{align}
X_1 \sim &\mathcal{N}(\mu_1, \Sigma_1)\
&\text{ and }\
X_2 \sim &\mathcal{N}(\mu_2, \Sigma_2).
\end{align}
In the following, $X_1$ will have $N_1=20$ samples and a mean $\mu_1=(1,1)$. $X_2$ will have $N_2=30$ samples and a mean $\mu_2=(3,3)$.
Plot the two subsets in one figure, choose two colors to indicate which sample belongs to which subset. In addition you should choose, $\Sigma_1$ and $\Sigma_2$ in a way that the two subsets become linearly separable. (Hint: Which form has the covariance matrix for a i.i.d. dataset?)
Can you write Python code for it?

# YOUR CODE HERE
# raise NotImplementedError()
np.random.seed(1)
plt.rcParams["figure.figsize"] = [10,10]

# Cov should be diagonal (independency) and have the same values (identical), i.e. a*I.
def create_X(mean, sig, N):
    return np.random.multivariate_normal(mean, sig * np.identity(2), N)

m1 = [1, 1]; m2 = [3, 3]
s1 = 1/2; s2 = 1/2
N1 = 20; N2 = 30

X1 = create_X(m1, s1, N1)
X2 = create_X(m2, s2, N2)

plt.figure()
plt.axis('equal')
plt.scatter(X1[:, 0], X1[:, 1], c='b', marker='o')
plt.scatter(X2[:, 0], X2[:, 1], c='r', marker='o')
plt.show()


I am working on the file "ML1/lab3.ipynb".
The first task is:
b) (10 points) In the next step we will combine the two datasets X_1, X_2 and generate a vector t containing the labels. Write a function create_X_and_t(X1, X2) it should return the combined data set X and the corresponding target vector t.
Can you write Python code for it?

def create_X_and_t(X1, X2):
    # YOUR CODE HERE
    # raise NotImplementedError()
    X1_len = X1.shape[0]
    X2_len = X2.shape[0]

    X = np.vstack((X1, X2))
    t = np.hstack((-np.ones(X1_len), np.ones(X2_len)))

    # Shuffle data?
    indices = np.arange(X1_len + X2_len)
    np.random.shuffle(indices)

    return X[indices], t[indices]

### Test your function
dim = 2
N1_test = 2
N2_test = 3
X1_test = np.arange(4).reshape((N1_test, dim))
X2_test = np.arange(6).reshape((N2_test, dim))
X_test, t_test = create_X_and_t(X1_test, X2_test)

assert X_test.shape == (N1_test + N2_test, dim), "the shape of X is incorrect"
assert t_test.shape == (N1_test + N2_test,), "the shape of t is incorrect"


I am working on the file "ML1/lab3_verna.ipynb".
The first task is:
1. Sampling from the Gaussian process prior (30 points)
We will implement Gaussian process regression using the kernel function in Bishop Eqn. 6.63.
1.1 k_n_m( xn, xm, thetas ) (5 points)
To start, implement function k_n_m(xn, xm, thetas) that takes scalars $x_n$ and $x_m$, and a vector of $4$ thetas, and computes the kernel function Bishop Eqn. 6.63 (10 points).  NB: usually the kernel function will take $D$ by $1$ vectors, but since we are using a univariate problem, this makes things easier.
Can you write Python code for it?

def k_n_m(xn, xm, thetas):
    theta0, theta1, theta2, theta3 = thetas # Unpack thetas

    if(xn == xm):
        k = theta0 + theta2 + theta3*xn*xm
    else:
        k = theta0 * np.exp(-(theta1/2)*(xn-xm)**2) + theta2 + theta3*xn*xm

    return k


I am working on the file "ML1/lab3_verna.ipynb".
The first task is:
1.2 computeK( X1, X2, thetas ) (10 points)
Eqn 6.60 is the marginal distribution of mean output of $N$ data vectors: $p(\mathbf{y}) = \mathcal{N}(0, \mathbf{K})$.  Notice that the expected mean function is $0$ at all locations, and that the covariance is a $N$ by $N$ kernel matrix $\mathbf{K}$.  Write a function computeK(x1, x2, thetas)
that computes the kernel matrix. Use k_n_m as part of an inner loop (of course, there are more efficient ways of computing the kernel function making better use of vectorization, but that is not necessary) (5 points).
Can you write Python code for it?


I am working on the file "ML1/lab3_verna.ipynb".
The first task is:
1.3 Plot function samples (15 points)
Now sample mean functions at the x_test locations for the theta values in Bishop Figure 6.5, make a figure with a 2 by 3 subplot and make sure the title reflects the theta values (make sure everything is legible).  In other words, sample $\by_i \sim \mathcal{N}(0, \mathbf{K}_{\theta})$.  Make use of numpy.random.multivariate_normal().  On your plots include the expected value of $\by$ with a dashed line and fill_between 2 standard deviations of the uncertainty due to $\mathbf{K}$ (the diagonal of $\mathbf{K}$ is the variance of the model uncertainty) (15 points).
Can you write Python code for it?

import matplotlib.pyplot as plt

# The thetas
thetas0 = [1, 4, 0, 0]
thetas1 = [9, 4, 0, 0]
thetas2 = [1, 64, 0, 0]
thetas3 = [1, 0.25, 0, 0]
thetas4 = [1, 4, 10, 0]
thetas5 = [1, 4, 0, 5]

f, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3) # Subplot setup

all_thetas = [thetas0, thetas1, thetas2, thetas3, thetas4, thetas5] # List of all thetas
all_plots = [ax1, ax2, ax3, ax4, ax5, ax6] # List of all plots

n = 5 # Number of samples per subplot

for subplot_, theta_ in zip(all_plots, all_thetas): # Iterate over all plots and thetas
    K = computeK(x_test, x_test, theta_) # Compute K

    # Fix numerical error on eigenvalues 0 that are slightly negative (<e^15)
    min_eig = np.min(np.real(np.linalg.eigvals(K)))
    if min_eig < 0:
        K -= 10*min_eig * np.eye(*K.shape)

    mean = np.zeros(shape=len(K)) # Generate Means

    random = numpy.random.multivariate_normal(mean, K)

    # Draw n random samples
    samples = [numpy.random.multivariate_normal(mean, K) for i in range(n)]

    # Calculate expected y and variance
    expected_y = numpy.mean(np.array(samples), axis=0)
    uncertainties = numpy.sqrt(K.diagonal())

    for sample in samples:
        subplot_.plot(sample)

    x = np.arange(0, 100) # 100 Steps

    # Plot uncertainty
    subplot_.fill_between(
        x, expected_y - 2 * uncertainties, expected_y + 2 * uncertainties,
        alpha=0.3, color='pink'
    )
    subplot_.plot(expected_y, 'g--') # Plot ground truth
    # subplot_.legend(['Sampled y', 'Expected y']) # Add legend
    subplot_.set_title(theta_) # Set title

plt.show()


I am working on the file "ML1/lab3_verna.ipynb".
The first task is:
2. Predictive distribution (35 points)
So far we have sampled mean functions from the prior.  We can draw actual data $\bt$ two ways.  The first way is generatively, by first sampling $\by | \mathbf{K}$, then sampling $\bt | \by, \beta$ (Eqns 6.60 followed by 6.59).  The second way is to integrate over $\by$ (the mean draw) and directly sample $\bt | \mathbf{K}, \beta$ using Eqn 6.61.    This is the generative process for $\bt$.  Note that we have not specified a distribution over inputs $\bx$;  this is because Gaussian processes are conditional models.  Because of this we are free to generate locations $\bx$ when playing around with the GP; obviously a dataset will give us input-output pairs.
Once we have data, we are interested in the predictive distribution (note: the prior is the predictive distribution when there is no data).  Consider the joint distribution for $N+1$ targets, given by Eqn 6.64.  Its covariance matrix is composed of block components $C_N$, $\mathbf{k}$, and $c$.  The covariance matrix $C_N$ for $\bt_N$ is $C_N = \mathbf{K}_N + \beta^{-1}\mathbf{I}_N$.  We have just made explicit the size $N$ of the matrix; $N$ is the number of training points.  The kernel vector $\mathbf{k}$ is a $N$ by $1$ vector of kernel function evaluations between the training input data and the test input vector.  The scalar $c$ is a kernel evaluation at the test input.
2.1 gp_predictive_distribution(...) (10 points)
Write a function gp_predictive_distribution(x_train, t_train, x_test, theta, beta, C=None) that computes  Eqns 6.66 and 6.67, except allow for an arbitrary number of test points (not just one) and now the kernel matrix is for training data.  By having C as an optional parameter, we can avoid computing it more than once (for this problem it is unimportant, but for real problems this is an issue).  The function should compute $\mathbf{C}$, $\mathbf{k}$, and return the mean, variance and $\mathbf{C}$.  Do not forget: the computeK function computes $\mathbf{K}$, not $\mathbf{C}$! (10 points)
Can you write Python code for it?

def computeC(x1, x2, theta, beta):
    K = computeK(x1, x2, theta)
    return K + np.diag(np.array([1/beta for x in x1]))

def gp_predictive_distribution(x_train, t_train, x_test, theta, beta, C=None):
    # Calculate or reuse C
    if C is None:
        C = computeC(x_train, x_train, theta, beta)

    # Calculate mean and variance
    c = computeC(x_test, x_test, theta, beta)
    K = computeK(x_train, x_test, theta)
    KC = np.matmul(np.linalg.inv(C), K)
    mean_test = np.asarray(np.matrix(t_train) * KC)
    var_test = c - np.matmul(KC.T, K)
    return mean_test.squeeze(), var_test.squeeze(), C

### Test your function
N = 2
train_x = np.linspace(-1, 1, N)
train_t = 2*train_x
test_N = 3
test_x = np.linspace(-1, 1, test_N)
theta = [1, 2, 3, 4]
beta = 25
test_mean, test_var, C = gp_predictive_distribution(train_x, train_t, test_x, theta, beta, C=None)

assert test_mean.shape == (test_N,), "the shape of mean is incorrect"
assert test_var.shape == (test_N, test_N), "the shape of var is incorrect"
assert C.shape == (N, N), "the shape of C is incorrect"

C_in = np.array([[0.804, -0.098168436], [-0.098168436, 0.804]])
_, _, C_out = gp_predictive_distribution(train_x, train_t, test_x, theta, beta, C=C_in)

assert np.allclose(C_in, C_out), "C is not reused!"


I am working on the file "ML1/lab3_verna.ipynb".
The first task is:
2.2 Finding the support vectors (15 points)
Finally we going to use a SVM to obtain the decision boundary for which the margin is maximized. We have to solve the optimization problem
\begin{align}
\arg \min_{\bw, b} \frac{1}{2} \lVert \bw \rVert^2,
\end{align}
subject to the constraints
\begin{align}
t_n(\bw^T \phi(\bx_n) + b) \geq 1, n = 1,...,N.
\end{align}
In order to solve this constrained optimization problem, we introduce Lagrange multipliers $a_n \geq 0$. We obtain the dual
representation of the maximum margin problem in which we maximize
\begin{align}
\sum_{n=1}^N a_n - \frac{1}{2}\sum_{n=1}^N\sum_{m=1}^N a_n a_m t_n t_m k(\bx_n, \bx_m),
\end{align}
with respect to a subject to the constraints
\begin{align}
a_n &\geq 0, n=1,...,N,\
\sum_{n=1}^N a_n t_n &= 0.
\end{align}
This takes the form of a quadratic programming problem in which we optimize a quadratic function of a subject to a set of inequality constraints.
a) (5 points) In this example we will use a linear kernel $k(\bx, \bx') = \bx^T\bx'$. Write a function computeK(X) that computes the kernel matrix $K$ for the 2D dataset $X$.
Can you write Python code for it?

def computeK(X):
    # YOUR CODE HERE
    # raise NotImplementedError()
    K = np.dot(X, X.T).astype('float')
    return K

dim = 2
N_test = 3
X_test = np.arange(6).reshape((N_test, dim))
K_test = computeK(X_test)

assert K_test.shape == (N_test, N_test)


I am working on the file "ML1/lab3_verna.ipynb".
The first task is:
2.3 Plot support vectors (5 points)
Now that we have obtained the lagrangian multipliers $\ba$, we use them to find our support vectors. Repeat the plot from 2.1, this time use a third color to indicate which samples are the support vectors.
Can you write Python code for it?

# YOUR CODE HERE
# raise NotImplementedError()
np.random.seed(420)

X, t = create_X_and_t(X1, X2)
a_opt = compute_multipliers(X, t)

sv_ind = np.nonzero(np.around(a_opt[:, 0]))

X_sv = X[sv_ind]
t_sv = t[sv_ind]
a_sv = a_opt[sv_ind]

plt.figure()
plt.axis('equal')
plt.scatter(X1[:, 0], X1[:, 1], c='b', marker='o')
plt.scatter(X2[:, 0], X2[:, 1], c='r', marker='o')
plt.scatter(X_sv[:, 0], X_sv[:, 1], s=200, facecolors='none', edgecolors='lime', linewidth='3')
plt.show()


I am working on the file "ML1/lab3_verna.ipynb".
The first task is:
2.4 More plotting (5 points)
Repeat the 6 plots above, but this time conditioned a new set of 10 training points. (5 points)
Can you write Python code for it?

# Number of data points
n = 10

plot_conditioned_on_training(n)


I am working on the file "ML1/lab3_verna.ipynb".
The first task is:
2.4 Plot the decision boundary (10 Points)
The decision boundary is fully specified by a (usually very small) subset of training samples, the support vectors. Make use of
\begin{align}
\bw &= \sum_{n=1}^N a_n t_n \mathbf{\phi}(\bx_n)\
b &= \frac{1}{N_S}\sum_{n \in S} (t_n - \sum_{m \in S} a_m t_m k(\bx_n, \bx_m)),
\end{align}
where $S$ denotes the set of indices of the support vectors, to calculate the slope and intercept of the decision boundary. Generate a last plot that contains the two subsets, support vectors and decision boundary.
Can you write Python code for it?


I am working on the file "ML1/lab3_verna.ipynb".
The first task is:
Part 2: Support Vector Machines (45 points)
As seen in Part 1: Gaussian Processes, one of the significant limitations of many such algorithms is that the kernel function $k(\bx_n , \bx_m)$ must be evaluated for all possible pairs $\bx_n$ and $\bx_m$ of training points, which can be computationally infeasible during training and can lead to excessive computation times when making predictions for new data points.
In Part 2: Support Vector Machines, we shall look at kernel-based algorithms that have sparse solutions, so that predictions for new inputs depend only on the kernel function evaluated at a subset of the training data points.
2.1 Generating a linearly separable dataset (15 points)
a) (5 points) First of all, we are going to create our own 2D toy dataset $X$. The dataset will consists of two i.i.d. subsets $X_1$ and $X_2$, each of the subsets will be sampled from a multivariate Gaussian distribution,
\begin{align}
X_1 \sim &\mathcal{N}(\mu_1, \Sigma_1)\
&\text{ and }\
X_2 \sim &\mathcal{N}(\mu_2, \Sigma_2).
\end{align}
In the following, $X_1$ will have $N_1=20$ samples and a mean $\mu_1=(1,1)$. $X_2$ will have $N_2=30$ samples and a mean $\mu_2=(3,3)$.
Plot the two subsets in one figure, choose two colors to indicate which sample belongs to which subset. In addition you should choose, $\Sigma_1$ and $\Sigma_2$ in a way that the two subsets become linearly separable. (Hint: Which form has the covariance matrix for a i.i.d. dataset?)
Can you write Python code for it?


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
1. Sampling from the Gaussian process prior (30 points)
We will implement Gaussian process regression using the kernel function in Bishop Eqn. 6.63.
1.1 k_n_m( xn, xm, thetas ) (5 points)
To start, implement function k_n_m(xn, xm, thetas) that takes scalars $x_n$ and $x_m$, and a vector of $4$ thetas, and computes the kernel function Bishop Eqn. 6.63 (10 points).  NB: usually the kernel function will take $D$ by $1$ vectors, but since we are using a univariate problem, this makes things easier.
Can you write Python code for it?

def k_n_m(xn, xm, thetas):
    # YOUR CODE HERE
    raise NotImplementedError()

    return k


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
1.2 computeK( X1, X2, thetas ) (10 points)
Eqn 6.60 is the marginal distribution of mean output of $N$ data vectors: $p(\mathbf{y}) = \mathcal{N}(0, \mathbf{K})$.  Notice that the expected mean function is $0$ at all locations, and that the covariance is a $N$ by $N$ kernel matrix $\mathbf{K}$.  Write a function computeK(x1, x2, thetas)
that computes the kernel matrix. Use k_n_m as part of an inner loop (of course, there are more efficient ways of computing the kernel function making better use of vectorization, but that is not necessary) (5 points).
Can you write Python code for it?

def computeK(x1, x2, thetas):
    # YOUR CODE HERE
    raise NotImplementedError()

    return K

### Test your function
x1 = [0, 1, 2]
x2 = [1, 2, 3, 4]
thetas = [1, 2, 3, 4]
K = computeK(x1, x2, thetas)

assert K.shape == (len(x1), len(x2)), "the shape of K is incorrect"


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
1.3 Plot function samples (15 points)
Now sample mean functions at the x_test locations for the theta values in Bishop Figure 6.5, make a figure with a 2 by 3 subplot and make sure the title reflects the theta values (make sure everything is legible).  In other words, sample $\by_i \sim \mathcal{N}(0, \mathbf{K}_{\theta})$.  Make use of numpy.random.multivariate_normal().  On your plots include the expected value of $\by$ with a dashed line and fill_between 2 standard deviations of the uncertainty due to $\mathbf{K}$ (the diagonal of $\mathbf{K}$ is the variance of the model uncertainty) (15 points).
Can you write Python code for it?

# YOUR CODE HERE
raise NotImplementedError()


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
2.2 gp_log_likelihood(...) (10 points)
To learn the hyperparameters, we would need to compute the log-likelihood of the of the training data.  Implicitly, this is conditioned on the value setting for $\mathbf{\theta}$.  Write a function gp_log_likelihood(x_train, t_train, theta, C=None, invC=None, beta=None), where C and invC can be stored and reused. It should return the log-likelihood, C and invC  (10 points)
Can you write Python code for it?

def gp_log_likelihood(x_train, t_train, theta, beta, C=None, invC=None):
    # YOUR CODE HERE
    raise NotImplementedError()

    return lp, C, invC

### Test your function
N = 2
train_x = np.linspace(-1, 1, N)
train_t = 2 * train_x
theta = [1, 2, 3, 4]
beta = 25
lp, C, invC = gp_log_likelihood(train_x, train_t, theta, beta, C=None, invC=None)

assert lp < 0, "the log-likelihood should smaller than 0"
assert C.shape == (N, N), "the shape of var is incorrect"
assert invC.shape == (N, N), "the shape of C is incorrect"

C_in = np.array([[0.804, -0.098168436], [-0.098168436, 0.804]])
_, C_out, _ = gp_log_likelihood(train_x, train_t, theta, beta, C=C_in, invC=None)

assert np.allclose(C_in, C_out), "C is not reused!"

invC_in = np.array([[1.26260453, 0.15416407], [0.15416407, 1.26260453]])
_, _, invC_out = gp_log_likelihood(train_x, train_t, theta, beta, C=None, invC=invC_in)

assert np.allclose(invC_in, invC_out), "invC is not reused!"


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
2.3 Plotting (10 points)
Repeat the 6 plots above, but this time conditioned on the training points.  Use the periodic data generator to create 2 training points where x is sampled uniformly between $-1$ and $1$.  For these plots, feel free to use the provided function "gp_plot".  Make sure you put the parameters in the title and this time also the log-likelihood. Try to understand the two types of uncertainty!  If you do not use gp_plot(...), please add a fill between for the model and target noise. (10 points)
Can you write Python code for it?

def gp_plot( x_test, y_test, mean_test, var_test, x_train, t_train, theta, beta ):
    # x_test:
    # y_test:    the true function at x_test
    # mean_test: predictive mean at x_test
    # var_test:  predictive covariance at x_test
    # t_train:   the training values
    # theta:     the kernel parameters
    # beta:      the precision (known)

    # the reason for the manipulation is to allow plots separating model and data stddevs.
    std_total = np.sqrt(np.diag(var_test))       # includes all uncertainty, model and target noise
    std_model = np.sqrt(std_total**2 - 1.0/beta) # remove data noise to get model uncertainty in stddev
    std_combo = std_model + np.sqrt(1.0/beta)    # add stddev (note: not the same as full)

    plt.plot(x_test, y_test, 'b', lw=3)
    plt.plot(x_test, mean_test, 'k--', lw=2)
    plt.fill_between(x_test, mean_test+2*std_combo,mean_test-2*std_combo, color='k', alpha=0.25)
    plt.fill_between(x_test, mean_test+2*std_model,mean_test-2*std_model, color='r', alpha=0.25)
    plt.plot(x_train, t_train, 'ro', ms=10)

# YOUR CODE HERE
raise NotImplementedError()


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
2.4 More plotting (5 points)
Repeat the 6 plots above, but this time conditioned a new set of 10 training points. (5 points)
Can you write Python code for it?


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
2.4 Plot the decision boundary (10 Points)
The decision boundary is fully specified by a (usually very small) subset of training samples, the support vectors. Make use of
\begin{align}
\bw &= \sum_{n=1}^N a_n t_n \mathbf{\phi}(\bx_n)\
b &= \frac{1}{N_S}\sum_{n \in S} (t_n - \sum_{m \in S} a_m t_m k(\bx_n, \bx_m)),
\end{align}
where $S$ denotes the set of indices of the support vectors, to calculate the slope and intercept of the decision boundary. Generate a last plot that contains the two subsets, support vectors and decision boundary.
Can you write Python code for it?


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
Lab 3: Gaussian Processes and Support Vector Machines
Machine Learning 1, September 2017
Notes on implementation:

You should write your code and answers in this IPython Notebook: http://ipython.org/notebook.html. If you have problems, please contact your teaching assistant.
Please write your answers right below the questions.
Among the first lines of your notebook should be "%pylab inline". This imports all required modules, and your plots will appear inline.
Refer to last week's lab notes, i.e. http://docs.scipy.org/doc/, if you are unsure about what function to use. There are different correct ways to implement each problem!
use the provided test boxes to check if your answers are correct
Can you write Python code for it?

%pylab inline
plt.rcParams["figure.figsize"] = [20,10]


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
Next, we will rewrite the dual representation so that we can make use of computationally efficient vector-matrix multiplication. The objective becomes
\begin{align}
\min_{\ba} \frac{1}{2} \ba^T K' \ba - 1^T\ba,
\end{align}
subject to the constraints
\begin{align}
a_n &\geq 0, n=1,...,N,\
\bt^T\ba &= 0.
\end{align}
Where
\begin{align}
K'{nm} = t_n t_m k(\bx_n, \bx_m),
\end{align}
and in the special case of a linear kernel function,
\begin{align}
K'{nm} = t_n t_m k(\bx_n, \bx_m) = k(t_n \bx_n, t_m \bx_m).
\end{align}
To solve the quadratic programming problem we will use a python module called cvxopt. You first have to install the module in your virtual environment (you have to activate it first), using the following command:
conda install -c conda-forge cvxopt
The quadratic programming solver can be called as
cvxopt.solvers.qp(P, q[, G, h[, A, b[, solver[, initvals]]]])
This solves the following problem,
\begin{align}
\min_{\bx} \frac{1}{2} \bx^T P \bx + q^T\bx,
\end{align}
subject to the constraints,
\begin{align}
G\bx &\leq h,\
A\bx &= b.
\end{align}
All we need to do is to map our formulation to the cvxopt interface.
b) (10 points) Write a function compute_multipliers(X, t) that solves the quadratic programming problem using the cvxopt module and returns the lagrangian multiplier for every sample in the dataset.
Can you write Python code for it?


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
Part 1: Gaussian Processes
For part 1 we will be refer to Bishop sections 6.4.2 and 6.4.3. You may also want to refer to Rasmussen's Gaussian Process text which is available online at http://www.gaussianprocess.org/gpml/chapters/ and especially to the project found at https://www.automaticstatistician.com/index/ by Ghahramani for some intuition in GP.  To understand Gaussian processes, it is highly recommended understand how marginal, partitioned Gaussian distributions can be converted into conditional Gaussian distributions.  This is covered in Bishop 2.3 and summarized in Eqns 2.94-2.98.
$\newcommand{\bt}{\mathbf{t}}$
$\newcommand{\bx}{\mathbf{x}}$
$\newcommand{\by}{\mathbf{y}}$
$\newcommand{\bw}{\mathbf{w}}$
$\newcommand{\ba}{\mathbf{a}}$
Periodic Data
We will use the same data generating function that we used previously for regression.
Can you write Python code for it?


I am working on the file "ML1/lab3_yves.ipynb".
The first task is:
Part 2: Support Vector Machines (45 points)
As seen in Part 1: Gaussian Processes, one of the significant limitations of many such algorithms is that the kernel function $k(\bx_n , \bx_m)$ must be evaluated for all possible pairs $\bx_n$ and $\bx_m$ of training points, which can be computationally infeasible during training and can lead to excessive computation times when making predictions for new data points.
In Part 2: Support Vector Machines, we shall look at kernel-based algorithms that have sparse solutions, so that predictions for new inputs depend only on the kernel function evaluated at a subset of the training data points.
2.1 Generating a linearly separable dataset (15 points)
a) (5 points) First of all, we are going to create our own 2D toy dataset $X$. The dataset will consists of two i.i.d. subsets $X_1$ and $X_2$, each of the subsets will be sampled from a multivariate Gaussian distribution,
\begin{align}
X_1 \sim &\mathcal{N}(\mu_1, \Sigma_1)\
&\text{ and }\
X_2 \sim &\mathcal{N}(\mu_2, \Sigma_2).
\end{align}
In the following, $X_1$ will have $N_1=20$ samples and a mean $\mu_1=(1,1)$. $X_2$ will have $N_2=30$ samples and a mean $\mu_2=(3,3)$.
Plot the two subsets in one figure, choose two colors to indicate which sample belongs to which subset. In addition you should choose, $\Sigma_1$ and $\Sigma_2$ in a way that the two subsets become linearly separable. (Hint: Which form has the covariance matrix for a i.i.d. dataset?)
Can you write Python code for it?


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
CV might take some time
Can you write Python code for it?

from sklearn.cross_validation import cross_val_score
cv_gini = cross_val_score(DecisionTreeClassifier(criterion='gini'),
                hits_train[['energy_deposit', 'relative_time']].values, (hits_train.label == 1).values.astype(np.int),
               scoring='roc_auc')
print(cv_gini.mean(), cv_gini.std())

cv_entropy = cross_val_score(DecisionTreeClassifier(criterion='entropy'),
                hits_train[['energy_deposit', 'relative_time']].values, (hits_train.label == 1).values.astype(np.int),
                scoring='roc_auc')
print(cv_entropy.mean(), cv_entropy.std())

classifier = DecisionTreeClassifier(criterion='gini')
classifier.fit(hits_train[['energy_deposit', 'relative_time']], (hits_train.label == 1))

candidates = hits_test.loc[hits_test.energy_deposit > 0]
ml_prediction = pd.DataFrame({
        "prediction": classifier.predict_proba(candidates[[
                    'energy_deposit', 'relative_time']])[:, 1]}, index=candidates.index)

ml_prediction.to_csv("naive_ml_prediction.csv", index_label='global_id')


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
Check how good the model describes the data.
Can you write Python code for it?

from sklearn.metrics import roc_auc_score
hits_train_is_signal = (hits_train.label == 1)
roc_auc_score(hits_train_is_signal, is_signal(hits_train))


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
It can't! So far so good.
Can you write Python code for it?

candidates = hits_train.loc[(hits_train.energy_deposit > 0)]


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
Let's make a predicion for submission. Take note at the format: only the events with positive energy.
Can you write Python code for it?

prediction = pd.DataFrame({"prediction": is_signal(hits_test.loc[hits_test.energy_deposit > 0]).astype(np.int)})

prediction.to_csv("naive_manual_prediction.csv", index_label='global_id')


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
Looks like we could use a selection rule.
Can you write Python code for it?

fig, ax = plt.subplots()
ax.scatter(np.log(noise_hits.energy_deposit)**2, noise_hits.relative_time**2, c='b', edgecolors='none', alpha=0.3)
ax.scatter(np.log(signal_hits.energy_deposit)**2, signal_hits.relative_time**2, c='r', edgecolors='none')
high_relative_time = 1.35e6
low_relative_time = 256300
low_points = np.array([[160, 0], [194, low_relative_time], [229, low_relative_time], [200, 0]])
high_points = np.array([[164, 1.4e6], [195, 1.4e6], [195, high_relative_time], [164, high_relative_time],
                        [164, 1.4e6]])
ax.plot(low_points[:, 0], low_points[:, 1], 'g', lw=3)
ax.plot(high_points[:, 0], high_points[:, 1], 'g', lw=3)
ax.set_xlabel(r"$\log(\mathrm{energy\_deposit})^2$")
ax.set_ylabel(r"$\mathrm{relative\_time}^2$")

top_line_coeffs = np.polyfit(low_points[0:2, 0], low_points[0:2, 1], deg=1)
bottom_line_coeffs = np.polyfit(low_points[2:4, 0], low_points[2:4, 1], deg=1)
def is_signal(event):
    log_energy_squared = np.log(event.energy_deposit)**2
    relative_time_squared = event.relative_time**2
    return (((relative_time_squared < low_relative_time) & (
            relative_time_squared < np.poly1d(top_line_coeffs)(log_energy_squared)) & (
                relative_time_squared > np.poly1d(bottom_line_coeffs)(log_energy_squared))) |
            ((relative_time_squared > high_relative_time) &
             (log_energy_squared > 164) & (log_energy_squared < 195)))


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
Moral: sometimes you can outdo simple machine learning by thinking. Corollary: the best result is achieved by combining the approaches.
Can you write Python code for it?

FileLink("naive_ml_prediction.csv")

from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
rf_classifier = RandomForestClassifier(criterion='entropy')

rf_classifier.fit(hits_train[['energy_deposit', 'relative_time']], (hits_train.label == 1))

rf_candidates = hits_test.loc[hits_test.energy_deposit > 0]
rf_ml_prediction = pd.DataFrame({
        "prediction": rf_classifier.predict_proba(rf_candidates[[
                    'energy_deposit', 'relative_time']])[:, 1]}, index=rf_candidates.index)

rf_ml_prediction.to_csv("rf_ml_prediction.csv", index_label='global_id')

FileLink("rf_ml_prediction.csv")


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
Naive machine learning
Can you write Python code for it?

from sklearn.tree import DecisionTreeClassifier


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
Naive manual analysis
Obviously a not-so-good algorithm, used primaraly for illustrating IPython
First, check whether a a signal wire can have energy_deposit = 0
Can you write Python code for it?


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
The data from Kaggle is already here in the "data" folder. Let's take a look at it.
Can you write Python code for it?

hits_train = pd.read_csv("data/train.csv", index_col='global_id')
hits_train.head()

hits_test = pd.read_csv("data/test.csv", index_col='global_id')
hits_test.head()


! pwd

hits_train = pd.read_csv("mlhep2015_starterkit/data/train.csv", index_col='global_id')
hits_train.head()

hits_test = pd.read_csv("mlhep2015_starterkit/data/test.csv", index_col='global_id')
hits_test.head()


I am working on the file "MLHEP 2015 starterkit.ipynb".
The first task is:
Try plotting time vs. energy vs. label. It's too big, so we'll take a sample.
Can you write Python code for it?

plot_sample_indices = np.random.choice(np.arange(len(candidates)), size=50000)
hits_to_plot = candidates.iloc[plot_sample_indices]

fig, ax = plt.subplots()
signal_hits = hits_to_plot.loc[(hits_to_plot.label == 1)]
noise_hits = hits_to_plot.loc[(hits_to_plot.label == 2)]
ax.scatter(noise_hits.energy_deposit, noise_hits.relative_time, c='b', edgecolors='none', alpha=0.3)
ax.scatter(signal_hits.energy_deposit, signal_hits.relative_time, c='r', edgecolors='none')
ax.set_xscale('log')
ax.set_xlim(1e-9, 1e-2)
ax.set_xlabel("energy_deposit")
ax.set_ylabel("relative_time")


I am working on the file "MLP_final_test/MLP_from_data.ipynb".
The first task is:
Artificial Neural Network (Gridsearch, DO NOT RUN)
Can you write Python code for it?

#Grid search, random state =0: same beginning for all
alpha1 = np.linspace(0.001,0.9, 9).tolist()
momentum1 = np.linspace(0.3,0.9, 9).tolist()
params_dist = {"hidden_layer_sizes":[(20, 40), (15, 40), (10,15), (15, 15, 10), (15, 10), (15, 5)],
               "activation":['tanh','logistic'],"algorithm":['sgd', 'l-bfgs'], "alpha":alpha1,
               "learning_rate":['constant'],"max_iter":[500], "random_state":[0],
               "verbose": [False], "warm_start":[False], "momentum":momentum1}
grid = GridSearchCV(MLPRegressor(), param_grid=params_dist)
grid.fit(X_train_std, y_train)

print "Best score:", grid.best_score_
print "Best parameter's set found:\n"
print grid.best_params_

reg = MLPRegressor(warm_start = grid.best_params_['warm_start'], verbose= grid.best_params_['verbose'],
                   algorithm= grid.best_params_['algorithm'],hidden_layer_sizes=grid.best_params_['hidden_layer_sizes'],
                   activation= grid.best_params_['activation'], max_iter= grid.best_params_['max_iter'],
                   random_state= None,alpha= grid.best_params_['alpha'], learning_rate= grid.best_params_['learning_rate'],
                   momentum= grid.best_params_['momentum'])

reg.fit(X_train_std, y_train)


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
Again, not bad for the number of trials.
TODO

Extend the problem by letting the bump position vary.
Make brazil plot
Can you write Python code for it?

def find_bestfit(data):
    N = 20
    bkgs = np.linspace(0, 2000, N)
    sigs = np.linspace(0, 2000, N)
    pos  = np.linspace(0, 100, N)

    points = np.array(np.meshgrid(bkgs, sigs, pos)).T.reshape(-1,3)
    ts = list(map(lambda x: -2*lnLike(x[0], x[1], data, mu=x[2]), points))
    start = points[np.argmin(ts),:]

    return minimize(lambda x: -2*lnLike(x[0], x[1], data, mu=x[2]), start)

def TS_pos(sig_norm, sig_pos, data, bestfit=None):
    numerator = minimize(lambda b: -2*lnLike(b, sig_norm, data, mu=sig_pos), 1e3)
    if not bestfit:
        bestfit = minimize(lambda x: -2*lnLike(x[0], x[1], data, mu=x[2]),
                           (1e3, 1e3, 1e1))
    return numerator.fun - bestfit.fun

def visualise_TS_pos(data, signormlim=(500, 1500), sigposlim=(1,100), ax=None, title=None):
    if ax is None:
        fig, ax = plt.subplots()

    def starting_point(data):
        N = 20
        bkgs = np.linspace(0, 2000, N)
        sigs = np.linspace(0, 2000, N)
        pos  = np.linspace(0, 100, N)

        points = np.array(np.meshgrid(bkgs, sigs, pos)).T.reshape(-1,3)
        ts = list(map(lambda x: -2*lnLike(x[0], x[1], data, mu=x[2]), points))
        return points[np.argmin(ts),:]

    bestfit = minimize(lambda x: -2*lnLike(x[0], x[1], data, mu=x[2]), starting_point(data))
    print(bestfit)
    sig_pos = np.logspace(*np.log10(sigposlim), 50)
    sig_norms = np.linspace(*signormlim, 100)
    ts = [[TS_pos(n, p, data, bestfit=bestfit) for n in sig_norms] for p in sig_pos]
    X, Y = np.meshgrid(sig_pos, sig_norms)
    Z = np.array(ts).T

    r = ax.contourf(X, Y, Z, 100, cmap='Blues_r')
    plt.colorbar(r, label='TS', ax=ax)
    ax.contour(X, Y, Z, colors='white', levels=[5.991])

    ax.set_xlim(*sigposlim)
    ax.set_ylim(*signormlim)
    ax.set_xscale('log')
    ax.set_xlabel('Signal Position')
    ax.set_ylabel('Signal Normalisation')
    ax.set_title(title)

fig, axes = plt.subplots(ncols=3, nrows=2, figsize=(14,8))
for i, p in enumerate([3, 10, 20]):
    d =...


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
As we can see, in this case the empirical distribution is well-described by a $\chi_{k=1}^2$ distribution.
Upper Limits
We've seen how the the maximum likelihood test statistic leads to two sided intervals, but our aim is to do exclusion limits. We modify our test statistic as follows.
$$\mathrm{TS}(\theta_s) =
\begin{cases}
  \mathrm{TS}(\theta_s) & \quad \theta_s \geq \hat{\theta_s}\
  0 & \quad \text{elsewise}\
\end{cases}
$$
This is for a upper limit on $\theta_s$.
Can you write Python code for it?

def TS_upper_limit(sig_norm, data, bestfit=None):
    if not bestfit:
        bestfit = minimize(lambda x: -2*lnLike(x[0], x[1], data), (1e3,1e3))

    if sig_norm < bestfit.x[1]:
        return 0.0
    else:
        return TS_sig(sig_norm, data, bestfit=bestfit)

bestfit = minimize(lambda x: -2*lnLike(x[0], x[1], data_small_sig), (1e3,1e3))
x = np.linspace(-100, 100, 30)
y = [TS_sig(s, data_small_sig, bestfit=bestfit) for s in x]

plt.plot(x, y);
y = [TS_upper_limit(s, data_small_sig, bestfit=bestfit) for s in x]
plt.plot(x, y)
plt.xlabel('Signal Normalisation')
plt.ylabel('TS');


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
Check coverage
To check if our procedure work we will do a 100 experiments and see if the true value are covered at the correct frequency, e.g. 68ish times if we want 68% CL.
Can you write Python code for it?

def coverage_check(sig_norm, CL=0.68, bkg_norm=1000, n=100):
    covered = 0
    for _ in range(n):
        d = generate_data(bkg_norm, sig_norm)
        l, u = confidence_interval(d, CL=CL)
        if l < sig_norm and u > sig_norm:
            covered += 1

    return covered/n

print('Coverage small signal: %.3f' % coverage_check(10))
print('Coverage large signal: %.3f' % coverage_check(600))


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
Check coverage for the upper limit
We perform a multitude of 'experiments' and investigate if the upper limit covers the true value as often as it should.
Can you write Python code for it?

def coverage_check_ul(sig_norm, CL=0.90, bkg_norm=1000, n=100):
    upper_limits = []
    for _ in range(n):
        d = generate_data(bkg_norm, sig_norm)
        upper_limits.append(upper_limit(d, CL=CL))

    upper_limits = np.array(upper_limits)
    not_excluded = (upper_limits >= sig_norm).sum()

    return not_excluded/n

print('Coverage small signal: %.3f' % coverage_check_ul(10))
print('Coverage large signal: %.3f' % coverage_check_ul(600))


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
Not bad, with more experiments this should be better. Try it yourself!
Verifying that Wilk's theorem applies
We can verify that our test statistic is $\chi^2_{k=1}$ distributed by doing monte carlo experiments. We can then compare the empirical distribution of the test statistic with the chi-squared one.
Can you write Python code for it?

def mc(sig_norm, bkg_norm=1000, n=100):
    ts = []
    for _ in range(n):
        d = generate_data(bkg_norm, sig_norm)
        bf = minimize(lambda x: -2*lnLike(x[0], x[1], d), (1e3,1e3))
        ts.append(minimize(lambda s: TS_sig(s, d, bestfit=bf), sig_norm).fun)

    return np.array(ts)

mc_small_signal = mc(sig_norm=10)

x = np.linspace(np.min(mc_small_signal), np.max(mc_small_signal), 100)
plt.hist(mc_small_signal, bins=20, normed=True, alpha=0.5, label='MC')
plt.plot(x, chi2.pdf(x, df=1), lw=4, label='chi2 df=1')
plt.legend(frameon=False)
plt.xlabel('TS');


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
Our test statistic of choice is the logarithm of the maximum likelihood ratio which we defined as
$$ \mathrm{TS}(\theta) = -2 \ln \frac{\max_{\nu} \mathcal{L}( \theta, \nu \,|\, D)}{\max_{\theta, \nu} \mathcal{L}( \theta, \nu \,|\, D)}$$
where $\theta$ are the parameter(s) we want to constrain, and $\nu$ are the remained. So if we wanted to constrain the normalisation of the signal $\theta_s$ the test statistic to consider is
$$ \mathrm{TS}(\theta_s) = -2 \ln \frac{\max_{\theta_b} \mathcal{L}( \theta_s, \theta_b \,|\, D)}{\max_{\theta_s, \theta_b} \mathcal{L}( \theta_s, \theta_b \,|\, D)}$$
Can you write Python code for it?

def TS_sig(sig_norm, data, bestfit=None):
    numerator = minimize(lambda b: -2*lnLike(b, sig_norm, data), 1e3)
    if not bestfit:
        bestfit = minimize(lambda x: -2*lnLike(x[0], x[1], data), (1e3,1e3))
    return numerator.fun - bestfit.fun


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
We can now use a root finder to find for which $\theta_s$ our new TS has this threshold value, and this is our upper limit!
Can you write Python code for it?

def upper_limit(data, bestfit=None, CL=0.90):
    if not bestfit:
        bestfit = minimize(lambda x: -2*lnLike(x[0], x[1], data), (1e3,1e3))
    threshold = threshold_ul(CL)
    return brentq(lambda x: TS_upper_limit(x, data, bestfit=bestfit)-threshold,
                  -1000, 1000)

print('90%% CL upper limit for small signal: %.2f' % upper_limit(data_small_sig))
print('90%% CL upper limit for large signal: %.2f' % upper_limit(data_large_sig))


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
We need something to place a limit on, and in our first example we are looking for a bump in an otherwise smooth background. This could be a gamma-ray line from dark matter annihilation, or maybe a particle resonance at the LHC.
In this first toy example we keep the shape of the background, the width, and position of the signal bump are all fixed. The two parameters are then the normalisations of the signal bump and the smooth background.
Futhermore we define the data to be a measured number of events in $n=30$ energy bins, and define a data generating function with the signal spectra being a Gaussian at $E = 10$ and with a variance $\sigma^2 = 2$. The background is a power law with an index $-1$.
Can you write Python code for it?

E = np.logspace(0,2,30+1)
E = (E[1:] + E[:-1]) / 2 # bin centers

def signal_shape(E=E, mu=10, var=2):
    return norm.pdf(E, mu, var)

def background_shape(E=E, gamma=-1):
    return E**gamma

def generate_data(bkg_norm, sig_norm, sig_mu=10, sig_var=2, E=E, seed=None):
    np.random.seed(seed)
    return np.random.poisson(sig_norm * signal_shape(E, mu=sig_mu, var=sig_var) +
                             bkg_norm * background_shape(E))


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
Where the black x marks the true parameter values of each of the data sets. We see that the test statistic is small in the region of the true values, not perfectly so as we have statistical noise.
We can also consider the full 2D case where
$$ \mathrm{TS}(\theta_s, \theta_b) = -2 \ln \frac{ \mathcal{L}( \theta_s, \theta_b \,|\, D)}{\max_{\theta_s, \theta_b} \mathcal{L}( \theta_s, \theta_b \,|\, D)}$$
which is simply the likelihood normalised to it's maximum.
Can you write Python code for it?

def TS_2d(bkg_norm, sig_norm, data, bestfit=None):
    numerator = -2*lnLike(bkg_norm, sig_norm, data)
    if not bestfit:
        bestfit = minimize(lambda x: -2*lnLike(x[0], x[1], data), (1e3,1e3))
    return numerator - bestfit.fun

def visualise_TS_2d(data, siglim=(-100, 1000), bkglim=(800,1200), ax=None, title=None):
    if ax is None:
        fig, ax = plt.subplots()

    bestfit = minimize(lambda x: -2*lnLike(x[0], x[1], data), (1e3,1e3))

    bkg_norms = np.linspace(*bkglim, 100)
    sig_norms = np.linspace(*siglim, 100)
    ts = [[TS_2d(b, s, data, bestfit=bestfit) for s in sig_norms] for b in bkg_norms]
    X, Y = np.meshgrid(bkg_norms, sig_norms)
    Z = np.array(ts).T

    r = ax.contourf(X, Y, Z, 100, cmap='Blues_r')
    plt.colorbar(r, label='TS', ax=ax)
    ax.contour(X, Y, Z, colors='white', levels=[5.991])

    ax.set_xlim(*bkglim)
    ax.set_ylim(*siglim)

    ax.set_xlabel('Background Normalisation')
    ax.set_ylabel('Signal Normalisation')
    ax.set_title(title)

fig, axes = plt.subplots(ncols=2, figsize=(12,4))
visualise_TS_2d(data_small_sig, ax=axes[0], title='Small signal')
axes[0].scatter(1000, 10, color='white', marker='x')
visualise_TS_2d(data_large_sig, ax=axes[1], title='Large signal')
axes[1].scatter(1000, 600, color='white', marker='x');


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
With this we can now find the 68% CL intervals of $\theta_s$ for our two examples.
Can you write Python code for it?

def confidence_interval(data, CL=0.68, bestfit=None,
                        ts=TS_sig, threshold_fun=threshold_1d):
    if not bestfit:
        bestfit = minimize(lambda x: -2*lnLike(x[0], x[1], data), (1e3,1e3))

    threshold = threshold_fun(CL)

    # Simple way to find starting points for the root finder.
    # We need (a, b) for which TS-ts_threshold have different sign.
    step = 10+bestfit.x[1]/2
    u = bestfit.x[1] + step

    while ts(u, data, bestfit=bestfit) <= threshold:
        u += step

    # The TS tend to be symmetrical which we can use to do a better initial guess.
    l = 2*bestfit.x[1] - u
    while ts(l, data, bestfit=bestfit) <= threshold:
        l -= step

    upper_bound = brentq(lambda x: ts(x, data, bestfit=bestfit) - threshold,
                         bestfit.x[1], u)

    lower_bound = brentq(lambda x: ts(x, data, bestfit=bestfit) - threshold,
                         l, bestfit.x[1])

    return lower_bound, upper_bound

print(confidence_interval(data_small_sig))
print(confidence_interval(data_large_sig))


I am working on the file "MLR/An_Exclusive_Guide_to_Exclusion_Limits.ipynb".
The first task is:
where the white x marks the true parameter values of each of the data sets. We see that, as expected, the test statistic is smaller closer to the true value.
The white contour is simply us moving ahead of ourselves and plotting the $95.4$% confidence limit region using Wilks' theorem, i.e. by assuming that our TS is distributed as $\chi^2_{k=2}$. Ignore this for now.
Confidence Intervals
From the visualisation of the test statistics above we see that they are deep valleys close to the true values. This can be used to construct confidence intervals. The interval is simply
$${\, \theta \, | \, \mathrm{TS}(\theta) < c \, }$$
where $c$ is a threshold value such that we have the desired coverage. To determine $c$ we need to know how the test statistic is distributed under the null, i.e. when $\theta$ is the true value. If it applies, Wilks' theorem states that it's asymptotically chi-squared distributed with as many degrees of freedom $k$ as parameters of interest. So for 1D and 2D we can compute
Can you write Python code for it?

from functools import partial

def threshold(cl, cdf):
    return brentq(lambda x: cl-cdf(x), 0, 10)

threshold_1d = partial(threshold, cdf=partial(chi2.cdf, df=1))
threshold_2d = partial(threshold, cdf=partial(chi2.cdf, df=2))

print('68%% and 95%% thresholds for 1D: %.3f and %.3f'
      % tuple([threshold_1d(x) for x in [0.68, 0.95]]))
print('68%% and 95%% thresholds for 2D: %.3f and %.3f'
      % tuple([threshold_2d(x) for x in [0.68, 0.95]]))


I am working on the file "ML_SS2017/Tensorflow_mechanics_I.ipynb".
The first task is:
Constant, variables, placeholders
Constants:
tf.constant

tf.zeros
tf.zeros_like
tf.ones
tf.ones_like
tf.fill
tf.linspace
tf.range
Can you write Python code for it?

import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.reset_default_graph()

sess = tf.InteractiveSession(config=config)

a = tf.linspace(0.,11.,23)

b =  a*a
print(b.eval())

c = tf.constant([1,2,3.])

type(a)


I am working on the file "ML_SS2017/Tensorflow_mechanics_I.ipynb".
The first task is:
Random numbers
Can you write Python code for it?

n = tf.random_normal([2,2])

n.eval()

n = tf.random_uniform([12])

n.eval()

sess.close()


I am working on the file "ML_SS2017/Tensorflow_mechanics_I.ipynb".
The first task is:
Tensorboard
https://www.tensorflow.org/get_started/summaries_and_tensorboard
Uwaga - należy wybrać unikalny port: --port 3456
Can you write Python code for it?

!ls -l   /DATA/marcin.kostur/tf001/*

!tensorboard --logdir=/DATA/marcin.kostur/tf001/ --port 4001

tf.


I am working on the file "ML_SS2017/Tensorflow_mechanics_I.ipynb".
The first task is:
Variables
Can you write Python code for it?

x = tf.Variable([1,2,3.])

type(x)

x.initializer.run()
x.eval()

(a[:3]*x)

(a[:3]*x).eval()


I am working on the file "ML_SS2017/linear_regression_from_np_to_tf.ipynb".
The first task is:
Macierz $A$ dla regresji liniowej wynosi:
Can you write Python code for it?

import numpy as np
M = np.vstack([np.ones_like(train_X),train_X]).T
M

print (np.dot(M.T,M))

print(np.dot(M.T,train_Y))


I am working on the file "ML_SS2017/linear_regression_from_np_to_tf.ipynb".
The first task is:
Tensor flow - gradient descend
Can you write Python code for it?

# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")

# Set model weights
W = tf.Variable(1.0, name="weight")
b = tf.Variable(1.0, name="bias")

# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.global_variables_initializer()

# TEST
with tf.Session() as sess:
    sess.run(init)
    sess.run(tf.assign(W,1.0))
    sess.run(tf.assign(b,2.0))

    print(sess.run(b),sess.run(cost, feed_dict={X: train_X, Y: train_Y}))

# Launch the graph
x_tf_lst = []
y_tf_lst = []
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        #Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y:train_Y})
            print ("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "W=", sess.run(W), "b=", sess.run(b))

        x_tf_lst.append(sess.run(b))
        y_tf_lst.append(sess.run(W))

    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print ("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

plt.plot(x_tf_lst,y_tf_lst,'yo')


I am working on the file "ML_SS2017/linear_regression_from_np_to_tf.ipynb".
The first task is:
Współczynniki dokładnie będą wynosiły:
Can you write Python code for it?

c = np.linalg.solve(np.dot(M.T,M),np.dot(M.T,train_Y))
c

plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, c[1] * train_X + c[0], label='Fitted line')
plt.legend()

plt.close()


I am working on the file "ML_SS2017/mnist_1st.ipynb".
The first task is:
Budowa sieci (grafu obliczeniowego w tf)

obrazek reprezentowany jak wektor o długości 784.
budujemy jednowarstwową sieć
warstwa wyjściowa  to 10 percetpronów
używamy funkcji softmax na wyjsciu (zamiast np. max).
x jest placeholderem dla pewnej nieustalonej liczby obrazków
Can you write Python code for it?

x = tf.placeholder(tf.float32, [None, 784])
#W = tf.Variable(tf.zeros([784, 10]))
W = tf.Variable( tf.truncated_normal([784, 10], stddev=0.1,dtype=tf.float32) )
b = tf.Variable(tf.zeros([10]))


I am working on the file "ML_SS2017/mnist_1st.ipynb".
The first task is:
Dane MNIST
Dostęp do danych MNIST jest ułatwniony w tensorflow poprzez moduł tensorflow.examples.tutorials.mnist.
Można korzystać w sposób bezpośredni, albo przez next_batch, przykłady:
Can you write Python code for it?

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(local_dir, one_hot=True)

mnist.train.images.shape,mnist.test.images.shape,mnist.test.labels.shape

plt.matshow(mnist.train.images[1,:].reshape(28,28),cmap='gray')

mnist.train.labels[1]

mnist.train.next_batch(100)[1].shape

mnist.train.images[123:124,:].shape

mnist.train._num_examples

mnist.train.next_batch


I am working on the file "ML_SS2017/mnist_1st.ipynb".
The first task is:
Learning !

tf.global_variables_initializer().run() resetuje wszystkie W i b - jeśli chcemy kontynuować uczenie to trzeba to wyrzucić.
stosujemy Stochastic Gradient Descent dla 100 szt. danych w jednym kroku
Can you write Python code for it?

%%time
hst = []
hst2 = []
tf.global_variables_initializer().run()
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    hst2.append(  sess.run(cross_entropy, feed_dict={x: batch_xs, y_: batch_ys}))
    hst.append(  sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys}))

plt.plot(hst)
plt.plot(hst2,'r')
plt.axhline(1.0,color='black', linestyle='--')


I am working on the file "ML_SS2017/mnist_1st.ipynb".
The first task is:
Niech y_ będzie zawieram poprawą odpowiedź (label), jako funkcję kosztu możemy zastosować cross entropię lub zwykła normę.
Can you write Python code for it?

y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
norm =  tf.norm(y_ - y)

train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

config = tf.ConfigProto(log_device_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.05
sess = tf.InteractiveSession(config=config)
tf.global_variables_initializer().run()


I am working on the file "ML_SS2017/mnist_1st.ipynb".
The first task is:
Sieć jest definiowana jedną linią w tensorflow:
Jak działa softmax?
Can you write Python code for it?

y = tf.nn.softmax(tf.matmul(x, W) + b)
#y = tf.nn.sigmoid(tf.matmul(x, W) + b)
#y = tf.nn.relu(tf.matmul(x, W) + b)
#y = tf.matmul(x, W) + b


I am working on the file "ML_SS2017/mnist_conv.ipynb".
The first task is:
Analiza wyników
Możemy obejrzeć jakie błędy popełnia sieć.
Can you write Python code for it?

P = sess.run(correct_prediction,feed_dict={ x:mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
P.shape

Bad_x = mnist.test.images[np.where(P==False)[0],:]
Bad_x_truelabels = mnist.test.labels[np.where(P==False)[0],:]
Bad_x.shape

prediction =sess.run( tf.argmax(y_conv,1),
                          feed_dict={ x:Bad_x, y_: Bad_x_truelabels, keep_prob: 1.0})

prediction

Bad_x_true = np.argmax(Bad_x_truelabels,axis=1)

Bad_x.shape

Bad_x_true.shape

prediction.shape

#Imports for visualization
import PIL.Image
from io import BytesIO
from IPython.display import clear_output, Image, display
def DisplayArray(a, fmt='jpeg', rng=[0,1]):
    """Display an array as a picture."""
    a = (a - rng[0])/float(rng[1] - rng[0])*255
    a = np.uint8(np.clip(a, 0, 255))
    f = BytesIO()
    PIL.Image.fromarray(a).save(f, fmt)
    display(Image(data=f.getvalue()))

from time import sleep

for ith in range(Bad_x_true.shape[0]//20):
    print("zamiast ",Bad_x_true[ith], "sieć odczytała",prediction[ith],end="")
    DisplayArray( Bad_x[ith].reshape((28,28)) )
    #sleep(0.5)
    #clear_output(wait=True)


I am working on the file "ML_SS2017/mnist_conv.ipynb".
The first task is:
Test run
Pouczające jest prześledzenie jak zmienia się w trakcie przejścia przez sieć jeden obrazek:
Can you write Python code for it?

batch = mnist.train.next_batch(1)
print( batch[0].shape )

sess.run(x_image,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape

sess.run(h_conv1,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape

sess.run(h_pool1,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape

sess.run(h_conv2,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape

sess.run(h_pool2,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape

sess.run(h_pool2_flat,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape

sess.run(h_fc1_drop,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape

sess.run(y_conv,feed_dict={ x:batch[0], y_: batch[1], keep_prob: 1.0}).shape

%%time
sess.run(tf.global_variables_initializer())
for i in range(1000):
    batch = mnist.train.next_batch(150)
    if i%100 == 0:
        train_accuracy = accuracy.eval(feed_dict={ x:batch[0], y_: batch[1], keep_prob: 0.8})
        print("step %d, training accuracy %g"%(i, train_accuracy))

    train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

print("test accuracy %g"%accuracy.eval(feed_dict={
    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))


I am working on the file "ML_SS2017/numpy_rachunek_rozniczkowy.ipynb".
The first task is:
Funkcja pierwotna
$$ \int_{a}^{b} f(x)dx = F(b) - F(a) $$
$$F(x) = \int_{a}^{x} f(x')dx' $$
Pochodne funkcji wielu zmiennych
Can you write Python code for it?

import numpy as np
x = np.linspace(-2,1,40)
y = np.linspace(-2,3,34)
X,Y = np.meshgrid(x,y)

%matplotlib notebook
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

import sympy
from sympy.abc import x,y
sympy.init_printing(use_latex='mathjax')

from IPython.display import display

f_symb = -sympy.exp(-(x**2+2*y**2))
display(f_symb)

F = sympy.lambdify((x,y),f_symb,np)
Fx = sympy.lambdify((x,y),f_symb.diff(x),np)
Fy = sympy.lambdify((x,y),f_symb.diff(y),np)

fig = plt.figure()
ax = fig.gca(projection='3d')

#ax.plot_wireframe(X,Y,F)
ax.plot_surface(X, Y, F(X,Y), cmap=cm.coolwarm,rstride=1,cstride=1)
#ax.quiver3D(X, Y, Fx, Fy) #, cmap=cm.coolwarm,rstride=1,cstride=1)
ax.set_xlabel('X')
ax.set_ylabel('Y')

x0,y0 = -1.5,-.5
h = 0.2
for i in range(30):
    x0 += -h * Fx(x0,y0)
    y0 += -h * Fy(x0,y0)
    #ax.scatter3D(x0,y0,F(x0,y0),s=240,c='g',marker='o')
    ax.plot([x0,x0],[y0,y0],[-1,F(x0,y0)],c='r')

plt.figure()
plt.contourf(X,Y,F(X,Y))
plt.quiver(X,Y,-Fx(X,Y),-Fy(X,Y))
plt

x0,y0 = -1.5,-.5
h = 0.1
for i in range(100):
    x0 += -h * Fx(x0,y0)
    y0 += -h * Fy(x0,y0)
    plt.plot(x0,y0,'go')


I am working on the file "ML_SS2017/numpy_rachunek_rozniczkowy.ipynb".
The first task is:
Laplasjan
https://en.wikipedia.org/wiki/Discrete_Laplace_operator
Can you write Python code for it?

import sympy
from sympy.abc import x,y
sympy.init_printing(use_latex='mathjax')

from IPython.display import display

f_symb = sympy.sin(x**2+y**2)
lap_f = f_symb.diff(x,2) + f_symb.diff(y,2)
display(lap_f.simplify())


I am working on the file "ML_SS2017/numpy_rachunek_rozniczkowy.ipynb".
The first task is:
Numeryczne całkowanie
Całka to pole, więc obliczmy je w przybliżony sposób za pomocą prostokątów.
Przedział $[a,b]$ jest podzielony na $n$ podprzedziałów $$(a,x_1), (x_1,x_2), ..., (x_{n-1},b),$$ punkt $\xi_{j}$ zaś, dla $j = 1, 2, ..., n$ leży gdzieś wewnątrz $j$-tego odcinka. Tworzymy sumę:
$$S_{n} = \sum_{j=1}^{n}f(\xi_{j})(x_{j} - x_{j-1}) = \sum_{j=1}^{n}f(\xi_{j}) \Delta x_{j}$$
przy czym $x_0 = a$, $x_n = b$ oraz $\Delta x_j = x_j - x_{j-1}$.
Geometrycznie suma ta, nazywana sumą Riemanna, jest sumą pól prostokątów zaznaczonych na rysunku poniżej. Jeżeli będziemy dzielić $[a,b]$ na coraz to więcej odcinków o coraz to mniejszych długościach, otrzymamy jako granicę sum Riemanna całkę Riemanna funkcji $f(x)$, oznaczaną przez:
$$ \int_{a}^{b} f(x)dx = \lim_{|L| \to 0} \sum_{j=1}^{n} f(\xi_{j}) \Delta x_{j} \quad (1) $$
gdzie przez $|L|$ oznaczyliśmy długość największego z podprzedziałów.
Kwadratury
Metody numetyczne obliczania całek sprowadzają sie do obliczania kwadratur - tj. sumy ważonej wartości funkcji w punktach:
$$ \int_a^b f(x)dx = \sum_{i=1}^N w_i f_i,$$
gdzie $f_i = f(x_i)$.
W metodzie trapezów link mamy wagi $w_i=1$ dla wszystkich  punków z wyjątkiem: $w_1=w_{N}=\frac{1}{2}$.
Przykład:
$$\int_0^\pi \sin(x)dx = 2$$
Can you write Python code for it?

import numpy as np
x = np.linspace(0,np.pi,10)
f = np.sin(x)
w = np.ones_like(x)
w[0] = 0.5
w[-1] = 0.5
h = x[1]-x[0]
print(h*np.sum(w*f))


I am working on the file "ML_SS2017/numpy_rachunek_rozniczkowy.ipynb".
The first task is:
hill shading
Can you write Python code for it?

x_ = np.linspace(-5,5,150)
y_ = np.linspace(-5,5,154)
X,Y = np.meshgrid(x_,y_)

f_symb = sympy.sin(x**2/4+y**2)
expr = sympy.diff(f_symb,x)
display(f_symb)

F = sympy.lambdify((x,y),f_symb,np)
Fx = sympy.lambdify((x,y),f_symb.diff(x),np)
Fy = sympy.lambdify((x,y),f_symb.diff(y),np)

plt.figure()
plt.imshow(F(X,Y),alpha=.6,cmap='jet')
plt.imshow(np.diff(F(X,Y),axis=0),alpha=.4,cmap='gray')


I am working on the file "ML_SS2017/tf_logistic_equation.ipynb".
The first task is:
Logistic with tf
Can you write Python code for it?

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

import tensorflow as tf

config = tf.ConfigProto()
config.gpu_options.allow_growth = True

Nx = 10240
Na = 1024

a = np.linspace(3.255,4,Na).astype(np.float32)
a = np.repeat(a,Nx)
x = np.random.rand(Na*Nx).astype(np.float32)


I am working on the file "ML_SS2017/tf_logistic_equation.ipynb".
The first task is:
Performance:

ultra optimal logistic - ~200 Giter/s
ElementwiseKernel - 10+ Giter/s
CPU single core - 0.3 Giter/s
Tensorflow @GPU 5Giter/s
Tensorflow @CPU (20cores) 0.5Giter/s
Can you write Python code for it?

import pycuda.gpuarray as gpuarray

from pycuda.curandom import rand as curand
from pycuda.compiler import SourceModule
import pycuda.driver as cuda

try:
    ctx.pop()
    ctx.detach()
except:
    print( "No CTX!")

cuda.init()
device = cuda.Device(1)
ctx = device.make_context()

mod = SourceModule("""
    __global__ void logistic_iterations(float *a,float *x,int Niter)
    {

      int idx = threadIdx.x + blockDim.x*blockIdx.x;
      float a_ = a[idx];
      float x_ = x[idx];
      int i;
      for (i=0;i<Niter;i++){

          x_ = a_*x_*(1-x_);
      }

      x[idx] = x_;
    }
    """)
logistic_iterations = mod.get_function("logistic_iterations")

block_size=128
Nx = 1024
Na = 1024
blocks = Nx*Na//block_size

a = np.linspace(3.255,4,Na).astype(np.float32)
a = np.repeat(a,Nx)
x = np.random.rand(Na*Nx).astype(np.float32)

a_gpu = gpuarray.to_gpu(a)
#%time x_gpu = curand((Na*Nx,))
%time x_gpu = gpuarray.to_gpu(x)

%%time
logistic_iterations(a_gpu,x_gpu, np.int32(1000),block=(block_size,1,1), grid=(blocks,1,1))
ctx.synchronize()

%%time
for i in range(1000):
    logistic_iterations(a_gpu,x_gpu, np.int32(1),block=(block_size,1,1), grid=(blocks,1,1))
ctx.synchronize()

%%time
for i in range(1000):
    x_gpu = a_gpu*x_gpu*(1-x_gpu)

import numexpr as ne

%%time
for i in range(1000):
    x = ne.evaluate("a*x*(1-x)")

%%time
for i in range(1000):
    x = a*x*(1-x)

%%time
for i in range(1000):
    x *= (1.0-x)
    x *= a

a,x = a_gpu.get(),x_gpu.get()

H, xedges, yedges = np.histogram2d(a,x,bins=(Na,500))
plt.figure(num=1, figsize=(10,5))
plt.imshow(1-np.log(H.T+5e-1),origin='lower',cmap='gray')


I am working on the file "ML_SS2017/tf_logistic_equation.ipynb".
The first task is:
Pętla zewnętrzna
Can you write Python code for it?

with tf.device('/gpu:0'):
    X = tf.Variable(x)
    A = tf.constant(a)

    X_ = A*X*(1.0-X)
    step = tf.group( X.assign(X_) ) # ?with or not?

sess = tf.InteractiveSession(config=config)
init_op = tf.global_variables_initializer()
sess.run(init_op)

step = tf.group( X.assign(X_) )

%%time
for i in range(1000):
    step.run()


I am working on the file "ML_SS2017/tf_logistic_equation.ipynb".
The first task is:
WRONG !!!!
This creates huuuge graph
python
    for i in range(100):
        sess.run( X.assign(X_))
Visualization
Can you write Python code for it?

H, xedges, yedges = np.histogram2d(a,x,bins=(Na,500))

plt.figure(num=1, figsize=(10,5))
plt.imshow(1-np.log(H.T+5e-1),origin='lower',cmap='gray')


I am working on the file "ML_SS2017/tf_logistic_equation.ipynb".
The first task is:
tf.while_loop
Can you write Python code for it?

with tf.device('/gpu:0'):
    X = tf.Variable(x)
    A = tf.constant(a)

    def c(i,x):
        return  tf.less(i, 1000)

    def b(i,x):
        x = A*x*(1-x)
        return tf.add(i, 1),x

    r = tf.while_loop(c, b, (tf.constant(0),X))

init_op = tf.global_variables_initializer()
with tf.Session(config=config) as sess:
    sess.run(init_op)
    %time iterations,x = sess.run(r)


I am working on the file "ML_SS2017/zajecia_MJ_21.4.2017.ipynb".
The first task is:
Input pipeline
Zastosowano tu następującą strategie:

do trenowania używamy kolejki zarządzanej przez tensorflow
to testowania, pobieramy z kolejki dane w postaci tablic numpy i przekazujemy je do tensorflow z użyciem feed_dict

net
Can you write Python code for it?

def read_data(filename_queue):
    reader = tf.TFRecordReader()
    _, se = reader.read(filename_queue)
    f = tf.parse_single_example(se,features={'image/encoded':tf.FixedLenFeature([],tf.string),
                                            'image/class/label':tf.FixedLenFeature([],tf.int64),
                                            'image/height':tf.FixedLenFeature([],tf.int64),
                                            'image/width':tf.FixedLenFeature([],tf.int64)})
    image = tf.image.decode_png(f['image/encoded'],channels=3)
    image.set_shape( (32,32,3) )
    return image,f['image/class/label']

tf.reset_default_graph()

fq = tf.train.string_input_producer([dane_train])
image_data, label = read_data(filename_queue=fq)

batch_size = 128
images, sparse_labels = tf.train.shuffle_batch( [image_data,label],batch_size=batch_size,
                                               num_threads=2,
                                               capacity=1000+3*batch_size,
                                               min_after_dequeue=1000
                                               )
images = (tf.cast(images,tf.float32)-128.0)/33.0


I am working on the file "ML_SS2017/zajecia_MJ_21.4.2017.ipynb".
The first task is:
Testing
Możemy wykorzystać feed_dict by wykonać graf operacji na ndanych testowych.
Can you write Python code for it?

Images,Labels = sess.run([test_images,test_sparse_labels])

predicted = np.argmax(sess.run(logits,feed_dict={images: Images}),axis=1)
np.sum(predicted==Labels)/Labels.size

for ith in range(5):
    print (label2txt[Labels[ith]],(label2txt[predicted[ith]]))
    plt.imshow((Images[ith]*33+128).astype(np.uint8))
    plt.show()

%%time
l_lst =[]
for i in range(1):
    Images,Labels = sess.run([test_images,test_sparse_labels])
    predicted = np.argmax(sess.run(logits,feed_dict={images: Images}),axis=1)
    rlst = np.sum(predicted==Labels)/Labels.size
print(rlst)

saver = tf.train.Saver()

saver.save(sess,'cifar_convet.ckpt',global_step=global_step)


I am working on the file "ML_UoW/Course00_MLFoundation/03_Classification_Analyzing_Product_Sentiment-Quiz.ipynb".
The first task is:
Defining Positive and Negative Sentense
Ignore 0 and 3 star ratings
1 and 2 are treated as Negative
4 and 4 are treated as Positive
Can you write Python code for it?

# ignoring the 3 star rating
data2 = data[data['rating'] != 3 ]

data2['sentiment'] = data2['rating'] > 3

data2.head(5)

# training the classifier model
# first, spliting the data into train and test datasets

train_data, test_data = data2.random_split(0.8, seed=0)

sentiment_model = gl.logistic_classifier.create(train_data,
                                    target='sentiment',
                                    features=['word_count'],
                                   validation_set=test_data)

# Evaluate the sentiment model
sentiment_model.evaluate(test_data, metric='roc_curve')

sentiment_model.show(view='Evaluation')

selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']

def word_count(line):
    wc={}
    for word in line.split():
        wc[word] = wc.get(word, 0) +1
    return wc

#data3['word_count_dic'] = data2['review'].apply(word_count)

data2.head(5)


I am working on the file "ML_UoW/Course00_MLFoundation/03_Classification_Analyzing_Product_Sentiment-Quiz.ipynb".
The first task is:
def selected_word_count(line, selected_words=['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']):
    wc={}
    for word in line.split():
        if word in selected_words:
            wc[word] = wc.get(word, 0) +1
    return wc
Can you write Python code for it?

def selected_word_count(line, selected_words=['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']):
    wc={}
    for key in selected_words:
         if key in line.keys():
            wc[key] = line[key]
    return wc

#data2['selected_word_count_dic'] = data2['word_count'].apply(selected_word_count)
data2['selected_word_count_dic'] = data2['word_count'].dict_trim_by_keys(selected_words, exclude=False)

def get_count(data, word):
    return data.get(word,0)

for word in selected_words:
    data2[word+'_count'] = data2['selected_word_count_dic'].apply(lambda line: get_count(line, word))

data2.head(4)

train_data,test_data = data2.random_split(.8, seed=0)

selected_words_model = gl.logistic_classifier.create(train_data,
                                 target='sentiment',
                                 features=['selected_word_count_dic'],
                                 validation_set=test_data)

#gl.SFrame.print_rows(num_rows=12, num_columns=5)
coef=selected_words_model['coefficients'].sort('value')
coef.print_rows(num_rows=12, num_columns=5)

# accuracy
selected_words_model.evaluate(test_data)

sentiment_model.evaluate(test_data)

# Analysis why clf is works better than selected_Word_model

diaper_champ_reviews = data2[data2['name']=='Baby Trend Diaper Champ']
diaper_champ_reviews.head(2)

diaper_champ_reviews['pred_sentiment'] = sentiment_model.predict(diaper_champ_reviews, output_type='probability')
diaper_champ_reviews_sorted = diaper_champ_reviews.sort('pred_sentiment', ascending=False)
diaper_champ_reviews_sorted.head(2)

diaper_champ_reviews['sel_pred_sentiment'] = selected_words_model.predict(diaper_champ_reviews, output_type='probability')
diaper_champ_reviews_sel_sorted = diaper_champ_reviews.sort('pred_sentiment', ascending=False)
diaper_champ_reviews_sel_sorted.head(2)

diaper_champ_reviews_sel_sorted[0:1]['review']

# Out of the 11 words in selected_words, which one is most used in the reviews in the...


I am working on the file "ML_UoW/Course00_MLFoundation/Deep Features for Image Classification.ipynb".
The first task is:
Apply the deep features model to first few images of test set
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/Deep Features for Image Classification.ipynb".
The first task is:
As we can see, the column deep_features already contains the pre-computed deep features for this data.
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/Deep Features for Image Classification.ipynb".
The first task is:
Computing deep features for our images
The two lines below allow us to compute deep features.  This computation takes a little while, so we have already computed them and saved the results as a column in the data you loaded.
(Note that if you would like to compute such deep features and have a GPU on your machine, you should use the GPU enabled GraphLab Create, which will be significantly faster for this task.)
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/Deep Features for Image Classification.ipynb".
The first task is:
Exploring the image data
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/Deep Features for Image Classification.ipynb".
The first task is:
Given the deep features, let's train a classifier
Can you write Python code for it?

deep_features_model = graphlab.logistic_classifier.create(image_train,
                                                         features=['deep_features'],
                                                         target='label')


I am working on the file "ML_UoW/Course00_MLFoundation/Deep Features for Image Classification.ipynb".
The first task is:
Load a common image analysis dataset
We will use a popular benchmark dataset in computer vision called CIFAR-10.
(We've reduced the data to just 4 categories = {'cat','bird','automobile','dog'}.)
This dataset is already split into a training set and test set.
Can you write Python code for it?

image_train = graphlab.SFrame('image_train_data/')
image_test = graphlab.SFrame('image_test_data/')


I am working on the file "ML_UoW/Course00_MLFoundation/Deep Features for Image Classification.ipynb".
The first task is:
The accuracy of this model is poor, getting only about 46% accuracy.
Can we improve the model using deep features
We only have 2005 data points, so it is not possible to train a deep neural network effectively with so little data.  Instead, we will use transfer learning: using deep features trained on the full ImageNet dataset, we will train a simple model on this small dataset.
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/Deep Features for Image Classification.ipynb".
The first task is:
The classifier with deep features gets all of these images right!
Compute test_data accuracy of deep_features_model
As we can see, deep features provide us with significantly better accuracy (about 78%)
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/document-retrieval.ipynb".
The first task is:
Applying the nearest-neighbors model for retrieval
Who is closest to Obama?
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/document-retrieval.ipynb".
The first task is:
As we can see, president Obama's article is closest to the one about his vice-president Biden, and those of other politicians.
Other examples of document retrieval
Can you write Python code for it?

swift = people[people['name'] == 'Taylor Swift']

knn_model.query(swift)

jolie = people[people['name'] == 'Angelina Jolie']

knn_model.query(jolie)

arnold = people[people['name'] == 'Arnold Schwarzenegger']

knn_model.query(arnold)


I am working on the file "ML_UoW/Course00_MLFoundation/document-retrieval.ipynb".
The first task is:
Build a nearest neighbor model for document retrieval
We now create a nearest-neighbors model and apply it to document retrieval.
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/document-retrieval.ipynb".
The first task is:
Exploring the entry for actor George Clooney
Can you write Python code for it?

clooney = people[people['name'] == 'George Clooney']
clooney['text']


I am working on the file "ML_UoW/Course00_MLFoundation/document-retrieval.ipynb".
The first task is:
Is Obama closer to Clinton than to Beckham?
We will use cosine distance, which is given by
(1-cosine_similarity)
and find that the article about president Obama is closer to the one about former president Clinton than that of footballer David Beckham.
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/document-retrieval.ipynb".
The first task is:
Most common words include uninformative words like "the", "in", "and",...
Compute TF-IDF for the corpus
To give more weight to informative words, we weigh them by their TF-IDF scores.
Can you write Python code for it?

people['word_count'] = graphlab.text_analytics.count_words(people['text'])
people.head()

tfidf = graphlab.text_analytics.tf_idf(people['word_count'])

# Earlier versions of GraphLab Create returned an SFrame rather than a single SArray
# This notebook was created using Graphlab Create version 1.7.1
if graphlab.version <= '1.6.1':
    tfidf = tfidf['docs']

tfidf

people['tfidf'] = tfidf


I am working on the file "ML_UoW/Course00_MLFoundation/document-retrieval.ipynb".
The first task is:
Sort the word counts for the Obama article
Turning dictonary of word counts into a table
Can you write Python code for it?

obama_word_count_table = obama[['word_count']].stack('word_count', new_column_name = ['word','count'])


I am working on the file "ML_UoW/Course00_MLFoundation/document-retrieval.ipynb".
The first task is:
Sorting the word counts to show most common words at the top
Can you write Python code for it?


I am working on the file "ML_UoW/Course00_MLFoundation/document-retrieval.ipynb".
The first task is:
Words with highest TF-IDF are much more informative.
Manually compute distances between a few people
Let's manually compare the distances between the articles for a few famous people.
Can you write Python code for it?

clinton = people[people['name'] == 'Bill Clinton']

beckham = people[people['name'] == 'David Beckham']


I am working on the file "ML_UoW/Course01_Regression/Week01_Linear_Regression_Assignment.ipynb".
The first task is:
According to this function and the regression slope and intercept from (3) what is the estimated square-feet for a house costing $800,000
Can you write Python code for it?

inverse_regression_predictions(800000, intercept, slope)

print(intercept, slope)


I am working on the file "ML_UoW/Course01_Regression/Week01_Linear_Regression_Assignment.ipynb".
The first task is:
According to this function and the slope and intercept  What is the RSS for the simple linear regression using squarefeet to predict prices on TRAINING data?
Can you write Python code for it?

print(get_residual_sum_of_squares(input_feature, output, intercept,slope))

def inverse_regression_predictions(output, intercept, slope):
    estimated_input=1.0*(output - intercept)/slope
    return(estimated_input)


I am working on the file "ML_UoW/Course01_Regression/Week01_Linear_Regression_Assignment.ipynb".
The first task is:
Which model (square feet or bedrooms) has lowest RSS on TEST data? Think about why this might be the case.
Can you write Python code for it?

bedroom_slope, bedroom_intercept = simple_linear_regression(train_data['bedrooms'], train_data['price'])

bedroom_RSS = get_residual_sum_of_squares(test_data['bedrooms'], test_data['price'], bedroom_intercept,bedroom_slope)
print(bedroom_RSS)

sqft_slope, sqft_intercept = simple_linear_regression(train_data['sqft_living'], train_data['price'])
sqft_RSS = get_residual_sum_of_squares(test_data['sqft_living'], test_data['price'], intercept, slope)
print(sqft_RSS)


I am working on the file "ML_UoW/Course01_Regression/Week02_Polynomial_Regression_Assignment01.ipynb".
The first task is:
Now using your three estimated models compute the RSS (Residual Sum of Squares) on the Training data.
Can you write Python code for it?

def calcRSS(model, features, output):
    predict = model.predict(features)
    error = output - predict
    rss = np.sum(np.square(error))
    return rss

calcRSS(model1, train_data[model1_features], train_data['price'])

calcRSS(model2, train_data[model2_features], train_data['price'])

calcRSS(model3, train_data[model3_features], train_data['price'])


I am working on the file "ML_UoW/Course01_Regression/Week02_Polynomial_Regression_Assignment01.ipynb".
The first task is:
What is the sign (positive or negative) for the coefficient/weight for ‘bathrooms’ in Model 1?
Can you write Python code for it?


I am working on the file "ML_UoW/Course01_Regression/Week02_Polynomial_Regression_Assignment01.ipynb".
The first task is:
What is the sign (positive or negative) for the coefficient/weight for ‘bathrooms’ in Model 2?
Can you write Python code for it?