Machine Learning
Master core ML concepts, algorithms, and model evaluation techniques for interviews.
Practice Questions
Artificial Intelligence (AI): Creating intelligent systems that mimic human intelligence.
Machine Learning (ML): A subset of AI that learns from data.
Deep Learning (DL): A subset of ML that uses neural networks with multiple layers.
Machine Learning (ML) is a branch of Artificial Intelligence (AI) that enables computers to learn from data and make decisions or predictions without being explicitly programmed for every task.
Simple Definition
Machine Learning is the process of teaching computers to learn patterns from data and improve their performance over time.
Supervised learning uses labeled data to train models where the algorithm learns from input-output pairs. Examples include regression and classification. Unsupervised learning works with unlabeled data to find hidden patterns or groupings. Examples include clustering (K-Means) and dimensionality reduction (PCA).
The bias-variance tradeoff is a fundamental concept in ML. Bias is the error from overly simplistic assumptions (underfitting). Variance is the error from sensitivity to small fluctuations in training data (overfitting). The goal is to find a model that balances both - low enough bias to capture patterns, and low enough variance to generalize to new data.
Overfitting happens when a model learns the noise and details of the training data so closely that it performs poorly on new, unseen data. Signs: very high training accuracy but much lower validation accuracy.
Ways to prevent it:
• Use more training data
• Simplify the model (fewer features or parameters)
• Regularization (L1/L2)
• Cross-validation
• Early stopping
• Dropout (for neural networks)
• Pruning (for decision trees)
Underfitting occurs when a model is too simple to capture the underlying patterns in the data, so it performs poorly on both training and test data. It is a sign of high bias. Fixes include using a more complex model, adding relevant features, reducing regularization, or training longer.
Both are supervised learning tasks.
• Classification predicts a discrete category or label (e.g., spam vs. not spam, disease yes/no).
• Regression predicts a continuous numeric value (e.g., house price, temperature).
Algorithms like Decision Trees and Random Forests can do both; the output type defines the task.
• Training set: used to fit the model's parameters.
• Validation set: used to tune hyperparameters and compare models.
• Test set: kept aside until the end to give an unbiased estimate of performance on unseen data.
A common split is 70/15/15 or 80/10/10. Using the test set during tuning leaks information and gives over-optimistic results.
Cross-validation is a technique to evaluate a model more reliably by training and testing it on different subsets of the data. In k-fold cross-validation, the data is split into k equal parts; the model is trained on k−1 folds and tested on the remaining fold, repeated k times. The average score is a more stable estimate of performance than a single train/test split and helps detect overfitting.
• Precision = TP / (TP + FP): of all predicted positives, how many were actually positive.
• Recall (Sensitivity) = TP / (TP + FN): of all actual positives, how many the model found.
• F1-score = 2 × (Precision × Recall) / (Precision + Recall): the harmonic mean, useful when you need a balance between precision and recall, especially on imbalanced data.
A confusion matrix is a table that summarizes a classification model's predictions against actual labels. For binary classification it has four cells: True Positives (TP), True Negatives (TN), False Positives (FP) and False Negatives (FN). Metrics like accuracy, precision, recall and F1-score are calculated from it.
If 95% of transactions are genuine and 5% are fraud, a model that always predicts 'genuine' gets 95% accuracy while catching zero fraud. On imbalanced data, accuracy hides poor performance on the minority class. Better metrics include precision, recall, F1-score, ROC-AUC and PR-AUC.
• Resampling: oversample the minority class (e.g., SMOTE) or undersample the majority class
• Use class weights so errors on the minority class cost more
• Choose suitable metrics (recall, F1, PR-AUC) instead of accuracy
• Adjust the classification threshold
• Use algorithms that handle imbalance well, such as tree-based ensembles
• Collect more minority-class data if possible
The ROC (Receiver Operating Characteristic) curve plots the True Positive Rate (recall) against the False Positive Rate at different classification thresholds. AUC (Area Under the Curve) summarizes it into one number between 0 and 1: 0.5 means random guessing and 1.0 means perfect separation. AUC measures how well the model ranks positives above negatives, independent of a specific threshold.
Regularization adds a penalty on model complexity to the loss function to reduce overfitting.
• L1 (Lasso) adds the sum of absolute values of the weights. It can shrink some weights to exactly zero, so it also performs feature selection.
• L2 (Ridge) adds the sum of squared weights. It shrinks weights smoothly but rarely to zero, and works well when many features have small effects.
• Elastic Net combines both.
Linear Regression models the relationship between input features and a continuous target as a straight line (or hyperplane): y = b0 + b1x1 + b2x2 + ... It finds the coefficients that minimize the sum of squared errors between predicted and actual values (Ordinary Least Squares). Key assumptions include linearity, independence of errors, constant variance (homoscedasticity) and normally distributed residuals.
Logistic Regression uses a linear combination of features, like linear regression, but passes it through the sigmoid (logistic) function to produce a probability between 0 and 1. A threshold (commonly 0.5) converts that probability into a class. It is called 'regression' because it estimates the probability (a continuous value) of the class.
A Decision Tree splits the data into branches based on feature values, choosing at each node the split that best separates the target (using Gini impurity or entropy/information gain for classification, or variance reduction for regression). A new sample follows the splits from the root to a leaf, and the leaf's majority class or average value is the prediction. Trees are easy to interpret but can overfit if grown too deep.
• Bagging (Bootstrap Aggregating) trains many models independently on random bootstrap samples of the data and averages or votes their predictions. It mainly reduces variance. Example: Random Forest.
• Boosting trains models sequentially, where each new model focuses on the errors of the previous ones. It mainly reduces bias. Examples: AdaBoost, Gradient Boosting, XGBoost, LightGBM.
Random Forest builds many decision trees, each on a bootstrap sample of the data and using a random subset of features at each split. Predictions are combined by majority vote (classification) or averaging (regression). The randomness makes trees less correlated, so the ensemble overfits less and generalizes better than a single deep tree. It also provides feature importance scores.
Gradient Boosting builds an ensemble of weak learners (usually shallow trees) sequentially. It starts with a simple prediction, then each new tree is trained to predict the residual errors (the negative gradient of the loss function) of the current ensemble. The new tree's predictions are added with a learning rate that controls step size. Repeating this gradually minimizes the loss. Popular implementations are XGBoost, LightGBM and CatBoost, which add regularization and speed optimizations.
KNN is a simple, instance-based algorithm. To predict a new point, it finds the k closest training points using a distance metric (often Euclidean) and returns the majority class (classification) or average value (regression). It has no real training phase, but prediction can be slow on large datasets. Feature scaling is essential because distances are affected by feature magnitudes. Choosing k is a bias-variance trade-off.
SVM is a supervised algorithm that finds the hyperplane that separates classes with the maximum margin, i.e. the largest distance to the nearest points of each class (the support vectors). For data that is not linearly separable, the kernel trick (e.g., RBF, polynomial kernels) maps data into a higher-dimensional space where a linear separator can be found. The C parameter controls the trade-off between a wide margin and misclassification.
K-Means is an unsupervised algorithm that groups data into k clusters:
1. Choose k and randomly initialize k centroids.
2. Assign each point to its nearest centroid.
3. Recompute each centroid as the mean of its assigned points.
4. Repeat steps 2–3 until assignments stop changing.
It is sensitive to the initial centroids (K-Means++ helps) and to feature scaling, and works best with roughly spherical clusters.
• Elbow method: plot within-cluster sum of squares (inertia) against k and pick the point where the decrease slows sharply.
• Silhouette score: measures how similar points are to their own cluster versus others; higher is better.
• Domain knowledge and business needs (e.g., number of customer segments that can be acted on).
• Gap statistic for a more statistical approach.
PCA is an unsupervised dimensionality-reduction technique. It transforms correlated features into a smaller set of uncorrelated variables called principal components, ordered by how much variance they explain. It is used to speed up training, reduce noise, deal with multicollinearity and visualize high-dimensional data in 2D/3D. Data should be standardized before applying PCA.
Many algorithms (KNN, SVM, K-Means, gradient-descent based models, PCA) are sensitive to feature magnitudes, so a feature with large values can dominate. Scaling puts features on a comparable range.
• Normalization (Min-Max) rescales values to [0, 1].
• Standardization (Z-score) transforms values to mean 0 and standard deviation 1.
Tree-based models generally do not need scaling.
Feature engineering is creating, transforming or selecting input variables to help a model learn better. Examples: extracting day/month from dates, combining features (price per square foot), encoding categories, binning continuous values, log-transforming skewed data, and creating aggregates (customer's average spend). Good features often improve performance more than switching algorithms.
• Label/Ordinal Encoding: map categories to integers; suitable when there is a natural order (low/medium/high).
• One-Hot Encoding: create a binary column per category; suitable for nominal data with few categories.
• Target/Mean Encoding: replace categories with the average target value; useful for high-cardinality features but must be done carefully to avoid leakage.
• Frequency encoding or embeddings for very many categories.
Parameters are learned from data during training (e.g., regression coefficients, neural network weights). Hyperparameters are set before training and control the learning process (e.g., learning rate, tree depth, k in KNN, regularization strength).
Tuning methods: Grid Search, Random Search, Bayesian Optimization (e.g., Optuna), always evaluated with cross-validation on the training/validation data.
Data leakage happens when information that would not be available at prediction time is used during training, leading to unrealistically good validation results that fail in production. Examples: scaling using the full dataset before splitting, including a feature derived from the target, or using future data in time-series.
Prevention: split data first, fit preprocessing only on training data (use pipelines), respect time order, and carefully review features.
• MAE (Mean Absolute Error): average absolute difference; easy to interpret.
• MSE (Mean Squared Error): penalizes large errors more.
• RMSE: square root of MSE, in the same units as the target.
• R² (coefficient of determination): proportion of variance explained by the model.
• MAPE: average percentage error, useful for business reporting.
Gradient descent is an optimization algorithm that minimizes a loss function by repeatedly moving model parameters in the direction of the negative gradient. The learning rate controls the step size: too high can overshoot, too low is slow.
Variants: Batch (full dataset per step), Stochastic (one sample per step) and Mini-batch (small batches, most common). Optimizers like Adam and RMSProp adapt the learning rate automatically.
1. Package the trained model and preprocessing pipeline (e.g., pickle/joblib or ONNX).
2. Serve it via an API (FastAPI/Flask), a batch job, or a managed platform (SageMaker, Vertex AI).
3. Containerize with Docker and automate with CI/CD (MLOps).
4. Monitor latency, errors, input data drift and prediction drift.
5. Track model performance as true labels arrive and retrain when performance degrades.
Tools like MLflow help with versioning and experiment tracking.
