Practice
1. What will be the output of the following Python code snippet using NumPy for a simple linear regression prediction?
import numpy as np
X = np.array([[1, 2], [3, 4]])
weights = np.array([0.5, 0.5])
predictions = np.dot(X, weights)
print(predictions)easy
Solution
Step 1: Calculate the dot product
The dot product of X and weights is calculated as follows: [1*0.5 + 2*0.5, 3*0.5 + 4*0.5] = [2.0, 4.0].Step 2: Eliminate wrong options
Options A, B, and D result from incorrect calculations of the dot product.Final Answer:
Option D -> Option DQuick Check:
Dot product: sum of element-wise products. [OK]
Hint: Dot product: sum of element-wise products. [OK]
Common Mistakes:
2.
import shap
import xgboost
# Load data
X, y = shap.datasets.boston()
# Train model
model = xgboost.XGBRegressor().fit(X, y)
# Explain the model's predictions using SHAP
explainer = shap.Explainer(model, X)
shap_values = explainer(X)
# What is the shape of shap_values?easy
Solution
Step 1: Understand SHAP output.
SHAP values are calculated for each feature of each instance.Step 2: Determine the shape based on input data.
The dataset has 506 instances and 13 features, so SHAP values will have the shape (506, 13).Final Answer:
Option C -> Option CQuick Check:
SHAP values match data shape: (instances, features). [OK]
Hint: SHAP values match data shape: (instances, features). [OK]
Common Mistakes:
3. Which regularization technique is most suitable for feature selection in a linear regression model?
easy
Solution
Step 1: Identify the purpose of feature selection.
L1 Lasso regularization can shrink some coefficients to zero, effectively selecting features.Step 2: Eliminate wrong options.
L2 Ridge does not shrink coefficients to zero. Elastic Net combines L1 and L2 but is not as direct for feature selection. Dropout is not used in linear regression.Final Answer:
Option A -> Option AQuick Check:
L1 Lasso zeros coefficients for selection. [OK]
Hint: L1 Lasso zeros coefficients for selection. [OK]
Common Mistakes:
4.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
result = np.dot(x, y)
print(result)What does this code output?easy
Solution
Step 1: Understand the dot product operation.
The dot product of two vectors is the sum of the products of their corresponding elements.Step 2: Calculate the dot product.
(1*4) + (2*5) + (3*6) = 32.Final Answer:
Option C -> Option CQuick Check:
Dot product sums element-wise products. [OK]
Hint: Dot product sums element-wise products. [OK]
Common Mistakes:
5. A model using wrapper methods for feature selection is overfitting. What is the most likely cause?
medium
Solution
Step 1: Identify overfitting causes.
Data leakage during feature selection can lead to overfitting as the model sees information from the validation set.Step 2: Eliminate wrong options.
Too few features usually lead to underfitting. An inappropriate scoring function affects feature ranking, not directly overfitting. High variance can cause overfitting but is not specific to wrapper methods.Final Answer:
Option A -> Option AQuick Check:
Data leakage = overfitting risk. [OK]
Hint: Data leakage causes overfitting. [OK]
Common Mistakes:
