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
Layer Normalization Compute the layer normalization all the hidden units in the same layer
Cross Validation simple cross validation:
Randomly divide training data into S t r a i n S_{train} S t r ain and S c v S_{cv} S c v (cross validation) For each model, traing on S t r a i n S_{train} S t r ain Estimate error on validation set S c v S_{cv} S c v find the smallest error K fold cross validation:
Randomly divide training data into k equal parts S 1 , S 2 , . . . , S k S_1,S_2,...,S_k S 1 , S 2 , ... , S k For each model, traing on all the data except S j S_j S j Estimate error on validation set S j S_j S j : e r r o r S j error_{S_j} erro r S j Repeat k times, find the smallest error E r r o r = 1 k ∑ i = 1 k e r r o r S j Error = \frac1k \sum_{i=1}^k error_{S_j} E rror = k 1 ∑ i = 1 k erro r 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. Precision i = TP i TP i + FP i \text{Precision}_i = \frac{\text{TP}_i}{\text{TP}_i + \text{FP}_i} Precision i = TP i + FP i TP i Recall: the proportion of correctly predicted positive instances out of all actual positive instances. Recall i = TP i TP i + FN i \text{Recall}_i = \frac{\text{TP}_i}{\text{TP}_i + \text{FN}_i} Recall i = TP i + FN i TP i Macro F1 = 1 n ∑ i = 1 n F1 i \text{Macro F1} = \frac{1}{n} \sum_{i=1}^{n} \text{F1}_i Macro F1 = n 1 ∑ i = 1 n F1 i , Macro Precision = 1 n ∑ i = 1 n Precision i \text{Macro Precision} = \frac{1}{n} \sum_{i=1}^{n} \text{Precision}_i Macro Precision = n 1 ∑ i = 1 n Precision i Micro Precision = ∑ c T P c ∑ c T P c + ∑ c F P c \text{Micro Precision} = \frac{\sum_c TP_c}{\sum_c TP_c + \sum_c FP_c} Micro Precision = ∑ c T P c + ∑ c F P c ∑ c T P c , Micro Recall = ∑ c T P c ∑ c T P c + ∑ c F N c \text{Micro Recall} = \frac{\sum_c TP_c}{\sum_c TP_c + \sum_c FN_c} Micro Recall = ∑ c T P c + ∑ c F N c ∑ c T P c
Linear Regression Say we find a function f ( x , θ ) = θ T x f(x,\theta) = \theta^T x f ( x , θ ) = θ T x
minimize a loss function L ( θ ) = ∑ i = 1 N l ( f ( x ( i ) , θ ) , y ( i ) ) L(\theta) = \sum_{i=1}^Nl(f(x^{(i)},\theta),y^{(i)}) L ( θ ) = ∑ i = 1 N l ( f ( x ( i ) , θ ) , y ( i ) )
Ordinary Least Square Regression θ : ω , b \theta: \omega,b θ : ω , b l l l : square lossL = 1 2 N ∑ i = 1 N ( f ( x ( i ) , θ ) − y ( i ) ) 2 L = \frac1{2N}\sum_{i=1}^N(f(x^{(i)},\theta) - y^{(i)})^2 L = 2 N 1 ∑ i = 1 N ( f ( x ( i ) , θ ) − y ( i ) ) 2 (1 2 N \frac1{2N} 2 N 1 为了gradients约分还有average)
Gradient descent For square loss,
∇ θ L ( θ t ) = ∂ L ( θ ) ∂ θ = 1 N ∑ i = 1 N ( 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)}} ∇ θ L ( θ t ) = ∂ θ ∂ L ( θ ) = N 1 i = 1 ∑ N ( f ( x ( i ) , θ ) − y ( i ) ) x ( i ) Initialize θ 0 \theta_0 θ 0 randomly, t = 0 Repeat: θ t + 1 = θ t − η ∇ θ L ( θ t ) \theta_{t+1} = \theta_{t} - \eta \nabla_{\theta} L(\theta_t) θ t + 1 = θ t − η ∇ θ L ( θ 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 , g t = ∇ θ L ( θ t ) θ t + 1 = θ t − η t σ t g t σ t = ∑ i = 0 t g i 2 t + 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} η t = t + 1 η , g t = ∇ θ L ( θ t ) θ t + 1 = θ t − σ t η t g t σ t = t + 1 ∑ i = 0 t g i 2 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) ∇ L ( θ t )
Classification Assume in C1, X ∼ N ( μ 1 , σ 2 ) X \sim N(\mu_1,\sigma^2) X ∼ N ( μ 1 , σ 2 ) ; in C2, X ∼ N ( μ 2 , σ 2 ) X \sim N(\mu_2,\sigma^2) X ∼ N ( μ 2 , σ 2 )
Apply min-max normalization for the target variable → \rightarrow → [0,1]
Bayes‘ rule:
P ( C 1 ∣ x ) = P ( x ∣ C 1 ) P ( C 1 ) P ( x ∣ C 1 ) P ( C 1 ) + P ( x ∣ C 2 ) P ( C 2 ) 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)} P ( C 1 ∣ x ) = P ( x ∣ C 1 ) P ( C 1 ) + P ( x ∣ C 2 ) P ( C 2 ) P ( x ∣ C 1 ) P ( C 1 ) we can proof that Logistic Regression
log P ( C 1 ∣ x ) P ( C 2 ∣ x ) = θ T x \log\frac{P(C_1|x)}{P(C_2|x)} = \theta^T x log P ( C 2 ∣ x ) P ( C 1 ∣ x ) = θ T x As
P ( C 1 ∣ x ) + P ( C 2 ∣ x ) = 1 P ( C 1 ∣ x ) = 1 1 + e − θ T x = 1 1 + 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} P ( C 1 ∣ x ) + P ( C 2 ∣ x ) = 1 P ( C 1 ∣ x ) = 1 + e − θ T x 1 = 1 + e − z 1 We called it "Sigmoid"
Maximum Likelihood Estimate For binary classification, labels y ( i ) ∈ { 0 , 1 } y^{(i)} \in \{0, 1\} y ( i ) ∈ { 0 , 1 } .
Assuming N N N samples are independent and identically distributed (i.i.d.):
L ( θ ) = ∏ i = 1 N P ( y ( i ) ∣ x ( i ) ; θ ) = ∏ i = 1 N f ( 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)}} L ( θ ) = i = 1 ∏ N P ( y ( i ) ∣ x ( i ) ; θ ) = i = 1 ∏ N f ( x ( i ) , θ ) y ( i ) ( 1 − f ( x ( i ) , θ ) ) 1 − y ( i ) Average Log-Likelihood (to maximize ):
ℓ ( θ ) = 1 N log L ( θ ) = 1 N ∑ i = 1 N [ 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] ℓ ( θ ) = N 1 log L ( θ ) = N 1 i = 1 ∑ N [ y ( i ) log f ( x ( i ) , θ ) + ( 1 − y ( i ) ) log ( 1 − f ( x ( i ) , θ )) ] Thus:
∇ θ ℓ ( θ ) = ∂ ℓ ( θ ) ∂ θ = 1 N ∑ i = 1 N ( 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)}} ∇ θ ℓ ( θ ) = ∂ θ ∂ ℓ ( θ ) = N 1 i = 1 ∑ N ( y ( i ) − f ( x ( i ) , θ ) ) x ( i ) Update via Gradient Ascent :
θ t + 1 = θ t + η ∇ θ ℓ ( θ t ) = θ t + η ( 1 N ∑ i = 1 N ( 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) θ t + 1 = θ t + η ∇ θ ℓ ( θ t ) = θ t + η ( N 1 i = 1 ∑ N ( y ( i ) − f ( x ( i ) , θ ) ) x ( i ) ) Cross-Entropy Loss Define loss (Negative Log-Likelihood) to minimize :
J ( θ ) = − ℓ ( θ ) = − 1 N ∑ i = 1 N [ 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] J ( θ ) = − ℓ ( θ ) = − N 1 i = 1 ∑ N [ y ( i ) log f ( x ( i ) , θ ) + ( 1 − y ( i ) ) log ( 1 − f ( x ( i ) , θ )) ] Gradient:
∇ θ J ( θ ) = ∂ J ( θ ) ∂ θ = 1 N ∑ i = 1 N ( 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)}} ∇ θ J ( θ ) = ∂ θ ∂ J ( θ ) = N 1 i = 1 ∑ N ( f ( x ( i ) , θ ) − y ( i ) ) x ( i ) Update via Gradient Descent :
θ t + 1 = θ t − η ∇ θ J ( θ t ) = θ t − η ( 1 N ∑ i = 1 N ( 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) θ t + 1 = θ t − η ∇ θ J ( θ t ) = θ t − η ( N 1 i = 1 ∑ N ( f ( x ( i ) , θ ) − y ( i ) ) x ( i ) ) same as gradient descent
Multiclass Classification If we look at sigmoid function P ( y 1 ∣ x ) = e θ T x e θ T x + 1 P(y_1|x) = \frac{e^{\theta^Tx}}{e^{\theta^Tx} + 1} P ( y 1 ∣ x ) = e θ T x + 1 e θ T x , we could extend the dimension and proof that
P ( y ∣ x ; θ ) = exp ( θ T x ⃗ ) ∑ j = 1 n exp ( θ j T x ) P(y|x;\theta) = \frac{\exp(\vec{\theta^T x})}{\sum_{j=1}^n\exp(\theta_j^Tx)} P ( y ∣ x ; θ ) = ∑ j = 1 n exp ( θ j T x ) exp ( θ T x ) We define softmax : softmax( t 1 , . . . , t k ) = exp ( t ⃗ ) ∑ j = 1 n exp ( t j ) (t_1, . . . , t_k) = \frac{\exp(\vec{t})}{\sum_{j=1}^n\exp(t_j)} ( t 1 , ... , t k ) = ∑ j = 1 n e x p ( t j ) e x p ( t )
In fact, that's the same as
p ( y i ∣ x ) = p ( x ∣ y i ) p ( y i ) ∑ j p ( x ∣ y j ) p ( y j ) p(y_i|x) = \frac{p(x|y_i)p(y_i)}{\sum_jp(x|y_j)p(y_j)} p ( y i ∣ x ) = ∑ j p ( x ∣ y j ) p ( y j ) p ( x ∣ y i ) p ( y i ) We could also proof that MLE loss is the same as cross entropy CE( y , y ^ ) (y,\hat y) ( y , y ^ )
L N L L = − 1 m ∑ i = 1 m ∑ y ∈ Y i log P ( y ∣ x i ; θ ) \mathcal{L}_{NLL} = - \frac{1}{m} \sum_{i=1}^m \sum_{y \in Y_i} \log P(y \mid x_i; \theta) L N LL = − m 1 i = 1 ∑ m y ∈ Y i ∑ log P ( y ∣ x i ; θ ) L C E = − 1 m ∑ i = 1 m ∑ j = 1 n y i j log P ( y j ∣ x i ; θ ) \mathcal{L}_{CE} = - \frac{1}{m} \sum_{i=1}^m \sum_{j=1}^n y_{ij} \log P(y_j \mid x_i; \theta) L CE = − m 1 i = 1 ∑ m j = 1 ∑ n y ij log P ( y j ∣ x i ; θ ) 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) P ( y = c ∣ x ) = P ( x ) P ( x ∣ y = c ) P ( y = c ) ∝ P ( y = c ) P ( x ∣ y = c ) Naive conditional independence assumption : features x 1 , … , x d x_1, \dots, x_d x 1 , … , x d are conditionally independent given class y y y :
P ( x ∣ y = c ) = ∏ j = 1 d P ( x j ∣ y = c ) P(x \mid y = c) = \prod_{j=1}^d P(x_j \mid y = c) P ( x ∣ y = c ) = j = 1 ∏ d P ( x j ∣ y = c ) Decision rule:
y ^ = arg max c P ( y = c ) ∏ j = 1 d P ( x j ∣ y = c ) = arg max c [ log P ( y = c ) + ∑ j = 1 d log P ( x j ∣ 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]} y ^ = arg c max P ( y = c ) j = 1 ∏ d P ( x j ∣ y = c ) = arg c max [ log P ( y = c ) + j = 1 ∑ d log P ( x j ∣ y = c ) ] Prior: P ( y = c ) = N c N P(y = c) = \frac{N_c}{N} P ( y = c ) = N N c
Likelihood models P ( x j ∣ y = c ) P(x_j \mid y = c) P ( x j ∣ y = c ) :
Gaussian NB (continuous features): x j ∣ y = c ∼ N ( μ c j , σ c j 2 ) x_j \mid y=c \sim \mathcal{N}(\mu_{cj}, \sigma_{cj}^2) x j ∣ y = c ∼ N ( μ c j , σ c j 2 ) Multinomial NB (discrete / word count features):P ( x j ∣ y = c ) = N c j + α N c + α d ( α = 1 : Laplace Smoothing ) P(x_j \mid y = c) = \frac{N_{cj} + \alpha}{N_c + \alpha d} \quad (\alpha = 1: \text{Laplace Smoothing}) P ( x j ∣ y = c ) = N c + α d N c j + α ( α = 1 : Laplace Smoothing ) Bernoulli NB (binary features): P ( x j ∣ y = c ) = p c j x j ( 1 − p c j ) 1 − x j P(x_j \mid y = c) = p_{cj}^{x_j}(1 - p_{cj})^{1 - x_j} P ( x j ∣ y = c ) = p c j x j ( 1 − p c j ) 1 − x j Naive Bayes vs Logistic Regression:
Generative (P ( x , y ) P(x, y) P ( x , y ) ) vs Discriminative (P ( y ∣ x ) P(y \mid x) P ( y ∣ x ) ) NB reaches its asymptotic error faster with fewer samples (O ( log d ) O(\log d) O ( log d ) vs O ( d ) 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 ) self . class_prior_ = counts / len ( y ) 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_ ] ) 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 ) diff = X [ : , None , : ] - self . theta_ [ None , : , : ] log_likelihood = - 0.5 * np . sum ( np . log ( 2 * np . pi * self . var_ ) [ None , : , : ] + diff ** 2 / self . var_ [ None , : , : ] , axis = 2 ) log_posterior = ( log_likelihood + np . log ( self . class_prior_ ) [ None , : ] ) return self . classes_ [ np . argmax ( log_posterior , axis = 1 ) ] Support Vector Machine Suppose
H : w T x + b = 0 , H:\quad w^Tx+b=0, H : w T x + b = 0 , w ≠ 0 \textbf{w}\neq 0 w = 0 is the normal vector
Distance from x 0 x_0 x 0 to H H H is
d ( x 0 , H ) = ∣ w T x 0 + b ∣ ∥ w ∥ 2 \boxed{ d(x_0,H)= \frac{|w^Tx_0+b|}{\|w\|_2} } d ( x 0 , H ) = ∥ w ∥ 2 ∣ w T x 0 + b ∣ Support vectors are the data points, which achieve the maximum margin (y i ( w T x i − b ) = 1 y_i(w^Tx_i-b)=1 y i ( w T x i − b ) = 1 )
hard-margin SVM
min w , b 1 2 ∥ w ∥ 2 2 s.t. y i ( w T x i − 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}} w , b min s.t. 2 1 ∥ w ∥ 2 2 y i ( w T x i − b ) ≥ 1 , i = 1 , … , n . The constraints are the annoying part. Lagrange multipliers let us fold all those constraints into one function.
L ( w , b , α ) = 1 2 ∥ w ∥ 2 − ∑ i = 1 n α i [ y i ( w T x i − b ) − 1 ] , L(w,b,\alpha) = \frac12\|w\|^2 - \sum_{i=1}^n \alpha_i \left[ y_i(w^Tx_i-b)-1 \right], L ( w , b , α ) = 2 1 ∥ w ∥ 2 − i = 1 ∑ n α i [ y i ( w T x i − b ) − 1 ] , with
Take derivatives of the Lagrangian.
With respect to (w):
∂ L ∂ w = w − ∑ i α i y i x i = 0 ⇒ w = ∑ i α i y i x i . \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 }. ∂ w ∂ L = w − i ∑ α i y i x i = 0 ⇒ w = i ∑ α i y i x i . ∂ L ∂ b = ∑ i α i y i = 0 ⇒ ∑ i α i y i = 0 . \frac{\partial L}{\partial b}=\sum_i\alpha_i y_i=0 \Rightarrow \boxed{ \sum_i\alpha_i y_i=0 }. ∂ b ∂ L = i ∑ α i y i = 0 ⇒ i ∑ α i y i = 0 . Dual SVM:
max α ∑ i α i − 1 2 ∑ i , j α i α j y i y j x i T x j \boxed{ \max_{\alpha} \sum_i\alpha_i - \frac12 \sum_{i,j} \alpha_i\alpha_j y_i y_j x_i^Tx_j } α max i ∑ α i − 2 1 i , j ∑ α i α j y i y j x i T x j subject to
∑ i α i y i = 0. \sum_i\alpha_i y_i=0. i ∑ α i y i = 0. Notice the crucial thing:
x i T x j \boxed{x_i^Tx_j} x i T x j is the only way input vectors appear.
That is exactly what later allows the kernel trick :
x i T x j ⟶ K ( x i , x j ) . x_i^Tx_j \longrightarrow K(x_i,x_j). x i T x j ⟶ 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}. primal SVM → Lagrangian → dual SVM → kernel SVM . Decision Tree Non-parametric model: recursively partitions feature space into axis-aligned rectangular regions.
Splitting Criteria Let dataset D D D have classes k = 1 , … , K k = 1, \dots, K k = 1 , … , K with empirical probabilities p k p_k p k .
Entropy:
H ( D ) = − ∑ k = 1 K p k log 2 p k H(D) = -\sum_{k=1}^K p_k \log_2 p_k H ( D ) = − k = 1 ∑ K p k log 2 p k Information Gain for feature A A A :
Gain ( D , A ) = H ( D ) − ∑ v ∈ Values ( A ) ∣ D v ∣ ∣ D ∣ H ( D v ) \boxed{\text{Gain}(D, A) = H(D) - \sum_{v \in \text{Values}(A)} \frac{|D_v|}{|D|} H(D_v)} Gain ( D , A ) = H ( D ) − v ∈ Values ( A ) ∑ ∣ D ∣ ∣ D v ∣ H ( D v ) Bias: favors features with many distinct values (e.g. ID numbers). Penalizes high-cardinality features using intrinsic value IV ( A ) \text{IV}(A) IV ( A ) :
GainRatio ( D , A ) = Gain ( D , A ) IV ( A ) , IV ( A ) = − ∑ v ∈ Values ( A ) ∣ D v ∣ ∣ D ∣ log 2 ∣ D v ∣ ∣ 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|} GainRatio ( D , A ) = IV ( A ) Gain ( D , A ) , IV ( A ) = − v ∈ Values ( A ) ∑ ∣ D ∣ ∣ D v ∣ log 2 ∣ D ∣ ∣ D v ∣ 3. Gini Impurity (CART - Classification) Gini ( D ) = 1 − ∑ k = 1 K p k 2 \text{Gini}(D) = 1 - \sum_{k=1}^K p_k^2 Gini ( D ) = 1 − k = 1 ∑ K p k 2 Binary split on feature A A A :
Gini ( D , A ) = ∣ D 1 ∣ ∣ D ∣ Gini ( D 1 ) + ∣ D 2 ∣ ∣ D ∣ Gini ( D 2 ) \boxed{\text{Gini}(D, A) = \frac{|D_1|}{|D|}\text{Gini}(D_1) + \frac{|D_2|}{|D|}\text{Gini}(D_2)} Gini ( D , A ) = ∣ D ∣ ∣ D 1 ∣ Gini ( D 1 ) + ∣ D ∣ ∣ D 2 ∣ Gini ( D 2 ) CART produces binary trees only. 4. Squared Error (CART - Regression) Find split feature j j j and threshold s s s minimizing sum of squared errors:
min j , s [ ∑ x i ∈ R 1 ( j , s ) ( y i − c 1 ) 2 + ∑ x i ∈ R 2 ( j , s ) ( y i − c 2 ) 2 ] , c m = mean ( y i ∣ x i ∈ R m ) \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) j , s min ⎣ ⎡ x i ∈ R 1 ( j , s ) ∑ ( y i − c 1 ) 2 + x i ∈ R 2 ( j , s ) ∑ ( y i − c 2 ) 2 ⎦ ⎤ , c m = mean ( y i ∣ x i ∈ 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| C α ( T ) = R ( T ) + α ∣ T ∣ where R ( T ) R(T) R ( T ) is training error, ∣ T ∣ |T| ∣ T ∣ is number of terminal leaves, α ≥ 0 \alpha \ge 0 α ≥ 0 controls penalty for complexity.Ensemble Learning: Bagging and Boosting Core idea: combine multiple base (weak) learners to build a strong learner.
Expected Error = Bias 2 + Variance + Noise \text{Expected Error} = \text{Bias}^2 + \text{Variance} + \text{Noise} Expected Error = Bias 2 + Variance + Noise Bagging reduces Variance (averaging independent models)Boosting reduces Bias (sequentially fitting errors)Bagging (Bootstrap Aggregating) Bootstrap sampling : draw N N N samples with replacement from training set of size N N N .Probability an instance is never chosen:lim N → ∞ ( 1 − 1 N ) N = 1 e ≈ 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}) N → ∞ lim ( 1 − N 1 ) N = e 1 ≈ 36.8% ( Out-Of-Bag, OOB data ) Each bootstrap sample contains ≈ 63.2 % \approx 63.2\% ≈ 63.2% distinct original instances. Train B B B models independently in parallel. Aggregate:Classification: Majority vote Regression: Mean f ˉ ( x ) = 1 B ∑ b = 1 B f b ( x ) \bar{f}(x) = \frac1B \sum_{b=1}^B f_b(x) f ˉ ( x ) = B 1 ∑ b = 1 B f b ( x ) Random Forest Bagging + Feature Subspace Sampling :
At each node split, randomly choose a subset of m m m features out of d d d total features (typically m = d m = \sqrt{d} m = d for classification, m = d / 3 m = d/3 m = 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 Var ( X ˉ ) = ρ σ 2 + B 1 − ρ σ 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 t t t (with error rate ϵ t \epsilon_t ϵ t ):α t = 1 2 ln ( 1 − ϵ t ϵ t ) \alpha_t = \frac12 \ln \left( \frac{1 - \epsilon_t}{\epsilon_t} \right) α t = 2 1 ln ( ϵ t 1 − ϵ t ) Final model:H ( x ) = sign ( ∑ t = 1 T α t h t ( x ) ) \boxed{H(x) = \text{sign}\left(\sum_{t=1}^T \alpha_t h_t(x)\right)} H ( x ) = sign ( t = 1 ∑ T α t h t ( x ) ) Gradient Boosting (GBDT) Instead of reweighting samples, fit the next base tree to the pseudo-residuals (negative gradient of loss):
r i m = − [ ∂ L ( y i , f ( x i ) ) ∂ f ( x i ) ] f = f m − 1 r_{im} = -\left[\frac{\partial L(y_i, f(x_i))}{\partial f(x_i)}\right]_{f=f_{m-1}} r im = − [ ∂ f ( x i ) ∂ L ( y i , f ( x i )) ] f = f m − 1 For square loss L = 1 2 ( y − f ) 2 L = \frac12(y - f)^2 L = 2 1 ( y − f ) 2 : r i m = y i − f m − 1 ( x i ) r_{im} = y_i - f_{m-1}(x_i) r im = y i − f m − 1 ( x i ) (exact residual). Model update: f m ( x ) = f m − 1 ( x ) + η h m ( x ) f_m(x) = f_{m-1}(x) + \eta h_m(x) f m ( x ) = f m − 1 ( x ) + η h m ( x ) (η \eta η : shrinkage / learning rate). XGBoost (Extreme Gradient Boosting) GBDT improvements:
2nd-order Taylor expansion of loss: uses both 1st gradient g i g_i g i and 2nd Hessian h i h_i h i .Regularized objective :L = ∑ i = 1 N l ( y i , y ^ i ) + ∑ k ( γ T k + 1 2 λ ∑ j = 1 T k w j 2 ) \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) L = i = 1 ∑ N l ( y i , y ^ i ) + k ∑ ( γ T k + 2 1 λ j = 1 ∑ T k w j 2 ) where T k T_k T k is number of leaves, w w w is leaf weights vector.Column subsampling + parallel tree construction. Bagging vs Boosting Dimension Bagging (e.g. Random Forest) Boosting (e.g. AdaBoost, GBDT, XGBoost) Strategy Parallel & independent Sequential & dependent Error focus Reduces Variance Reduces Bias Base learners Deep trees (low bias, high variance) Shallow trees / stumps (high bias, low variance) Overfitting risk Low (adding trees will not overfit) Higher if rounds too large / noisy data