Skip to main content

1 Regression and Classification

Classification vs Regression:

  • The target variables in classification are discrete, while in regression are continuous
  • Classes have their order, while Clusters are exchangeable

Notations and general concepts​

L-p norm​

Minkowski distance(L-norm)

def minkowski_distance(a, b, p):
return sum(abs(e1-e2)**p for e1, e2 in zip(a,b))**(1/p)
  • p = 1, Manhattan Distance
  • p = 2, Euclidean Distance

If the number of training instances: N

KNN: O(Np + N log N) for each test instance

Normalization​

  • Z score(See probability)
  • Min - max

Batch Normalization​

Internal Covariate Shift(ICS)

  • While training a model, we expect independent and identically distributed data
  • For a deep learning model, as the parameter of a lower layer changes, the input distribution of the upper layer also changes. It's called Internal Covariate Shift
  • The upper layer's parameters need to continuously adapt to the new input data distribution, thus reduces the learning speed

Problems caused by ICS

  • Require Lower learning rates: Training would be slower
  • More careful about initializing: saturated regime will slow down the convergence

Batch normalization

  • Batch normalize: Calculate mini-batch mean and variance of each layer to decrease covariate shift
  • Scale and shift: Identity transform in order to avoid linear regime of the nonlinearity

20240507173916

20231026231114

Layer Normalization​

Compute the layer normalization all the hidden units in the same layer

Cross Validation​

simple cross validation:

  1. Randomly divide training data into StrainS_{train} and ScvS_{cv}(cross validation)
  2. For each model, traing on StrainS_{train}
  3. Estimate error on validation set ScvS_{cv}
  4. find the smallest error

K fold cross validation:

  1. Randomly divide training data into k equal parts S1,S2,...,SkS_1,S_2,...,S_k
  2. For each model, traing on all the data except SjS_j
  3. Estimate error on validation set SjS_j: errorSjerror_{S_j}
  4. Repeat k times, find the smallest error

Error=1k∑i=1kerrorSjError = \frac1k \sum_{i=1}^k error_{S_j}

Leave-one-out cross validation:

repeatedly train on all but one of the training examples and test on that held-out example

K fold vs LOO:

  • much faster
  • more biased(LOO used in sparse dataset)

Metrics​

  • Precision: the proportion of correctly predicted positive instances out of all instances the model predicted as positive. Precisioni=TPiTPi+FPi\text{Precision}_i = \frac{\text{TP}_i}{\text{TP}_i + \text{FP}_i}
  • Recall: the proportion of correctly predicted positive instances out of all actual positive instances. Recalli=TPiTPi+FNi\text{Recall}_i = \frac{\text{TP}_i}{\text{TP}_i + \text{FN}_i}
  • Macro F1=1n∑i=1nF1i\text{Macro F1} = \frac{1}{n} \sum_{i=1}^{n} \text{F1}_i, Macro Precision=1n∑i=1nPrecisioni\text{Macro Precision} = \frac{1}{n} \sum_{i=1}^{n} \text{Precision}_i
  • Micro Precision=∑cTPc∑cTPc+∑cFPc\text{Micro Precision} = \frac{\sum_c TP_c}{\sum_c TP_c + \sum_c FP_c}, Micro Recall=∑cTPc∑cTPc+∑cFNc\text{Micro Recall} = \frac{\sum_c TP_c}{\sum_c TP_c + \sum_c FN_c}

20230510211336

Linear Regression​

Say we find a function f(x,θ)=θTxf(x,\theta) = \theta^T x

minimize a loss function L(θ)=∑i=1Nl(f(x(i),θ),y(i))L(\theta) = \sum_{i=1}^Nl(f(x^{(i)},\theta),y^{(i)})

Ordinary Least Square Regression​

  • θ:ω,b\theta: \omega,b
  • ll: square loss

L=12N∑i=1N(f(x(i),θ)−y(i))2L = \frac1{2N}\sum_{i=1}^N(f(x^{(i)},\theta) - y^{(i)})^2 (12N\frac1{2N} 为了gradients约分还有average)

Gradient descent​

For square loss,

∇θL(θt)=∂L(θ)∂θ=1N∑i=1N(f(x(i),θ)−y(i))x(i)\nabla_{\theta} L(\theta_t) = \frac{\partial L(\theta)}{\partial \theta} = \boxed{\frac1{N}\sum_{i=1}^{N} \left(f(x^{(i)},\theta)-y^{(i)}\right)x^{(i)}}
  1. Initialize θ0\theta_0 randomly, t = 0
  2. Repeat: θt+1=θt−η∇θL(θt)\theta_{t+1} = \theta_{t} - \eta \nabla_{\theta} L(\theta_t), t = t+1

η\eta is the learing rate

  • Too small: slow convergence.
  • Too large: may overshoot or diverge.

Adjust Learning rate​

  • constant learning rate
  • linear decay: linearly decreases over the training period
  • cosine learning rate: a max learning rate times the cosine value(from 0 to 1)
  • adaptive learning rate

Adagrad:

ηt=ηt+1,gt=∇θL(θt)θt+1=θt−ηtσtgtσt=∑i=0tgi2t+1\begin{aligned} \eta_t = \frac{\eta}{\sqrt{t+1}}, g_t = \nabla_{\theta} L(\theta_t) \\ \theta_{t+1} = \theta_t - \frac{\eta_t}{\sigma^t}g_t \\ \sigma_t = \sqrt{\frac{\sum_{i=0}^{t}g^2_i}{t+1}} \end{aligned}

Stochastic Gradient Descent​

Gradient descent could be slow: Every update involves the entire training data

Stochastic Gradient Descent (SGD)

Sample a batch of training data to estimate ∇L(θt)\nabla L(\theta_t)

Classification​

Assume in C1, X∼N(μ1,σ2)X \sim N(\mu_1,\sigma^2); in C2, X∼N(μ2,σ2)X \sim N(\mu_2,\sigma^2)

Apply min-max normalization for the target variable →\rightarrow [0,1]

20230515182156

Bayes‘ rule:

P(C1∣x)=P(x∣C1)P(C1)P(x∣C1)P(C1)+P(x∣C2)P(C2)P(C_1|x) = \frac{P(x|C_1)P(C_1)}{P(x|C_1)P(C_1) + P(x|C_2)P(C_2)}

we can proof that Logistic Regression

log⁡P(C1∣x)P(C2∣x)=θTx\log\frac{P(C_1|x)}{P(C_2|x)} = \theta^T x

As

P(C1∣x)+P(C2∣x)=1P(C1∣x)=11+e−θTx=11+e−z\begin{aligned} P(C_1|x) + P(C_2|x) = 1 \\ P(C_1|x) = \frac1{1+e^{-{\theta}^Tx}} = \frac1{1+e^{-z}} \end{aligned}

We called it "Sigmoid"

20230515183340

Maximum Likelihood Estimate​

For binary classification, labels y(i)∈{0,1}y^{(i)} \in \{0, 1\}.

Assuming NN samples are independent and identically distributed (i.i.d.):

L(θ)=∏i=1NP(y(i)∣x(i);θ)=∏i=1Nf(x(i),θ)y(i)(1−f(x(i),θ))1−y(i)L(\theta) = \prod_{i=1}^N P(y^{(i)} \mid x^{(i)}; \theta) = \prod_{i=1}^N f(x^{(i)}, \theta)^{y^{(i)}} (1 - f(x^{(i)}, \theta))^{1 - y^{(i)}}

Average Log-Likelihood (to maximize):

ℓ(θ)=1Nlog⁡L(θ)=1N∑i=1N[y(i)log⁡f(x(i),θ)+(1−y(i))log⁡(1−f(x(i),θ))]\ell(\theta) = \frac{1}{N} \log L(\theta) = \frac{1}{N} \sum_{i=1}^N \left[ y^{(i)} \log f(x^{(i)}, \theta) + (1 - y^{(i)}) \log(1 - f(x^{(i)}, \theta)) \right]

Thus:

∇θℓ(θ)=∂ℓ(θ)∂θ=1N∑i=1N(y(i)−f(x(i),θ))x(i)\nabla_{\theta}\ell(\theta) = \frac{\partial \ell(\theta)}{\partial \theta} = \boxed{\frac{1}{N} \sum_{i=1}^N \left( y^{(i)} - f(x^{(i)}, \theta) \right) x^{(i)}}

Update via Gradient Ascent:

θt+1=θt+η∇θℓ(θt)=θt+η(1N∑i=1N(y(i)−f(x(i),θ))x(i))\theta_{t+1} = \theta_t + \eta \nabla_{\theta}\ell(\theta_t) = \theta_t \mathbf{+} \eta \left( \frac{1}{N}\sum_{i=1}^{N}\left(y^{(i)} - f(x^{(i)},\theta)\right)x^{(i)} \right)

Cross-Entropy Loss​

Define loss (Negative Log-Likelihood) to minimize:

J(θ)=−ℓ(θ)=−1N∑i=1N[y(i)log⁡f(x(i),θ)+(1−y(i))log⁡(1−f(x(i),θ))]J(\theta) = -\ell(\theta) = -\frac{1}{N} \sum_{i=1}^N \left[ y^{(i)} \log f(x^{(i)}, \theta) + (1 - y^{(i)}) \log(1 - f(x^{(i)}, \theta)) \right]

Gradient:

∇θJ(θ)=∂J(θ)∂θ=1N∑i=1N(f(x(i),θ)−y(i))x(i)\nabla_{\theta} J(\theta) = \frac{\partial J(\theta)}{\partial \theta} = \boxed{\frac{1}{N} \sum_{i=1}^N \left( f(x^{(i)}, \theta) - y^{(i)} \right) x^{(i)}}

Update via Gradient Descent:

θt+1=θt−η∇θJ(θt)=θt−η(1N∑i=1N(f(x(i),θ)−y(i))x(i))\theta_{t+1} = \theta_t - \eta \nabla_{\theta}J(\theta_t) = \theta_t \mathbf{-} \eta \left( \frac{1}{N}\sum_{i=1}^{N}\left(f(x^{(i)},\theta) - y^{(i)}\right)x^{(i)} \right)

same as gradient descent

Multiclass Classification​

If we look at sigmoid function P(y1∣x)=eθTxeθTx+1P(y_1|x) = \frac{e^{\theta^Tx}}{e^{\theta^Tx} + 1}, we could extend the dimension and proof that

P(y∣x;θ)=exp⁡(θTx⃗)∑j=1nexp⁡(θjTx)P(y|x;\theta) = \frac{\exp(\vec{\theta^T x})}{\sum_{j=1}^n\exp(\theta_j^Tx)}

We define softmax: softmax(t1,...,tk)=exp⁡(t⃗)∑j=1nexp⁡(tj)(t_1, . . . , t_k) = \frac{\exp(\vec{t})}{\sum_{j=1}^n\exp(t_j)}

In fact, that's the same as

p(yi∣x)=p(x∣yi)p(yi)∑jp(x∣yj)p(yj)p(y_i|x) = \frac{p(x|y_i)p(y_i)}{\sum_jp(x|y_j)p(y_j)}

We could also proof that MLE loss is the same as cross entropy CE(y,y^)(y,\hat y)

LNLL=−1m∑i=1m∑y∈Yilog⁡P(y∣xi;θ)\mathcal{L}_{NLL} = - \frac{1}{m} \sum_{i=1}^m \sum_{y \in Y_i} \log P(y \mid x_i; \theta)
LCE=−1m∑i=1m∑j=1nyijlog⁡P(yj∣xi;θ)\mathcal{L}_{CE} = - \frac{1}{m} \sum_{i=1}^m \sum_{j=1}^n y_{ij} \log P(y_j \mid x_i; \theta)

Naive Bayes​

Bayes' rule for classification:

P(y=c∣x)=P(x∣y=c)P(y=c)P(x)∝P(y=c)P(x∣y=c)P(y = c \mid x) = \frac{P(x \mid y = c)P(y = c)}{P(x)} \propto P(y = c)P(x \mid y = c)

Naive conditional independence assumption: features x1,…,xdx_1, \dots, x_d are conditionally independent given class yy:

P(x∣y=c)=∏j=1dP(xj∣y=c)P(x \mid y = c) = \prod_{j=1}^d P(x_j \mid y = c)

Decision rule:

y^=arg⁡max⁡cP(y=c)∏j=1dP(xj∣y=c)=arg⁡max⁡c[log⁡P(y=c)+∑j=1dlog⁡P(xj∣y=c)]\boxed{\hat{y} = \arg\max_{c} P(y = c)\prod_{j=1}^d P(x_j \mid y = c) = \arg\max_c \left[ \log P(y = c) + \sum_{j=1}^d \log P(x_j \mid y = c) \right]}

Prior: P(y=c)=NcNP(y = c) = \frac{N_c}{N}

Likelihood models P(xj∣y=c)P(x_j \mid y = c):

  • Gaussian NB (continuous features): xj∣y=c∼N(μcj,σcj2)x_j \mid y=c \sim \mathcal{N}(\mu_{cj}, \sigma_{cj}^2)
  • Multinomial NB (discrete / word count features):
    P(xj∣y=c)=Ncj+αNc+αd(α=1:Laplace Smoothing)P(x_j \mid y = c) = \frac{N_{cj} + \alpha}{N_c + \alpha d} \quad (\alpha = 1: \text{Laplace Smoothing})
  • Bernoulli NB (binary features): P(xj∣y=c)=pcjxj(1−pcj)1−xjP(x_j \mid y = c) = p_{cj}^{x_j}(1 - p_{cj})^{1 - x_j}

Naive Bayes vs Logistic Regression:

  • Generative (P(x,y)P(x, y)) vs Discriminative (P(y∣x)P(y \mid x))
  • NB reaches its asymptotic error faster with fewer samples (O(log⁡d)O(\log d) vs O(d)O(d)), but LR has lower asymptotic error when data is sufficient.

import numpy as np

class GaussianNaiveBayes:
def __init__(self, var_smoothing=1e-9):
self.var_smoothing = var_smoothing

def fit(self, X, y):

self.classes_, counts = np.unique(
y, return_counts=True
)

# P(y = c)
self.class_prior_ = counts / len(y)

# Mean and variance for each class and feature
self.theta_ = np.array([
X[y == c].mean(axis=0)
for c in self.classes_
])

self.var_ = np.array([
X[y == c].var(axis=0)
for c in self.classes_
])

# Prevent division by zero
eps = max(
self.var_smoothing * np.var(X, axis=0).max(),
1e-12
)
self.var_ += eps

return self

def predict(self, X):
X = np.asarray(X, dtype=float)

# Shape:
# X[:, None, :] -> (n_samples, 1, n_features)
# theta_[None, :, :] -> (1, n_classes, n_features)
diff = X[:, None, :] - self.theta_[None, :, :]

# Log Gaussian density
log_likelihood = -0.5 * np.sum(
np.log(2 * np.pi * self.var_)[None, :, :]
+ diff**2 / self.var_[None, :, :],
axis=2
)

# Add log prior
log_posterior = (
log_likelihood
+ np.log(self.class_prior_)[None, :]
)

# Choose class with maximum posterior score
return self.classes_[
np.argmax(log_posterior, axis=1)
]

Support Vector Machine​

Suppose

H:wTx+b=0,H:\quad w^Tx+b=0,

w≠0\textbf{w}\neq 0 is the normal vector

Distance from x0x_0 to HH is

d(x0,H)=∣wTx0+b∣∥w∥2\boxed{ d(x_0,H)= \frac{|w^Tx_0+b|}{\|w\|_2} }

Support vectors are the data points, which achieve the maximum margin (yi(wTxi−b)=1y_i(w^Tx_i-b)=1)

20260930152351

hard-margin SVM

min⁡w,b12∥w∥22s.t.yi(wTxi−b)≥1,i=1,…,n.\boxed{ \begin{aligned} \min_{w,b}\quad & \frac12\|w\|_2^2\\ \text{s.t.}\quad &y_i(w^Tx_i-b)\ge1,\quad i=1,\ldots,n. \end{aligned}}

The constraints are the annoying part. Lagrange multipliers let us fold all those constraints into one function.

L(w,b,α)=12∥w∥2−∑i=1nαi[yi(wTxi−b)−1],L(w,b,\alpha) = \frac12\|w\|^2 - \sum_{i=1}^n \alpha_i \left[ y_i(w^Tx_i-b)-1 \right],

with

αi≥0.\alpha_i\ge0.

Take derivatives of the Lagrangian.

With respect to (w):

∂L∂w=w−∑iαiyixi=0⇒w=∑iαiyixi.\frac{\partial L}{\partial w}=w-\sum_i\alpha_i y_i x_i=0 \Rightarrow \boxed{ w=\sum_i\alpha_i y_i x_i }.
∂L∂b=∑iαiyi=0⇒∑iαiyi=0.\frac{\partial L}{\partial b}=\sum_i\alpha_i y_i=0 \Rightarrow \boxed{ \sum_i\alpha_i y_i=0 }.

Dual SVM:

max⁡α∑iαi−12∑i,jαiαjyiyjxiTxj\boxed{ \max_{\alpha} \sum_i\alpha_i - \frac12 \sum_{i,j} \alpha_i\alpha_j y_i y_j x_i^Tx_j }

subject to

αi≥0,\alpha_i\ge0,
∑iαiyi=0.\sum_i\alpha_i y_i=0.

Notice the crucial thing:

xiTxj\boxed{x_i^Tx_j}

is the only way input vectors appear.

That is exactly what later allows the kernel trick:

xiTxj⟶K(xi,xj).x_i^Tx_j \longrightarrow K(x_i,x_j).

So historically/conceptually:

primal SVM→Lagrangian→dual SVM→kernel SVM.\text{primal SVM} \rightarrow \text{Lagrangian} \rightarrow \text{dual SVM} \rightarrow \text{kernel SVM}.

Decision Tree​

Non-parametric model: recursively partitions feature space into axis-aligned rectangular regions.

Splitting Criteria​

Let dataset DD have classes k=1,…,Kk = 1, \dots, K with empirical probabilities pkp_k.

1. Information Gain (ID3)​

Entropy:

H(D)=−∑k=1Kpklog⁡2pkH(D) = -\sum_{k=1}^K p_k \log_2 p_k

Information Gain for feature AA:

Gain(D,A)=H(D)−∑v∈Values(A)∣Dv∣∣D∣H(Dv)\boxed{\text{Gain}(D, A) = H(D) - \sum_{v \in \text{Values}(A)} \frac{|D_v|}{|D|} H(D_v)}
  • Bias: favors features with many distinct values (e.g. ID numbers).

2. Information Gain Ratio (C4.5)​

Penalizes high-cardinality features using intrinsic value IV(A)\text{IV}(A):

GainRatio(D,A)=Gain(D,A)IV(A),IV(A)=−∑v∈Values(A)∣Dv∣∣D∣log⁡2∣Dv∣∣D∣\text{GainRatio}(D, A) = \frac{\text{Gain}(D, A)}{\text{IV}(A)}, \quad \text{IV}(A) = -\sum_{v \in \text{Values}(A)} \frac{|D_v|}{|D|} \log_2 \frac{|D_v|}{|D|}

3. Gini Impurity (CART - Classification)​

Gini(D)=1−∑k=1Kpk2\text{Gini}(D) = 1 - \sum_{k=1}^K p_k^2

Binary split on feature AA:

Gini(D,A)=∣D1∣∣D∣Gini(D1)+∣D2∣∣D∣Gini(D2)\boxed{\text{Gini}(D, A) = \frac{|D_1|}{|D|}\text{Gini}(D_1) + \frac{|D_2|}{|D|}\text{Gini}(D_2)}
  • CART produces binary trees only.

4. Squared Error (CART - Regression)​

Find split feature jj and threshold ss minimizing sum of squared errors:

min⁡j,s[∑xi∈R1(j,s)(yi−c1)2+∑xi∈R2(j,s)(yi−c2)2],cm=mean(yi∣xi∈Rm)\min_{j, s} \left[ \sum_{x_i \in R_1(j,s)} (y_i - c_1)^2 + \sum_{x_i \in R_2(j,s)} (y_i - c_2)^2 \right], \quad c_m = \text{mean}(y_i \mid x_i \in R_m)

Overfitting & Pruning​

Fully grown trees easily overfit (high variance).

  • Pre-pruning: Early stopping during tree construction (max_depth, min_samples_split, min_impurity_decrease).
  • Post-pruning: Grow full tree, then prune bottom-up minimizing cost-complexity:
    Cα(T)=R(T)+α∣T∣C_\alpha(T) = R(T) + \alpha |T|
    where R(T)R(T) is training error, ∣T∣|T| is number of terminal leaves, α≥0\alpha \ge 0 controls penalty for complexity.

Ensemble Learning: Bagging and Boosting​

Core idea: combine multiple base (weak) learners to build a strong learner.

Expected Error=Bias2+Variance+Noise\text{Expected Error} = \text{Bias}^2 + \text{Variance} + \text{Noise}
  • Bagging reduces Variance (averaging independent models)
  • Boosting reduces Bias (sequentially fitting errors)

Bagging (Bootstrap Aggregating)​

  1. Bootstrap sampling: draw NN samples with replacement from training set of size NN.
    • Probability an instance is never chosen:
      lim⁡N→∞(1−1N)N=1e≈36.8%(Out-Of-Bag, OOB data)\lim_{N \to \infty} \left(1 - \frac1N\right)^N = \frac1e \approx 36.8\% \quad (\text{Out-Of-Bag, OOB data})
    • Each bootstrap sample contains ≈63.2%\approx 63.2\% distinct original instances.
  2. Train BB models independently in parallel.
  3. Aggregate:
    • Classification: Majority vote
    • Regression: Mean fˉ(x)=1B∑b=1Bfb(x)\bar{f}(x) = \frac1B \sum_{b=1}^B f_b(x)

Random Forest​

Bagging + Feature Subspace Sampling:

  • At each node split, randomly choose a subset of mm features out of dd total features (typically m=dm = \sqrt{d} for classification, m=d/3m = d/3 for regression).
  • De-correlates individual trees:
    Var(Xˉ)=ρσ2+1−ρBσ2\text{Var}(\bar{X}) = \rho \sigma^2 + \frac{1 - \rho}{B}\sigma^2
    Decreasing correlation ρ\rho between trees directly reduces ensemble variance.

Boosting​

Train weak learners sequentially; each learner focuses on the mistakes of preceding ones.

AdaBoost (Adaptive Boosting)​

  • Base learner: shallow decision trees (decision stumps).
  • Iteratively update sample weights: increase weights of misclassified instances.
  • Model weight for round tt (with error rate ϵt\epsilon_t):
    αt=12ln⁡(1−ϵtϵt)\alpha_t = \frac12 \ln \left( \frac{1 - \epsilon_t}{\epsilon_t} \right)
  • Final model:
    H(x)=sign(∑t=1Tαtht(x))\boxed{H(x) = \text{sign}\left(\sum_{t=1}^T \alpha_t h_t(x)\right)}

Gradient Boosting (GBDT)​

Instead of reweighting samples, fit the next base tree to the pseudo-residuals (negative gradient of loss):

rim=−[∂L(yi,f(xi))∂f(xi)]f=fm−1r_{im} = -\left[\frac{\partial L(y_i, f(x_i))}{\partial f(x_i)}\right]_{f=f_{m-1}}
  • For square loss L=12(y−f)2L = \frac12(y - f)^2: rim=yi−fm−1(xi)r_{im} = y_i - f_{m-1}(x_i) (exact residual).
  • Model update: fm(x)=fm−1(x)+ηhm(x)f_m(x) = f_{m-1}(x) + \eta h_m(x) (η\eta: shrinkage / learning rate).

XGBoost (Extreme Gradient Boosting)​

GBDT improvements:

  • 2nd-order Taylor expansion of loss: uses both 1st gradient gig_i and 2nd Hessian hih_i.
  • Regularized objective:
    L=∑i=1Nl(yi,y^i)+∑k(γTk+12λ∑j=1Tkwj2)\mathcal{L} = \sum_{i=1}^N l(y_i, \hat{y}_i) + \sum_{k} \left( \gamma T_k + \frac12 \lambda \sum_{j=1}^{T_k} w_j^2 \right)
    where TkT_k is number of leaves, ww is leaf weights vector.
  • Column subsampling + parallel tree construction.

Bagging vs Boosting​

DimensionBagging (e.g. Random Forest)Boosting (e.g. AdaBoost, GBDT, XGBoost)
StrategyParallel & independentSequential & dependent
Error focusReduces VarianceReduces Bias
Base learnersDeep trees (low bias, high variance)Shallow trees / stumps (high bias, low variance)
Overfitting riskLow (adding trees will not overfit)Higher if rounds too large / noisy data