Data Science with Python Certification Data Science with Python Supervised Learning Models 3 — Questions and Answers
Question 1: When using sklearn's train_test_split, what does the stratify parameter do?
- Shuffles data randomly before splitting
- Preserves the class distribution in both train and test sets (Correct answer)
- Normalizes feature values across splits
- Ensures no duplicate samples appear in either set
Correct answer: Preserves the class distribution in both train and test sets
stratify=y ensures the proportion of each class label is maintained in both the training and test subsets.
Question 2: Which of the following is a key difference between bagging and boosting ensemble methods?
- Bagging builds trees sequentially; boosting builds them in parallel
- Bagging focuses on correcting errors of prior models; boosting does not
- Boosting trains models sequentially, each correcting the previous model's errors (Correct answer)
- Bagging uses only decision trees; boosting uses any base learner
Correct answer: Boosting trains models sequentially, each correcting the previous model's errors
Boosting trains each new model to focus on the samples that previous models misclassified, building a strong learner sequentially.
Question 3: In a Ridge regression model, what is the effect of increasing the regularization parameter alpha?
- Coefficients grow larger to fit data more tightly
- The model becomes more complex
- Coefficients shrink toward zero, reducing overfitting (Correct answer)
- Feature selection is performed by setting some coefficients to exactly zero
Correct answer: Coefficients shrink toward zero, reducing overfitting
Higher alpha imposes stronger L2 penalty, shrinking all coefficients toward zero and reducing model variance.
Question 4: What is the purpose of cross-validation in supervised learning?
- To speed up model training on large datasets
- To get a more reliable estimate of model generalization performance (Correct answer)
- To automatically tune hyperparameters to optimal values
- To reduce the dimensionality of the feature space
Correct answer: To get a more reliable estimate of model generalization performance
Cross-validation repeatedly splits data into train/validation folds, providing a robust performance estimate that reduces variance compared to a single hold-out split.
Question 5: Which algorithm would be most suitable for a dataset with 1 million samples and 500 features where training speed is critical?
- K-Nearest Neighbors
- Support Vector Machine with RBF kernel
- Stochastic Gradient Descent Classifier (Correct answer)
- Naive Bayes with Gaussian kernel
Correct answer: Stochastic Gradient Descent Classifier
SGD Classifier scales efficiently to large datasets by updating weights using one sample (or a mini-batch) at a time, making it much faster than SVM or KNN.
Question 6: What does the confusion matrix entry at position [1][0] represent in binary classification?
- True Positives
- True Negatives
- False Positives
- False Negatives (Correct answer)
Correct answer: False Negatives
In a confusion matrix indexed as [actual][predicted], position [1][0] means actual positive (1) predicted as negative (0) — a false negative.
Question 7: Which scikit-learn class is used to systematically search over a hyperparameter grid using cross-validation?
- RandomizedSearchCV
- GridSearchCV (Correct answer)
- HalvingGridSearchCV
- BayesSearchCV
Correct answer: GridSearchCV
GridSearchCV exhaustively evaluates all combinations of specified hyperparameter values using cross-validation to find the best combination.
When using sklearn's train_test_split, what does the stratify parameter do?