Practice
import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
y = np.array([1, 0, 1])
selector = SelectKBest(score_func=f_classif, k=2)
X_new = selector.fit_transform(X, y)
print(X_new)What does the above code output?Solution
Step 1: Understand SelectKBest.
SelectKBest selects the top k features based on the highest scores from the scoring function.Step 2: Determine the output.
f_classif scores features based on ANOVA F-value. Here, features 2 and 3 have the highest scores.Final Answer:
Option A -> Option AQuick Check:
Top k features by score. [OK]
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?Solution
Step 1: Understand np.dot behavior
np.dot for 1D arrays computes the dot product, which is a scalar.Step 2: Calculate the dot product
1*4 + 2*5 + 3*6 = 32.Final Answer:
Option C -> Option CQuick Check:
np.dot 1D arrays = scalar [OK]
from sklearn.linear_model import Ridge
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([1, 2, 3])
model = Ridge(alpha=0.0)
model.fit(X, y)
print(model.coef_)What does this code output?Solution
Step 1: Understand the effect of alpha=0 in Ridge.
Setting alpha to zero in Ridge regression makes it equivalent to ordinary least squares.Step 2: Eliminate wrong options.
Alpha=0 does not cause an error. Zero coefficients would require L1 regularization. Random coefficients are incorrect as the model still fits the data.Final Answer:
Option B -> Option BQuick Check:
Alpha=0 in Ridge = OLS. [OK]
Solution
Step 1: Consider dataset size and model complexity.
Small datasets are prone to overfitting with complex models.Step 2: Evaluate the impact of each option.
Polynomial features (A, C) increase complexity significantly, leading to overfitting. Interaction terms (B, D) add complexity but less than polynomial features, making them more suitable for small datasets.Final Answer:
Option D -> Option DQuick Check:
Small data: prefer interaction terms to avoid overfitting. [OK]
Solution
Step 1: Identify the challenge of high dimensions.
Matrix factorization reduces dimensionality effectively.Step 2: Eliminate wrong options.
Implement a simple content-based filtering system. doesn't reduce dimensions. Apply a deep learning model with many hidden layers. may overfit due to high dimensions. Use a rule-based system to manually curate recommendations. is not scalable for large datasets.Final Answer:
Option B -> Option BQuick Check:
Matrix factorization reduces dimensions, aids recommendations. [OK]
