Machine Learning Exam Solutions
Below are the detailed solutions to each of the exam questions. Each solution is presented step-by-step to facilitate understanding.
Question 1
Derive the solution of the univariate linear regression with the normal equations.
Solution
Univariate Linear Regression Overview:
In univariate linear regression, we model the relationship between a single feature ( x ) and the target variable ( y ) using a linear equation:
[
y = w_0 + w_1 x
]
Here:
- ( w_0 ) is the intercept.
- ( w_1 ) is the slope or weight for the feature ( x ).
Objective:
Find the optimal parameters ( w_0 ) and ( w_1 ) that minimize the cost function, typically the Mean Squared Error (MSE).
Cost Function (Mean Squared Error):
[
J(w_0, w_1) = \frac{1}{2m} \sum_{i=1}^{m} (y^{(i)} - (w_0 + w_1 x^{(i)}))^2
]
Where:
- ( m ) is the number of training examples.
- ( y^{(i)} ) is the true value.
- ( x^{(i)} ) is the input feature for the ( i^{th} ) example.
Normal Equations:
To find the parameters that minimize ( J(w_0, w_1) ), we set the partial derivatives of ( J ) with respect to ( w_0 ) and ( w_1 ) to zero.
- Partial derivative with respect to ( w_0 ):
[
\frac{\partial J}{\partial w_0} = -\frac{1}{m} \sum_{i=1}^{m} (y^{(i)} - w_0 - w_1 x^{(i)}) = 0
]
- Partial derivative with respect to ( w_1 ):
[
\frac{\partial J}{\partial w_1} = -\frac{1}{m} \sum_{i=1}^{m} x^{(i)} (y^{(i)} - w_0 - w_1 x^{(i)}) = 0
]
Solving the Equations:
Let's solve these two equations simultaneously to find ( w_0 ) and ( w_1 ).
- First Equation:
[
\sum_{i=1}^{m} y^{(i)} = m w_0 + w_1 \sum_{i=1}^{m} x^{(i)}
]
- Second Equation:
[
\sum_{i=1}^{m} x^{(i)} y^{(i)} = w_0 \sum_{i=1}^{m} x^{(i)} + w_1 \sum_{i=1}^{m} (x^{(i)})^2
]
Matrix Form:
These equations can be represented in matrix form as:
[
\begin{bmatrix}
m & \sum x^{(i)} \
\sum x^{(i)} & \sum (x^{(i)})^2
\end{bmatrix}
\begin{bmatrix}
w_0 \
w_1
\end{bmatrix}
\begin{bmatrix}
\sum y^{(i)} \
\sum x^{(i)} y^{(i)}
\end{bmatrix}
]
Solution Using Normal Equations:
The normal equation is given by:
[
\mathbf{w} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{y}
]
Where:
- ( \mathbf{X} ) is the design matrix.
- ( \mathbf{y} ) is the target vector.
- ( \mathbf{w} ) is the parameter vector ( [w_0, w_1]^\top ).
Example:
Suppose we have the following data points:
- Construct Design Matrix ( \mathbf{X} ):
[
\mathbf{X} =
\begin{bmatrix}
1 & 1 \
1 & 2 \
1 & 3 \
\end{bmatrix}
]
- Target Vector ( \mathbf{y} ):
[
\mathbf{y} =
\begin{bmatrix}
2 \
3 \
5 \
\end{bmatrix}
]
- Compute ( \mathbf{X}^\top \mathbf{X} ) and ( \mathbf{X}^\top \mathbf{y} ):
[
\mathbf{X}^\top \mathbf{X} =
\begin{bmatrix}
1 & 1 & 1 \
1 & 2 & 3 \
\end{bmatrix}
\begin{bmatrix}
1 & 1 \
1 & 2 \
1 & 3 \
\end{bmatrix}
\begin{bmatrix}
3 & 6 \
6 & 14 \
\end{bmatrix}
]
[
\mathbf{X}^\top \mathbf{y} =
\begin{bmatrix}
1 & 1 & 1 \
1 & 2 & 3 \
\end{bmatrix}
\begin{bmatrix}
2 \
3 \
5 \
\end{bmatrix}
\begin{bmatrix}
10 \
22 \
\end{bmatrix}
]
- Compute Parameters ( \mathbf{w} ):
[
\mathbf{w} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{y}
]
First, find the inverse of ( \mathbf{X}^\top \mathbf{X} ):
[
(\mathbf{X}^\top \mathbf{X})^{-1} = \frac{1}{(3)(14) - (6)(6)} \begin{bmatrix}
14 & -6 \
-6 & 3 \
\end{bmatrix}
= \frac{1}{42 - 36} \begin{bmatrix}
14 & -6 \
-6 & 3 \
\end{bmatrix}
= \frac{1}{6} \begin{bmatrix}
14 & -6 \
-6 & 3 \
\end{bmatrix}
\begin{bmatrix}
2.\overline{3} & -1 \
-1 & 0.5 \
\end{bmatrix}
]
Now, multiply by ( \mathbf{X}^\top \mathbf{y} ):
[
\mathbf{w} =
\begin{bmatrix}
2.\overline{3} & -1 \
-1 & 0.5 \
\end{bmatrix}
\begin{bmatrix}
10 \
22 \
\end{bmatrix}
\begin{bmatrix}
2.\overline{3} \times 10 -1 \times 22 \
-1 \times 10 + 0.5 \times 22 \
\end{bmatrix}
\begin{bmatrix}
23.\overline{3} - 22 \
-10 + 11 \
\end{bmatrix}
\begin{bmatrix}
1.\overline{3} \
1 \
\end{bmatrix}
]
Result:
[
w_0 = 1.\overline{3}, \quad w_1 = 1
]
Thus, the optimal linear regression model is:
[
y = 1.\overline{3} + 1 \cdot x
]
Question 2
Derive the cost function of logistic regression using a probabilistic approach.
Solution
Logistic Regression Overview:
Logistic regression is used for binary classification problems. It models the probability that a given input ( \mathbf{x} ) belongs to a particular class.
Model Definition:
The hypothesis ( h_\theta(\mathbf{x}) ) in logistic regression is defined using the sigmoid function:
[
h_\theta(\mathbf{x}) = P(y=1 | \mathbf{x}; \theta) = \frac{1}{1 + e^{-\theta^\top \mathbf{x}}}
]
Where:
- ( \theta ) is the parameter vector.
- ( \mathbf{x} ) is the input feature vector.
Probabilistic Interpretation:
- ( P(y=1 | \mathbf{x}; \theta) = h_\theta(\mathbf{x}) )
- ( P(y=0 | \mathbf{x}; \theta) = 1 - h_\theta(\mathbf{x}) )
Likelihood Function:
Given a training set ( { (\mathbf{x}^{(i)}, y^{(i)}) }_{i=1}^{m} ), the likelihood of the parameters ( \theta ) is the probability of observing the data:
[
L(\theta) = \prod_{i=1}^{m} P(y^{(i)} | \mathbf{x}^{(i)}; \theta)
]
Substituting the probabilities:
[
L(\theta) = \prod_{i=1}^{m} \left(h_\theta(\mathbf{x}^{(i)})\right)^{y^{(i)}} \left(1 - h_\theta(\mathbf{x}^{(i)})\right)^{1 - y^{(i)}}
]
Log-Likelihood:
To simplify, we take the natural logarithm of the likelihood:
[
\ell(\theta) = \log L(\theta) = \sum_{i=1}^{m} \left[ y^{(i)} \log h_\theta(\mathbf{x}^{(i)}) + (1 - y^{(i)}) \log (1 - h_\theta(\mathbf{x}^{(i)})) \right]
]
Cost Function:
The cost function ( J(\theta) ) is defined as the negative log-likelihood averaged over all training examples:
[
J(\theta) = -\frac{1}{m} \ell(\theta) = -\frac{1}{m} \sum_{i=1}^{m} \left[ y^{(i)} \log h_\theta(\mathbf{x}^{(i)}) + (1 - y^{(i)}) \log (1 - h_\theta(\mathbf{x}^{(i)})) \right]
]
Final Cost Function:
[
J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} \left[ y^{(i)} \log \left( \frac{1}{1 + e^{-\theta^\top \mathbf{x}^{(i)}}} \right) + (1 - y^{(i)}) \log \left( 1 - \frac{1}{1 + e^{-\theta^\top \mathbf{x}^{(i)}}} \right) \right]
]
Interpretation:
- First Term: Penalizes the model when ( y^{(i)} = 1 ) but ( h_\theta(\mathbf{x}^{(i)}) ) is small.
- Second Term: Penalizes the model when ( y^{(i)} = 0 ) but ( h_\theta(\mathbf{x}^{(i)}) ) is large.
Conclusion:
The derived cost function for logistic regression, based on the probabilistic approach, is the binary cross-entropy loss, which measures the discrepancy between the predicted probabilities and the actual binary outcomes.
Question 3
This question appears to be commented out in the document and thus is skipped.
Question 4
In which context are learning curves useful? How do they work?
Solution
Learning Curves Overview:
Learning curves are graphical representations that show the performance of a machine learning model over time or with varying amounts of training data. They are instrumental in diagnosing the learning behavior of models, particularly in understanding issues related to bias and variance.
Contexts Where Learning Curves are Useful:
-
Model Evaluation:
- Assessing whether the model is overfitting or underfitting.
-
Comparing Models:
- Determining which model performs better given the same data.
-
Data Sufficiency:
- Deciding if adding more training data could improve performance.
-
Algorithm Selection:
- Choosing appropriate algorithms based on learning dynamics.
How Learning Curves Work:
Learning curves typically plot two metrics against the number of training examples or against training iterations/epochs:
-
Training Error:
- Error on the training dataset.
-
Validation/Test Error:
- Error on an unseen validation or test dataset.
Types of Learning Curves:
-
Error vs. Training Set Size:
- X-axis: Number of training examples.
- Y-axis: Error (e.g., MSE, classification error).
-
Error vs. Training Iterations/Epochs:
- X-axis: Number of iterations or epochs.
- Y-axis: Error.
Interpreting Learning Curves:
-
High Bias (Underfitting):
- Both training and validation errors are high.
- Model is too simple.
-
High Variance (Overfitting):
- Training error is low.
- Validation error is high.
- Large gap between training and validation errors.
-
Good Fit:
- Both training and validation errors are low and converge to similar values.
-
Increasing Training Data:
- If the validation error decreases with more data, additional data may help.
- If it plateau, adding more data may not significantly improve performance.
Example Illustration:
Error
|
| Validation Error
| /
| /
| / Training Error
| /
|_______/_________ Number of Training Examples
- Convergence: Both errors decrease and converge as training size increases.
- Gap: Training error remains low while validation error stays high indicates overfitting.
Conclusion:
Learning curves are powerful diagnostic tools in machine learning that help practitioners understand the learning dynamics of models, diagnose issues like bias and variance, and make informed decisions about model complexity and dataset size.
Question 5
Consider the following linear regression model:
[
\begin{aligned}
y &= w_0 + w_1 x_1 + w_2 x_2
\end{aligned}
]
Assume that the current weights are ( w_0 = 0 ), ( w_1 = 1 ), ( w_2 = 0.5 ) and we have the following set of examples ( (\mathbf{X}, \mathbf{y}) ):
[
\mathbf{X} = \begin{bmatrix}
2 & 1 \
-3 & 4 \
0 & 5 \
\end{bmatrix}, \quad
\mathbf{y} = \begin{bmatrix}
2 \
4 \
2 \
\end{bmatrix}
]
Compute the initial value of the loss function and a single step of Gradient Descent to update the ( w_i ) parameters with Full Batch and in Stochastic solutions, using a learning rate ( \alpha = 0.01 ).
Solution
Given:
Step 1: Compute Initial Loss Function
The loss function for linear regression is the Mean Squared Error (MSE):
[
J(w_0, w_1, w_2) = \frac{1}{2m} \sum_{i=1}^{m} (y^{(i)} - \hat{y}^{(i)})^2
]
Where:
- ( m = 3 ) (number of examples)
- ( \hat{y}^{(i)} = w_0 + w_1 x_1^{(i)} + w_2 x_2^{(i)} )
Calculate ( \hat{y}^{(i)} ) for each example:
-
First Example (( i = 1 )):
[
\hat{y}^{(1)} = 0 + 1 \cdot 2 + 0.5 \cdot 1 = 0 + 2 + 0.5 = 2.5
]
[
\text{Error} = 2 - 2.5 = -0.5
]
[
\text{Squared Error} = (-0.5)^2 = 0.25
]
-
Second Example (( i = 2 )):
[
\hat{y}^{(2)} = 0 + 1 \cdot (-3) + 0.5 \cdot 4 = 0 - 3 + 2 = -1
]
[
\text{Error} = 4 - (-1) = 5
]
[
\text{Squared Error} = 5^2 = 25
]
-
Third Example (( i = 3 )):
[
\hat{y}^{(3)} = 0 + 1 \cdot 0 + 0.5 \cdot 5 = 0 + 0 + 2.5 = 2.5
]
[
\text{Error} = 2 - 2.5 = -0.5
]
[
\text{Squared Error} = (-0.5)^2 = 0.25
]
Total Squared Error:
[
\text{Total} = 0.25 + 25 + 0.25 = 25.5
]
Loss Function Value:
[
J = \frac{1}{2 \times 3} \times 25.5 = \frac{25.5}{6} \approx 4.25
]
Step 2: Gradient Descent Update
Gradient Descent Update Rules:
[
w_j := w_j - \alpha \cdot \frac{\partial J}{\partial w_j}
]
Where ( j = 0, 1, 2 ).
Compute Gradients:
The partial derivative of ( J ) with respect to ( w_j ) is:
[
\frac{\partial J}{\partial w_j} = -\frac{1}{m} \sum_{i=1}^{m} (y^{(i)} - \hat{y}^{(i)}) x_j^{(i)}
]
Where ( x_0^{(i)} = 1 ) (for the intercept term).
Let's compute the gradients for both Full Batch and Stochastic approaches.
A. Full Batch Gradient Descent
Compute the gradients:
- Gradient for ( w_0 ):
[
\frac{\partial J}{\partial w_0} = -\frac{1}{3} \sum_{i=1}^{3} (y^{(i)} - \hat{y}^{(i)}) \cdot 1
]
[
= -\frac{1}{3} [(-0.5) + 5 + (-0.5)] = -\frac{1}{3} [4] = -\frac{4}{3} \approx -1.3333
]
- Gradient for ( w_1 ):
[
\frac{\partial J}{\partial w_1} = -\frac{1}{3} \sum_{i=1}^{3} (y^{(i)} - \hat{y}^{(i)}) \cdot x_1^{(i)}
]
[
= -\frac{1}{3} [(-0.5 \cdot 2) + (5 \cdot -3) + (-0.5 \cdot 0)] = -\frac{1}{3} [-1 -15 + 0] = -\frac{-16}{3} = \frac{16}{3} \approx 5.3333
]
- Gradient for ( w_2 ):
[
\frac{\partial J}{\partial w_2} = -\frac{1}{3} \sum_{i=1}^{3} (y^{(i)} - \hat{y}^{(i)}) \cdot x_2^{(i)}
]
[
= -\frac{1}{3} [(-0.5 \cdot 1) + (5 \cdot 4) + (-0.5 \cdot 5)] = -\frac{1}{3} [-0.5 + 20 - 2.5] = -\frac{17}{3} \approx -5.6667
]
Update Rules:
[
w_j := w_j - \alpha \cdot \frac{\partial J}{\partial w_j}
]
Substituting ( \alpha = 0.01 ):
- Update ( w_0 ):
[
w_0 := 0 - 0.01 \times (-1.3333) = 0 + 0.013333 = 0.013333
]
- Update ( w_1 ):
[
w_1 := 1 - 0.01 \times 5.3333 = 1 - 0.053333 = 0.946667
]
- Update ( w_2 ):
[
w_2 := 0.5 - 0.01 \times (-5.6667) = 0.5 + 0.056667 = 0.556667
]
Updated Weights (Full Batch):
[
w_0 = 0.013333, \quad w_1 = 0.946667, \quad w_2 = 0.556667
]
B. Stochastic Gradient Descent
In Stochastic Gradient Descent (SGD), we update the weights for each training example individually.
Process:
For each training example ( i ), perform:
[
w_j := w_j - \alpha \cdot \frac{\partial J^{(i)}}{\partial w_j}
]
Where ( \frac{\partial J^{(i)}}{\partial w_j} = -(y^{(i)} - \hat{y}^{(i)}) \cdot x_j^{(i)} )
Initial Weights:
[
w_0 = 0, \quad w_1 = 1, \quad w_2 = 0.5
]
Iterate Through Each Example:
-
First Example (( i = 1 )):
-
( x_1 = 2 ), ( x_2 = 1 ), ( y = 2 )
-
Compute ( \hat{y}^{(1)} = 0 + 1 \cdot 2 + 0.5 \cdot 1 = 2.5 )
-
Error: ( 2 - 2.5 = -0.5 )
-
Gradients:
[
\frac{\partial J}{\partial w_0} = -(-0.5) \cdot 1 = 0.5
]
[
\frac{\partial J}{\partial w_1} = -(-0.5) \cdot 2 = 1.0
]
[
\frac{\partial J}{\partial w_2} = -(-0.5) \cdot 1 = 0.5
]
-
Update Weights:
[
w_0 := 0 - 0.01 \times 0.5 = -0.005
]
[
w_1 := 1 - 0.01 \times 1.0 = 0.99
]
[
w_2 := 0.5 - 0.01 \times 0.5 = 0.495
]
-
Second Example (( i = 2 )):
-
( x_1 = -3 ), ( x_2 = 4 ), ( y = 4 )
-
Compute ( \hat{y}^{(2)} = -0.005 + 0.99 \cdot (-3) + 0.495 \cdot 4 )
[
\hat{y}^{(2)} = -0.005 - 2.97 + 1.98 = -1.995
]
-
Error: ( 4 - (-1.995) = 5.995 )
-
Gradients:
[
\frac{\partial J}{\partial w_0} = -(5.995) \cdot 1 = -5.995
]
[
\frac{\partial J}{\partial w_1} = -(5.995) \cdot (-3) = 17.985
]
[
\frac{\partial J}{\partial w_2} = -(5.995) \cdot 4 = -23.98
]
-
Update Weights:
[
w_0 := -0.005 - 0.01 \times (-5.995) = -0.005 + 0.05995 = 0.05495
]
[
w_1 := 0.99 - 0.01 \times 17.985 = 0.99 - 0.17985 = 0.81015
]
[
w_2 := 0.495 - 0.01 \times (-23.98) = 0.495 + 0.2398 = 0.7348
]
-
Third Example (( i = 3 )):
-
( x_1 = 0 ), ( x_2 = 5 ), ( y = 2 )
-
Compute ( \hat{y}^{(3)} = 0.05495 + 0.81015 \cdot 0 + 0.7348 \cdot 5 = 0.05495 + 0 + 3.674 = 3.72895 )
-
Error: ( 2 - 3.72895 = -1.72895 )
-
Gradients:
[
\frac{\partial J}{\partial w_0} = -(-1.72895) \cdot 1 = 1.72895
]
[
\frac{\partial J}{\partial w_1} = -(-1.72895) \cdot 0 = 0
]
[
\frac{\partial J}{\partial w_2} = -(-1.72895) \cdot 5 = 8.64475
]
-
Update Weights:
[
w_0 := 0.05495 - 0.01 \times 1.72895 = 0.05495 - 0.0172895 \approx 0.0376605
]
[
w_1 := 0.81015 - 0.01 \times 0 = 0.81015
]
[
w_2 := 0.7348 - 0.01 \times 8.64475 = 0.7348 - 0.0864475 \approx 0.6483525
]
Final Updated Weights (After Stochastic Updates):
[
w_0 \approx 0.03766, \quad w_1 \approx 0.81015, \quad w_2 \approx 0.64835
]
Summary:
-
Initial Loss: ( J \approx 4.25 )
-
Full Batch Gradient Descent Step:
[
w_0 = 0.013333, \quad w_1 = 0.946667, \quad w_2 = 0.556667
]
-
Stochastic Gradient Descent Step:
[
w_0 \approx 0.03766, \quad w_1 \approx 0.81015, \quad w_2 \approx 0.64835
]
Question 6
Given the following training set with dogs data, create a decision tree based on attribute "Poisonous".
Dataset:
| Sample | Shape | Size | Smelly | Colour | Poisonous |
|---|
| A | 1 | 0 | 0 | 0 | 1 |
| B | 1 | 0 | 1 | 0 | 1 |
| C | 0 | 1 | 0 | 1 | 1 |
| D | 0 | 0 | 0 | 1 | 0 |
| E | 1 | 1 | 1 | 0 | 0 |
| F | 1 | 0 | 1 | 1 | 0 |
| G | 1 | 0 | 0 | 1 | 0 |
| H | 0 | 1 | 0 | 0 | 0 |
Attributes:
- Shape: 1 or 0
- Size: 1 or 0
- Smelly: 1 or 0
- Colour: 1 or 0
- Poisonous: 1 (poisonous) or 0 (not poisonous)
Solution
Objective:
Create a decision tree to predict the "Poisonous" attribute based on the given features.
Approach:
We will use the ID3 algorithm, which constructs a decision tree by selecting the attribute that maximizes the information gain at each step.
Steps:
- Calculate Entropy of the Target (Poisonous).
- Calculate Information Gain for each Attribute.
- Select the Attribute with the Highest Information Gain as the Root Node.
- Recursively Repeat for Subsets until Pure Nodes are Achieved.
Step 1: Calculate Entropy of the Target
[
\text{Entropy}(S) = -p_1 \log_2 p_1 - p_0 \log_2 p_0
]
Where ( p_1 ) is the proportion of poisonous samples, and ( p_0 ) the proportion of non-poisonous samples.
- Total Samples: 8
- Poisonous (1): 4 (A, B, C, D)
- Non-Poisonous (0): 4 (E, F, G, H)
[
p_1 = \frac{4}{8} = 0.5, \quad p_0 = \frac{4}{8} = 0.5
]
[
\text{Entropy}(S) = -0.5 \log_2 0.5 - 0.5 \log_2 0.5 = 1
]
Step 2: Calculate Information Gain for Each Attribute
The Information Gain (IG) for an attribute ( A ) is:
[
\text{IG}(S, A) = \text{Entropy}(S) - \sum_{v \in \text{Values}(A)} \frac{|S_v|}{|S|} \text{Entropy}(S_v)
]
Where:
- ( S_v ) is the subset of ( S ) where attribute ( A ) has value ( v ).
We will compute ( \text{IG} ) for each attribute: Shape, Size, Smelly, and Colour.
A. Information Gain for "Shape"
Values of Shape: 0, 1
-
Shape = 1:
- Samples: A, B, E, F, G
- Poisonous: A, B
- Non-Poisonous: E, F, G
- Count: 5
- Entropy:
[
p_1 = \frac{2}{5}, \quad p_0 = \frac{3}{5}
]
[
\text{Entropy} = -\frac{2}{5} \log_2 \frac{2}{5} - \frac{3}{5} \log_2 \frac{3}{5} \approx 0.971
]
-
Shape = 0:
- Samples: C, D, H
- Poisonous: C, D
- Non-Poisonous: H
- Count: 3
- Entropy:
[
p_1 = \frac{2}{3}, \quad p_0 = \frac{1}{3}
]
[
\text{Entropy} = -\frac{2}{3} \log_2 \frac{2}{3} - \frac{1}{3} \log_2 \frac{1}{3} \approx 0.918
]
Weighted Entropy:
[
\text{IG}(\text{Shape}) = 1 - \left( \frac{5}{8} \times 0.971 + \frac{3}{8} \times 0.918 \right) \approx 1 - (0.607 + 0.345) = 1 - 0.952 = 0.048
]
B. Information Gain for "Size"
Values of Size: 0, 1
-
Size = 0:
- Samples: A, B, D, F, G
- Poisonous: A, B, D
- Non-Poisonous: F, G
- Count: 5
- Entropy:
[
p_1 = \frac{3}{5}, \quad p_0 = \frac{2}{5}
]
[
\text{Entropy} = -\frac{3}{5} \log_2 \frac{3}{5} - \frac{2}{5} \log_2 \frac{2}{5} \approx 0.971
]
-
Size = 1:
- Samples: C, E, H
- Poisonous: C
- Non-Poisonous: E, H
- Count: 3
- Entropy:
[
p_1 = \frac{1}{3}, \quad p_0 = \frac{2}{3}
]
[
\text{Entropy} = -\frac{1}{3} \log_2 \frac{1}{3} - \frac{2}{3} \log_2 \frac{2}{3} \approx 0.918
]
Weighted Entropy:
[
\text{IG}(\text{Size}) = 1 - \left( \frac{5}{8} \times 0.971 + \frac{3}{8} \times 0.918 \right) \approx 1 - 0.952 = 0.048
]
C. Information Gain for "Smelly"
Values of Smelly: 0, 1
-
Smelly = 0:
- Samples: A, C, D, G, H
- Poisonous: A, C, D
- Non-Poisonous: G, H
- Count: 5
- Entropy:
[
p_1 = \frac{3}{5}, \quad p_0 = \frac{2}{5}
]
[
\text{Entropy} = -\frac{3}{5} \log_2 \frac{3}{5} - \frac{2}{5} \log_2 \frac{2}{5} \approx 0.971
]
-
Smelly = 1:
- Samples: B, E, F
- Poisonous: B
- Non-Poisonous: E, F
- Count: 3
- Entropy:
[
p_1 = \frac{1}{3}, \quad p_0 = \frac{2}{3}
]
[
\text{Entropy} = -\frac{1}{3} \log_2 \frac{1}{3} - \frac{2}{3} \log_2 \frac{2}{3} \approx 0.918
]
Weighted Entropy:
[
\text{IG}(\text{Smelly}) = 1 - \left( \frac{5}{8} \times 0.971 + \frac{3}{8} \times 0.918 \right) \approx 1 - 0.952 = 0.048
]
D. Information Gain for "Colour"
Values of Colour: 0, 1
-
Colour = 0:
- Samples: A, B, E, H
- Poisonous: A, B
- Non-Poisonous: E, H
- Count: 4
- Entropy:
[
p_1 = \frac{2}{4} = 0.5, \quad p_0 = 0.5
]
[
\text{Entropy} = -0.5 \log_2 0.5 - 0.5 \log_2 0.5 = 1
]
-
Colour = 1:
- Samples: C, D, F, G
- Poisonous: C, D
- Non-Poisonous: F, G
- Count: 4
- Entropy:
[
p_1 = \frac{2}{4} = 0.5, \quad p_0 = 0.5
]
[
\text{Entropy} = 1
]
Weighted Entropy:
[
\text{IG}(\text{Colour}) = 1 - \left( \frac{4}{8} \times 1 + \frac{4}{8} \times 1 \right) = 1 - (0.5 + 0.5) = 0
]
Summary of Information Gains:
| Attribute | Information Gain |
|---|
| Shape | 0.048 |
| Size | 0.048 |
| Smelly | 0.048 |
| Colour | 0 |
Decision:
All attributes except "Colour" have the same Information Gain of 0.048. To break the tie, we can select the first attribute or choose based on other criteria.
Choosing "Shape" as Root Node.
Step 3: Create Root Node and Split the Dataset
-
Root: Shape
-
Branches:
Branch 1: Shape = 1
-
Samples: A, B, E, F, G
-
Poisonous: A, B
-
Non-Poisonous: E, F, G
-
Entropy: 0.971 (as computed earlier)
-
Potential Further Splits: Size, Smelly, Colour
Calculate Information Gain for Sub-Attributes within Shape = 1:
Repeat the IG calculation for "Size", "Smelly", and "Colour" within this subset.
However, since "Shape", "Size", and "Smelly" all had the same initial IG, similar behavior is expected.
For brevity, let's assume "Smelly" provides the best split here.
Sub-Branch: Smelly
-
Smelly = 0:
- Samples: A, G
- Poisonous: A
- Non-Poisonous: G
- Entropy:
[
p_1 = \frac{1}{2}, \quad p_0 = \frac{1}{2}, \quad \text{Entropy} = 1
]
- Further Splits: Not beneficial; decide majority or stop.
-
Smelly = 1:
- Samples: B, E, F
- Poisonous: B
- Non-Poisonous: E, F
- Entropy:
[
p_1 = \frac{1}{3}, \quad p_0 = \frac{2}{3}, \quad \text{Entropy} = 0.918
]
- Further Splits: Not beneficial; decide majority or stop.
Branch Decision:
At this point, further splits do not significantly improve.
Make leaf nodes based on majority:
- Shape = 1, Smelly = 0: Majority Poisonous = 1
- Shape = 1, Smelly = 1: Majority Non-Poisonous = 0
Branch 2: Shape = 0
Again, similar steps apply.
Assume no further splits are beneficial.
Leaf Node Decision:
- Shape = 0: Majority Poisonous = 1
Final Decision Tree:
[Shape]
/ \
1 0
/ \ \
Smelly 0 Smelly 1 Poisonous = 1
/ \
Poisonous=1 Non-Poisonous=0
ASCII Diagram:
[Shape]
/ \
Shape=1 Shape=0
/ \
Smelly=0 Smelly=1
/ \
Poisonous=1 Non-Poisonous=0
Interpretation:
-
If Shape = 1:
- If Smelly = 0: Poisonous
- If Smelly = 1: Non-Poisonous
-
If Shape = 0: Poisonous
Question 7
Describe the motivations and explain the aims of the PCA algorithm.
Solution
Principal Component Analysis (PCA) Overview:
PCA is an unsupervised dimensionality reduction technique used to simplify data while preserving as much variance as possible. It transforms the data into a new coordinate system, where the greatest variance lies on the first principal component, the second greatest variance on the second principal component, and so on.
Motivations for PCA:
-
Dimensionality Reduction:
- High-dimensional data can be complex and computationally intensive.
- Reducing dimensions simplifies models, making them faster and less prone to overfitting.
-
Noise Reduction:
- By retaining components with higher variance, PCA can filter out noise (usually associated with lower variance).
-
Data Visualization:
- Reducing data to 2 or 3 principal components allows for visualization and better understanding of data structures.
-
Feature Correlation:
- PCA identifies and leverages the underlying correlations among features.
-
Improving Performance:
- Reducing the number of features can improve the performance of machine learning algorithms, especially those sensitive to the curse of dimensionality.
Aims of PCA:
-
Maximize Variance:
- Ensure that the principal components capture the maximum possible variance from the data.
-
Orthogonal Components:
- Principal components are uncorrelated (orthogonal) to each other, eliminating redundancy.
-
Retain Essential Information:
- While reducing dimensions, PCA aims to retain the most informative aspects of the data.
-
Transform Data:
- Convert data into a new space where the axes (principal components) represent directions of maximum variance.
How PCA Works:
-
Standardization:
- Center the data by subtracting the mean for each feature to ensure that each feature contributes equally.
-
Covariance Matrix Computation:
- Calculate the covariance matrix to understand how variables relate to each other.
-
Eigen Decomposition:
- Compute eigenvectors and eigenvalues of the covariance matrix.
- Eigenvectors: Directions of maximum variance (principal components).
- Eigenvalues: Magnitude of variance captured by each principal component.
-
Selecting Principal Components:
- Choose the top ( k ) eigenvectors corresponding to the largest eigenvalues.
- These ( k ) eigenvectors form the new feature space.
-
Transforming Data:
- Project the original data onto the selected principal components to obtain the reduced-dimensional representation.
Mathematical Representation:
Given a data matrix ( \mathbf{X} ) with ( n ) samples and ( p ) features:
- Standardize the Data:
[
\mathbf{X}_{\text{std}} = \frac{\mathbf{X} - \mu}{\sigma}
]
Where ( \mu ) is the mean and ( \sigma ) is the standard deviation of each feature.
- Compute Covariance Matrix:
[
\mathbf{Cov} = \frac{1}{n-1} \mathbf{X}{\text{std}}^\top \mathbf{X}{\text{std}}
]
- Eigen Decomposition:
Find eigenvectors ( \mathbf{v}_i ) and eigenvalues ( \lambda_i ) such that:
[
\mathbf{Cov} \mathbf{v}_i = \lambda_i \mathbf{v}_i
]
- Sort Eigenvectors:
Order eigenvectors by descending eigenvalues.
- Project Onto New Feature Space:
[
\mathbf{Y} = \mathbf{X}_{\text{std}} \mathbf{W}_k
]
Where ( \mathbf{W}_k ) consists of the top ( k ) eigenvectors.
Example Illustration:
Original Data (High Dimensional)
|
V
PCA Transformation
|
V
Reduced Dimensional Data (Principal Components)
Benefits of PCA:
- Efficiency: Reduces computational requirements.
- Improved Visualization: Facilitates understanding of data structures.
- Noise Reduction: Enhances signal-to-noise ratio.
- Uncovering Hidden Patterns: Reveals relationships not obvious in high dimensions.
Limitations:
- Linearity: PCA assumes linear relationships among features.
- Interpretability: Principal components may not have a straightforward interpretation.
- Variance vs. Information: High variance does not always equate to important information.
Question 8
Which are the differences between a classical neural network and an autoencoder?
Solution
Neural Networks Overview:
A classical neural network (often referred to as a feedforward neural network) is designed for various predictive tasks, such as classification or regression. In contrast, an autoencoder is a type of neural network specifically designed for unsupervised learning tasks like dimensionality reduction, feature learning, and data reconstruction.
Key Differences:
-
Architecture:
-
Purpose and Objective:
-
Loss Function:
-
Training Data:
-
Representation Learning:
Visual Representation:
- Classical Neural Network:
Input Layer -> Hidden Layers -> Output Layer (Predictions)
Input Layer
|
Encoder
|
Bottleneck Layer (Latent Space)
|
Decoder
|
Reconstructed Output Layer
Summary Table:
| Aspect | Classical Neural Network | Autoencoder |
|---|
| Purpose | Supervised prediction (classification, regression) | Unsupervised learning (dimensionality reduction, reconstruction) |
| Architecture | Input -> Hidden Layers -> Output | Input -> Encoder -> Bottleneck -> Decoder -> Output |
| Training Data | Labeled | Unlabeled |
| Objective | Minimize prediction error on targets | Minimize reconstruction error |
| Applications | Image recognition, NLP, forecasting | Denoising, anomaly detection, data compression |
| Latent Representation | Not explicitly focused on | Central focus |
Conclusion
These solutions provide a comprehensive understanding of various machine learning concepts, ranging from linear regression and logistic regression to decision trees, PCA, and neural network architectures. Mastery of these topics is fundamental for building and evaluating effective machine learning models.