Lecture 3 · Machine Learning for ICE · University of Klagenfurt, 2023
Lecture 3 of 8
From Machine Learning for ICE at the University of Klagenfurt, first taught in 2022: this is the 2023 edition, so some of it may be a little dated. All lectures.
01
A linear relation between a D-dimensional input \mathbf{x}_i and the output y_i, for i=1,\dots,N with N>D:
y_i=w_1x_{i1}+w_2x_{i2}+\dots+w_Dx_{iD}+n_i=\mathbf{x}_i^T\mathbf{w}+n_i
or compactly \mathbf{y}=\mathbf{X}\mathbf{w}+\mathbf{n}, with
\mathbf{X}=\begin{bmatrix}x_{11}&\cdots&x_{1D}\\\vdots&\ddots&\vdots\\x_{N1}&\cdots&x_{ND}\end{bmatrix},\quad\mathbf{w}=\begin{bmatrix}w_1\\\vdots\\w_D\end{bmatrix},\quad\mathbf{n}=\begin{bmatrix}n_1\\\vdots\\n_N\end{bmatrix},\quad\mathbf{y}=\begin{bmatrix}y_1\\\vdots\\y_N\end{bmatrix}
The system usually has no exact solution, so we solve it in the least-squares sense:
J(\mathbf{w})=\sum_{i=1}^N\Big(y_i-\sum_{j=1}^Dx_{ij}w_j\Big)^2=\lVert\mathbf{y}-\mathbf{X}\mathbf{w}\rVert^2
The minimum of J is the ordinary least squares solution
\mathbf{w}=(\mathbf{X}^T\mathbf{X})^{-1}\mathbf{X}^T\mathbf{y}
Two regularized versions add a penalty on the weights:
Regularizing is minimizing the error inside a budget for the weights. The contours of the error usually touch the disc of ridge at a generic point, where both weights are non-zero, but the diamond of lasso at a corner, where one weight is exactly zero.
Generate data as follows:
Implement ordinary least squares, ridge and lasso regression, and compare their errors.
N, D = 100, 50
X = 2*np.random.randint(low=0, high=2, size=(N, D)) - 1
true_w = np.random.uniform(low=0, high=1, size=(D, 1))
n = 0.1*np.random.normal(0, 1, (N, 1))
y = X.dot(true_w) + n
# ordinary least squares, in closed form
w_OLS = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
model = linear_model.Lasso(alpha=0.1)
model.fit(X, y)
w_Lasso = model.coef_
model = linear_model.Ridge(alpha=1)
model.fit(X, y)
w_Ridge = model.coef_[0]
the 50 weights: true, OLS, lasso, ridge
With N=100 samples for D=50 weights and little noise, OLS and ridge land on the true weights. The lasso with \alpha=0.1 shrinks them all, and sets several exactly to zero: a sparsity the true weights do not have.
A binary classifier: it models the probability of a class. It assumes a linear relation between the input \mathbf{x}_i and the logit, the log-odds of the class y_i=1:
\log\frac{p_i}{1-p_i}=\mathbf{x}_i^T\mathbf{w},\qquad p_i=p(y_i=1\mid\mathbf{x}_i;\mathbf{w})=\frac{1}{1+e^{-\mathbf{x}_i^T\mathbf{w}}}
so the probability is a sigmoid of a linear function.
The parameters that best fit the data maximize the likelihood
L(\mathbf{w})=\prod_{i=1}^Np_i^{\,y_i}\,(1-p_i)^{1-y_i}
Taking the logarithm turns it into the binary cross-entropy, minimized by gradient descent:
J(\mathbf{w})=-\sum_{i=1}^N\Big[y_i\log p_i+(1-y_i)\log(1-p_i)\Big]
Decide with a logistic regression model whether candidates are admitted to a prestigious university:
Report the accuracy of the model.
X = df[['gmat', 'gpa', 'work_experience']]
y = df['admitted']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0)
logistic_regression = LogisticRegression()
logistic_regression.fit(X_train, y_train)
y_pred = logistic_regression.predict(X_test)
print('Accuracy: ', metrics.accuracy_score(y_test, y_pred))Accuracy: 0.8

confusion matrix on the 10 test candidates
40 candidates, 10 kept for testing: 8 decided correctly. On five new candidates the model predicts [0 0 1 0 1]: admitted are the two with the most work experience.
02
A support vector machine (SVM) works for classification and regression. Its basic form is a binary classifier:
Map the data \mathbf{x}\in\mathbb{R}^D to a feature space \phi(\mathbf{x})\in\mathbb{R}^M, with labels y_i\in\{-1,+1\}. The decision boundary is the hyperplane
\mathcal{H}:\ \mathbf{w}^T\phi(\mathbf{x})+b=0
and the distance of a point from it is
d_{\mathcal{H}}(\phi(\mathbf{x}_0))=\frac{\lvert\mathbf{w}^T\phi(\mathbf{x}_0)+b\rvert}{\lVert\mathbf{w}\rVert_2}
We want the hyperplane with the maximum margin to the closest points:
\mathbf{w}^*=\arg\max_{\mathbf{w}}\Big[\min_i\,d_{\mathcal{H}}(\phi(\mathbf{x}_i))\Big]
A point is classified correctly when y_i\big[\mathbf{w}^T\phi(\mathbf{x}_i)+b\big]\ge0. So the problem becomes
\mathbf{w}^*=\arg\max_{\mathbf{w}}\,\frac{1}{\lVert\mathbf{w}\rVert_2}\Big[\min_i\,y_i\big[\mathbf{w}^T\phi(\mathbf{x}_i)+b\big]\Big]
Rescaling \mathbf{w} and b does not move the hyperplane, so without loss of generality set \min_i y_i[\mathbf{w}^T\phi(\mathbf{x}_i)+b]=1:
\mathbf{w}^*=\arg\max_{\mathbf{w}}\,\frac{1}{\lVert\mathbf{w}\rVert_2}\quad\text{s.t.}\quad\min_i\,y_i\big[\mathbf{w}^T\phi(\mathbf{x}_i)+b\big]=1
or equivalently, the primal form of the SVM:
\mathbf{w}^*=\arg\min_{\mathbf{w}}\,\frac12\lVert\mathbf{w}\rVert_2^2\quad\text{s.t.}\quad y_i\big[\mathbf{w}^T\phi(\mathbf{x}_i)+b\big]\ge1\ \ \forall i
If the data are not perfectly separable, introduce a slack variable \xi_i per point, which gives the model some flexibility and avoids overfitting:
\begin{aligned} \min_{\mathbf{w},b,\{\xi_i\}}\ &\frac12\lVert\mathbf{w}\rVert_2^2+C\sum_i\xi_i\\ \text{s.t.}\ \ &y_i\big[\mathbf{w}^T\phi(\mathbf{x}_i)+b\big]\ge1-\xi_i\quad\forall i\\ &\xi_i\ge0\quad\forall i \end{aligned}
C sets the trade-off between a wide margin and few violations. A point with 0<\xi_i\le1 is inside the margin but still correct; with \xi_i>1 it is misclassified.
On the line, no threshold separates the middle class from the outer one. Map each point to \phi(x)=(x,x^2) and a straight line does it. Kernels let the SVM work in such a space without ever computing \phi.
What if we do not know \phi(\mathbf{x})? Go to the dual through the Lagrangian. After some calculations, the dual form of the SVM is
\begin{aligned} \max_{\{\alpha_i\}}\ &\sum_i\alpha_i-\frac12\sum_i\sum_j\alpha_i\alpha_jy_iy_j\,\phi^T(\mathbf{x}_i)\,\phi(\mathbf{x}_j)\\ \text{s.t.}\ \ &0\le\alpha_i\le C\quad\forall i,\qquad\sum_i\alpha_iy_i=0 \end{aligned}
The great advantage: \phi only appears in products \phi^T(\mathbf{x}_i)\phi(\mathbf{x}_j), which can be replaced by a kernel K(\mathbf{x}_i,\mathbf{x}_j), with no knowledge of \phi. Predictions are simply
g(\mathbf{x})=\mathbf{w}^T\phi(\mathbf{x})+b=\sum_i\alpha_iy_i\,K(\mathbf{x}_i,\mathbf{x})+b
A famous kernel is the radial basis function, K(\mathbf{x}_i,\mathbf{x}_j)=\exp\!\Big(-\frac{\lVert\mathbf{x}_i-\mathbf{x}_j\rVert^2}{2\sigma^2}\Big).
Let X\in\{-1,1\} be the samples transmitted over a non-linear Gaussian channel with input-output relation
Y=X+4\epsilon\cdot\mathbb{1}(-X)+N
where \epsilon\sim\mathrm{Bernoulli}(0.5), N\sim\mathcal{N}(0,\sigma^2), and \mathbb{1}(x) is the step function, 1 for x>0 and 0 for x<0.
Train an SVM classifier that decodes Y, and discuss the choice of the kernel. Show the accuracy, the support vectors and the decision boundaries.
When -1 is sent, half of the time the channel pushes it to +3: no single threshold separates the two symbols. The same channel comes back in lectures 4 and 5.
N = 10000 # dataset dimension
sigma = 0.2 # standard deviation of the noise
data_tx = 2*np.random.randint(2, size=(N, 1)) - 1
linear_flag = 1 # 0 for the non-linear channel
bernoulli_event = (1-linear_flag)*np.random.binomial(size=(N, 1), n=1, p=0.5)
noise = sigma*np.random.normal(0, 1, (N, 1))
data_rx = data_tx + 4*bernoulli_event*(0.5*np.sign(-data_tx) + 0.5) + noise
# choose the SVM kernel based on the channel model
clf = svm.SVC(kernel='linear') if linear_flag else svm.SVC(kernel='rbf', C=1)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)Accuracy: 1.0

decision regions, linear channel
The saved run is the warm-up, the linear channel (linear_flag = 1): a linear kernel puts the threshold at 0 and decodes every test sample. On the non-linear channel the boundary must split Y in three regions, which is what the RBF kernel is for.
letizia.tech · Machine Learning for ICE · University of Klagenfurt