logo
  • userLoginStatus

Welcome

Our website is made possible by displaying online advertisements to our visitors.
Please disable your ad blocker to continue.

Current View

Computer Engineering - Machine Learning

Full exam

DIPARTIMENTO DI ELETTRONICA, INFORMAZIONE E BIOINGEGNERIA Politecnico di Milano Machine Learning (Code: 097683)June 17, 2025 Surname: Name: Student ID: Row: Column:Time:2hoursMaximumMarks:33 •The following exam is composed of8 exercises(one per page). The first page needs to be filled with yourname, surname and student ID. The following pages should be used only in the large squarespresent on each page. Any solution provided either outside these spaces orwithout a motivationwill not be considered for the final mark. •During this exam you arenot allowed to use electronic devices, such as laptops, smartphones, tablets and/or similar. As well, you are not allowed to bring with you any kind of note, book, written scheme, and/or similar. You are also not allowed to communicate with other students during the exam. •The first reported violation of the above-mentioned rules will be annotated on the exam and will be considered for the final mark decision. The second reported violation of the above-mentioned rules will imply the immediate expulsion of the student from the exam room and theannulment of the exam. •You are allowed to write the exam either with a pen (black or blue) or a pencil. It is your responsibility to provide a readable solution. We will not be held accountable for accidental partial or total cancellation of the exam. •The exam can be written either inEnglishorItalian. •You are allowed to withdraw from the exam at any time without any penalty. You are allowed to leave the room not early than half the time of the duration of the exam. You are not allowed to keep the text of the exam with you while leaving the room. •Three of the points will be given on the basis on how quick you are in solving the exam.If you finish earlier than 30 min before the end of the exam you will get 3 points, if you finish earlier than 20 min you will get 2 points and if you finish earlier than 10 min you will get 1 point (the points cannot be accumulated). •The box on Page 10 can only be used to complete the Exercises 7, and/or 8.Ex. 1Ex. 2Ex. 3Ex. 4Ex. 5Ex. 6Ex. 7Ex. 8TimeTot. / 7/ 7/ 2/ 2/ 2/ 2/ 4/ 4/ 3/ 33 Student’s name: Please go on to the next page. . . Computer Science Computer Science — Machine Learning Page 2 of 10 Exercise 1 (7marks) DescribePCAtechnique, how it works, and what its purpose is.Student’s name: Please go on to the next page. . . Computer Science Computer Science — Machine Learning Page 3 of 10 Exercise 2 (7marks) Compare thePerceptronalgorithm andLogistic Regressionin terms of their update rules, loss functions, and convergence properties.Student’s name: Please go on to the next page. . . Computer Science Computer Science — Machine Learning Page 4 of 10 Exercise 3 (2marks) Consider the following Python code snippet:1 X = z s c o r e ( d a t a s e t [ [ ’ s e p a l - l e n g t h ’ , ’ s e p a l - w i d t h ’ , ’ p e t a l - l e n g t h ’ ] ] . v a l u e s )2 t = n p . w h e r e ( d a t a s e t [ ’ c l a s s ’ ] . v a l u e s = = ’ I r i s - s e t o s a ’ , 1 , - 1 )3 4 m o d e l = L i n e a r R e g r e s s i o n ( )5 m o d e l . f i t ( X , t )6 7 r a w _ p r e d i c t i o n s = m o d e l . p r e d i c t ( X )8 c l a s s _ p r e d i c t i o n s = n p . w h e r e ( r a w _ p r e d i c t i o n s > 0 , 1 , - 1 )9 a c c u r a c y = n p . m e a n ( c l a s s _ p r e d i c t i o n s = = t ) 1.Describe the procedure performed in the code above, detailingline-by-linewhat it does and the general goal of the snippet. 2.Tell what is the purpose of line 2 and if it is correct. 3.Is the approach used in the snippet appropriate for the task? Would you use a differentapproach? 1.The goal of the snitppet is solving a binary classification problem using ordinary leastsquares (OLS) regression on the Iris dataset. It begins by standardizing the three selected features—sepal length, sepal width, and petal length—to zero mean and unit variance (line 1). It then converts the categorical labels into a numeric vector of +1 for “Iris-setosa” and –1 for all other species (line 2), preparing them for regression. Next, an ordinary least squares model is instantiated (line 3) and fitted to the standardized inputs and encoded targets (line 4), learning a linear mapping. Continuous predictions are produced by applying this mapping to the inputs (line 5), then thresholded at zero to recover binary class labels (line 6). Finally, training accuracy is computed as the fraction of correct +1/–1 predictions (line 7). 2.Line 2 purpose is encoding the target variable. It is correct and necessary because OLSrequires numeric targets; mapping to +1/-1 aligns the decision threshold with zero. 3.Although the approach is technically training a classifier, using OLS (i.e., squared-error loss)is not a good choice for classification: it does not produce calibrated probabilities, penalizes large margins excessively, and can give suboptimal boundaries. Models such as logistic regression or linear SVM, which optimize classification-appropriate losses, are generally more suitable.Student’s name: Please go on to the next page. . . Computer Science Computer Science — Machine Learning Page 5 of 10 Exercise 4 (2marks) Tell if the following statements about reinforcement learning are true or false. Motivate your answers. 1.Temporal Difference (TD) methods require a complete episode to end before they can beginupdating value function estimates. 2.Q-learning is guaranteed to converge to the optimal action-value function,Q∗ , only if the agent’s behavior policy sufficiently explores all state-action pairs. 3.SARSA is considered an on-policy algorithm because it involves an incremental update ofthe action-value function. 4.One of the advantages of TD methods over Monte Carlo methods is their lower variance,as they update estimates based on other learned estimates rather than waiting for a final outcome. 1.False.Temporal Difference (TD) methods employ bootstrapping and updating value estimates at each time step (e.g. after observingS t, A t, R t+1, S t+1), without waiting for the episode to terminate. 2.True.Q-learning’s convergence proof assumes a sufficiently exploring behavior policy (e.g. GLIE) so that every state–action pair is visited infinitely often and the learning rates satisfy the usual stochastic-approximation conditions. 3.False.SARSA is on-policy because it updates using the action actually taken by the same policy that generates behavior. 4.True.By bootstrapping on existing value estimates rather than waiting for full returns, TD methods exhibit lower variance than Monte Carlo methods (though at the cost of introducing bias).Student’s name: Please go on to the next page. . . Computer Science Computer Science — Machine Learning Page 6 of 10 Exercise 5 (2marks) Tell if the following statements about regularization are true or false. Motivate your answers. 1.In ridge regression, increasing the penalty weightλalways increases the model’s bias. 2.Lasso (L1) regularization can drive some regression coefficients exactly to zero. 3.Adding a regularization term to the loss can never worsen performance on the training set. 4.Expanding a linear model with higher-degree polynomial features tends to decrease bias butincrease variance. 1.True.In ridge regression the penalty termλP jw2 jshrinks the weights w jtoward zero. As λincreases, the model becomes less flexible (more biased) even though variance decreases. 2.True.Lasso adds anℓ 1penalty λP j| w j| . This penalty has “corners” at zero in the loss geometry, which allows some weights to be driven exactly to zero, yielding sparse solutions. 3.False.Adding regularization increases the trade-off toward smaller weights. While the regularizedob jective always decreases (or stays the same), theunregularizedtraining error (e.g. MSE) canincrease, so plain training-set performance may worsen. 4.True.Introducing higher-degree polynomial features enlarges the hypothesis space, making it easier to fit the data (lower bias) but more sensitive to sampling noise (higher variance).Student’s name: Please go on to the next page. . . Computer Science Computer Science — Machine Learning Page 7 of 10 Exercise 6 (2marks) You are a ML expert working for the urban planning department of a ma jor city. Consider the following two challenges: 1.Bus Delay Prediction: To better inform commuters, the city wants to provide real-time predictions for bus delays. Your task is to predict the delay (in minutes) of a specific bus on its route. You have access to a large historical dataset containing information about past bus trips. 2.Traffic Light Control: To ease congestion at a particularly busy intersection, you are asked to design an intelligent traffic light controller. This system can observe the number of vehicles approaching the intersection from all directions in real-time. At any moment, it can decide whether to extend the current green light or switch to the next phase. The ob jective is to minimize the average vehicle waiting time. Model the two problems described above as ML problems, specifying the data (features and target or states, actions, and reward), the problem class, and a specific method or algorithm to solve them.Bus Delay Prediction.This is a classic supervised learning problem. Specifically, it is a regression task, as the goal is to predict a continuous value: the delay in minutes. To solve this, we would use a historical dataset where the features are predictive variables like the bus route, time of day, location, and weather conditions. The target variable is simply the recorded delay for each trip. Linear regression models or gradient boosting regressors are suitable methods for this task. Traffic Light Control.The task falls into the domain of Reinforcement Learning (RL). Here, an agent must learn an optimal decision-making policy through direct interaction with its environment. The problem can be modeled as follows: State: A snapshot of the current traffic situation (e.g., vehicle counts per lane, current light phase). Actions: The decisions the controller can make (e.g., extend the current green light or switch to the next phase). Reward: we could design it as the negative of the average vehicle waiting time. Suitable algorithms are Q-learning or SARSA.Student’s name: Please go on to the next page. . . Computer Science Computer Science — Machine Learning Page 8 of 10 Exercise 7 (4marks) Consider a binary classification problem with input spacex= [x 1, x 2] ∈R2 and feature mapping ϕ(x) = [x2 1,√2 x 1x 2, x2 2]. After training a hard-margin linear SVM in this feature space, the resulting classifier has the following weight vector and bias: w= 1 −1 0.5 , b=−2 Answer the following questions. Motivate your reasoning and show any necessary computations.1.Computeϕ([1,2]) and classify the point using the SVM. 2.Find a point in the input space that lies on the decision boundary in the feature space. 3.Could the point [0,1] be a support vector? Justify your answer. 4.Could we have trained the SVM in the dual (kernel) space? If yes, provide a kernel. If no,motivate your answer. 1.We have: ϕ([1,2]) = 1 2 √2 ·1·2 22 = 1 2√2 4 . Compute the decision function:f([1,2]) = 1·1 + (−1)·2√2 + 0 .5·4−2 = 1−2√2 1, the point cannot be a support vector. 4.We can always perform training in the dual space provided that we are able to define thecorresponding kernel. In this case, we just consider the kernel induced by the feature mapping: k(x, y) =ϕ(x)⊤ ϕ(y) =x2 1y2 1+ 2 x 1x 2y 1y 2+ x2 2y2 2= ( x 1y 1+ x 2y 2)2 , which is a polynomial kernel.Student’s name: Please go on to the next page. . . Computer Science Computer Science — Machine Learning Page 9 of 10 Exercise 8 (4marks) Consider a multi-armed bandit problem with two armsa 1, a 2and binary rewards. The reward distribution of arma 1is Bernoulli with parameter µ 1= 0 .6. The reward distribution of arma 2is Bernoulli with parameterµ 2= 0 .4. By the end of roundt= 9, agentAhas observed the following: Arm Number of successes Number of failuresa13 2 a21 3 AssumingAis playing UCB1, answer the following questions, writing all the necessary computations: 1.Do you have enough information to compute the random regret at the end of roundt= 9? And the pseudo regret (a.k.a. expected regret)? If so, compute them. 2.What action willAplay at roundt′ = 10? Hint:|p2 ln(10) /5−p2 ln(10) /4|36 −14 = 0 .25>0.21), soAwill play againa 1with probability 1.Student’s name: End of exam