Skip to main content

Module 4 Test: Machine Learning

This is your progress tracker . 20 questions: 5 Easy, 10 Moderate, 5 Hard . First solve all the questions yourself (before running the code in Colab, work out the output in your head), then check with the Answer Key.

ScoreMeaning
18 to 20Excellent! Your grip on Module 4 is strong.
12 to 17Good. Re-read the chapters of the questions you got wrong.
Below 12Do not worry. Re-read Chapters 12 to 20 and run the code yourself, then take the test again.
EASY​
Q1. In Supervised Learning, what is present along with the training data?​

A) Only features, no answers

B) Answers (label / target) along with the features

C) Only images

D) Only random numbers

​
EASY​
Q2. What is the correct meaning of overfitting?​

A) The model is bad on both train and test

B) The model is very good on train but bad on new (test) data

C) The model runs very fast

D) The data has missing values

​
EASY​
Q3. What does "K" mean in K-Means?​

A) Number of neighbours

B) Number of clusters (groups)

C) Number of features

D) Number of iterations

​
EASY​
Q4. Which of the following metrics tells how many of the actual positive cases the model caught?​

A) Precision B) Recall C) Accuracy

D) Specificity

​
EASY​
Q5. What is the basic purpose of PCA?​

A) Filling missing values

B) Reducing the number of features while keeping as much information as possible

C) Creating labels

D) Turning data into text

​
MODERATE​
Q6. What will be the output of this code?​
import numpy as np
from sklearn.model_selection
import train_test_split
X = np.arange(100).reshape(50, 2) Xtr,
Xte = train_test_split(X,
test_size=0.2,
random_state=0)
print(len(Xtr), len(Xte))

A) 30 20 B) 40 10

C) 45 5 D) 10 40

MODERATE​
Q7. What will be the output of this code (precision, recall)?​
from sklearn.metrics
import precision_score, recall_score
y_true = [1, 1, 1, 0, 0, 0, 0, 0]
y_pred = [1, 1, 0, 1, 1, 0, 0, 0]
print(round(precision_score(y_true, y_pred), 2), round(recall_score(y_true, y_pred), 2))

A) 0.67 0.5 B) 0.5 0.67 C) 0.5 0.5 D) 0.67 0.67

​
MODERATE​
Q8. In a Decision Tree, if two classes are mixed in equal parts (50% - 50%), what will the Gini impurity be?​
print(1 - (0.5 ** 2 + 0.5 ** 2))

A) 0.0 B) 0.25 C) 0.5 D) 1.0

MODERATE​
Q9. What will be the output of this code?​
import numpy as np
from sklearn.preprocessing
import StandardScaler
print(StandardScaler().fit_transform(np.array([[1], [2], [3]])).ravel().round(2))

A) [0. 0.5 1.] B) [-1.22 0. 1.22] C) [-1. 0. 1.] D) [1 2 3]

​
MODERATE​
Q10. Which of the following is data leakage?​

A) Fitting StandardScaler only on the train data

B) Fitting the scaler on the whole data and then doing the train/test split

C) Putting the scaler inside a Pipeline

D) Fitting a separate scaler in every fold of cross-validation

​
MODERATE​
Q11. How many scores does cross_val_score return with 5-fold cross-validation?​
from sklearn.datasets
import load_iris
from sklearn.linear_model
import LogisticRegression
from sklearn.model_selection
import cross_val_score X,
y = load_iris(return_X_y=True)
print(len(cross_val_score(LogisticRegression(max_iter=500), X, y,
cv=5)))

A) 1 B) 3 C) 5 D) 150

​
MODERATE​
Q12. What will be the output of this code (the sizes of the K-Means clusters)?​
import numpy as np
from sklearn.cluster import KMeans
X = [[1, 1], [1.2, 1], [8, 8], [8.2, 8]]
labels = KMeans(n_clusters=2, n_init=10, random_state=0).fit(X).labels_
print(sorted(np.bincount(labels).tolist()))

A) [1, 3] B) [2, 2] C) [4] D) [1, 1, 2]

​
MODERATE​
Q13. What happens when C is made very large in SVM?​

A) The margin is wide and the model simple (underfit)

B) The margin is narrow and the model moves toward memorizing the train data (overfit)

C) The model becomes fast

D) Nothing changes

​
MODERATE​
Q14. What is the output of the sigmoid function at z = 0?​
import numpy as np
print(1 / (1 + np.exp(0)))

A) 0.0 B) 0.5 C) 1.0 D) -1.0

​
MODERATE​
Q15. Which of the following algorithms does not need feature scaling?​

A) KNN B) SVM (rbf) C) PCA D) Random Forest

​
HARD​

Q16. Two features are perfectly correlated (y = 2x). How much spread (variance) does the first component of PCA capture?

import numpy as np
from sklearn.decomposition
import PCA
X = np.array([[1, 2], [2, 4], [3, 6], [4, 8]])
print(round(PCA(n_components=1).fit(X).explained_variance_ratio_[0], 2))

A) 0.50 B) 0.75 C) 1.0 D) 0.0

​
HARD​
Q17. What happens to the slope when alpha = 10 is applied in ridge regression? (the true slope is 2.0)​
import numpy as np
from sklearn.linear_model
import Ridge
X = np.array([[1], [2], [3], [4]])
y = [2, 4, 6, 8]
print(round(Ridge(alpha=10).fit(X, y).coef_[0], 2))

A) 2.0 B) 0.67 C) 10.0 D) 0.0

​
HARD​

Q18. On probabilities [0.2, 0.45, 0.6, 0.9], what will the predictions be with thresholds 0.5 and 0.4?

import numpy as np
probs = np.array([0.2, 0.45, 0.6, 0.9])
print((probs >= 0.5).astype(int).tolist(), (probs >= 0.4).astype(int).tolist())

A) [0, 0, 1, 1] [0, 1, 1, 1]

B) [0, 1, 1, 1] [0, 0, 1, 1] C) [0, 0, 1, 1] [0, 0, 1, 1] D) [1, 1, 1, 1] [1, 1, 1, 1]

​
HARD​

Q19. In a dataset 95% are negative and 5% positive. The model always says "negative". What will the accuracy and recall be?

from sklearn.metrics
import accuracy_score, recall_score
y_true = [0] * 95 + [1] * 5
y_pred = [0] * 100
print(accuracy_score(y_true, y_pred), recall_score(y_true, y_pred))

A) 0.95 0.95 B) 0.95 0.0 C) 0.5 0.0 D) 1.0 1.0

​
HARD​

Q20. In KNN, what will the train accuracy be (on a small dataset) at K = 1 and K = 4?

import numpy as np
from sklearn.neighbors
import KNeighborsClassifier
X = np.array([[0], [1], [2], [3]])
y = [0, 1, 0, 1]
print(KNeighborsClassifier(n_neighbors=1).fit(X, y).score(X, y), KNeighborsClassifier(n_neighbors=4).fit(X, y).score(X, y))

A) 1.0 1.0 B) 0.5 0.5 C) 1.0 0.5 D) 0.5 1.0

MODULE 4 TEST​

Module 4 Answer Key (Answers and explanations)​

#LevelAnswerExplanation
1EasyB) Answers (label / target)
along with the features
In supervised learning the correct answer (label) of every example is
present, from which the model learns the relationship from input to
answer.
2EasyB) The model is very good on
train but bad on new (test)
data
In overfitting the model memorizes the training data, so the train score is
very high and the test score is low.
3EasyB) Number of clusters (groups)In K-Means, K is the number of clusters that we state beforehand. (In
KNN, K is the number of neighbours.)
4EasyB) RecallRecall = TP / (TP + FN): of all the actual positives, how many were caught.
Precision tells how many of those called positive were correct.
5EasyB) Reducing the number of
features while keeping as
much information as possible
PCA creates new axes (principal components) that capture the most
spread, and reduces the dimensions by keeping fewer components.
6ModerateB) 40 1020% (10 rows) of the 50 rows went to the test, and the remaining 40 to
train.
7ModerateB) 0.5 0.67TP = 2, FP = 2, FN = 1. Precision = 2 / (2 + 2) = 0.5. Recall = 2 / (2 + 1) =
0.67.
8ModerateC) 0.5Gini = 1 - (0.5² + 0.5²) = 0.5, which is the highest (most mixed) impurity
for two classes.
9ModerateB) [-1.22 0. 1.22]Mean = 2, std (population) = 0.816. Z-score: (1-2)/0.816 = -1.22, 0, +1.22.
10ModerateB) Fitting the scaler on the
whole data and then doing the
train/test split
Fitting on the whole data lets the information of the test data (mean,
std) go into the scaler. The right way: split first, fit only on train.
11ModerateC) 55 folds give 5 different train/test splits, so we get 5 scores.
12ModerateB) [2, 2]Two points are near (1, 1) and two near (8, 8), so at K = 2 each cluster
gets 2 points.
13ModerateB) The margin is narrow and
the model moves toward
memorizing the train data
(overfit)
A large C punishes mistakes harshly, so the margin becomes narrow and
the model, trying to get every training point right, can overfit.
14ModerateB) 0.5sigmoid(0) = 1 / (1 + 1) = 0.5. That is why in logistic regression the
probability on the decision boundary is 0.5.
15ModerateD) Random ForestTrees and forests work on questions (thresholds), not on distance, so no
scaling is needed. KNN, SVM and PCA need it.
16HardC) 1.0All the points lie on one straight line, so a single axis (PC1) captures the
whole spread: 100% (1.0).
17HardB) 0.67Ridge shrinks the coefficient: slope = Sxy / (Sxx + alpha) = 10 / (5 + 10) =
0.67. Increasing alpha makes the slope even smaller.
18HardA) [0, 0, 1, 1] [0, 1, 1, 1]When the threshold is lowered (0.4) the 0.45 point also becomes
positive: recall rises, but so do false alarms.
19HardB) 0.95 0.0Accuracy is 95% but not a single positive was caught (recall = 0). This is
the Accuracy Paradox.
20HardC) 1.0 0.5At K = 1 every point is its own nearest neighbour (100% memorizing). At
K = 4 the vote of all points is tied 2 - 2, so a single class comes out and
only half are right (0.5).

Your score: ______ / 20