Machine Learning with Python
Machine Learning with Python
This domain tests the scikit-learn workflow: splitting data, building pipelines, training supervised and unsupervised models, and evaluating them with metrics that fit the problem. Expect questions on leakage, imbalanced classes, and overfitting.
Core rules
- Estimators implement
fit,predict, and oftentransformorfit_transform. - Split into train and test before preprocessing; put imputation and scaling inside a pipeline to prevent leakage.
- Classification metrics include accuracy, precision, recall, F1, ROC-AUC, and the confusion matrix. Regression metrics include MAE, RMSE, and R-squared.
- Use cross-validation for stable estimates and stratified folds for class imbalance.
- Regularization and simpler models reduce overfitting; tree ensembles often improve tabular performance.
- k-means uses distance; evaluate with inertia and silhouette score.
Worked example
A churn model has 5% positives. A constant "no churn" prediction scores 95% accuracy, which is useless. Reasoning: use a stratified train/test split, set class_weight='balanced', and evaluate recall, precision, F1, and the confusion matrix on the holdout set. If missing a churner is expensive, favor recall and tune the decision threshold. Build a pipeline: imputer, scaler, logistic regression, so preprocessing is learned only from training folds.
Traps
- Scaling or imputing before the train/test split.
- Tuning hyperparameters on the test set.
- Reporting accuracy alone with imbalanced classes.
- Calling
fit_transformon test data. - Forgetting
random_stateand getting non-reproducible splits. - Leaking target-derived features into training.