Brush up your Machine learning Skill

Machine leaning
EDA
Analysis
Preprocessing
Prediction
Pipeline
Author

Mukesh

Published

June 18, 2025

Machine learning

In a simple terms computer is able to learn from the data without being explicitly programmed.

Tip

Any sort of data science work Look at your data directly even though you have an description.

Types of Machine Learning

Type Description Examples
Supervised Learning Uses labeled data (input + output) Classification, Regression
Unsupervised Learning Uses unlabeled data (only input) Clustering, Dimensionality Reduction
Semi-supervised Learning Mix of labeled and unlabeled data Image classification with limited labels
Reinforcement Learning Agent learns by trial and error with rewards/penalties Game AI, Robotics

Key Concepts in Machine Learning

Concept Explanation
Features (X) or Independent variable Input variables used to make predictions
Target (y) or Dependent variable Output variable we want to predict
Training Data Data used to train the model
Test Data Data used to evaluate the model
Overfitting Model performs well on training data but poorly on new data
Underfitting Model doesn’t perform well even on training data
Bias-Variance Tradeoff Balancing simplicity vs complexity of a model
Generalization How well the model performs on unseen data

Data Understanding & Preprocessing

When I first receive the dataset, I begin by performing an initial assessment to understand its structure and quality. This involves the following key steps:

1. Data Inspection

  • Check for Missing Values: Identify any missing or null entries across all features. Depending on the volume and pattern of missingness, decide whether to drop, impute (replace), or model around them.
  • Data Types Check: Review the data types of each feature (e.g., integer, float, object) to ensure they align with the expected format. For example, date columns should be in datetime format, and categorical variables should be strings or category types.
  • Correlation Analysis: Examine the correlation between independent variables and the target variable. This helps identify potentially predictive features and detect multicollinearity issues.

2. Feature Exploration & Engineering

  • Date Handling: If the dataset includes date-time fields, I extract meaningful features such as year, month, day, week of year, day of week, etc. These engineered features can significantly enhance model performance by capturing temporal patterns.
  • Missing Value Treatment:
    • Numerical Features (Continuous): Impute missing values using mean, median, or advanced methods like KNN or model-based imputation.
    • Categorical Features: Replace missing values with “Unknown” or use frequency-based approaches.
  • Normalization / Scaling: Apply normalization or standardization techniques to numerical features when working with models sensitive to feature scales (e.g., SVMs, neural networks, gradient descent-based algorithms).
  • Categorification (Encoding): Convert categorical variables into numerical formats suitable for machine learning models:
    • One-Hot Encoding: Preferred for nominal categorical variables with no inherent order (especially when cardinality is low).
    • Label Encoding: Used for binary or ordinal variables where natural ordering exists. e.g.: ["small", "medium", "hard"]
    • Target Encoding / Leave-One-Out Encoding: Useful for high-cardinality categorical features (like zip codes), where one-hot encoding would lead to dimensionality explosion. (Leave-One-Out Encoding replaces each category with the average target value of all other rows in the same category, excluding the current row. This helps prevent overfitting by avoiding data leakage during encoding.)

3. Categorical vs Continuous Variables

  • Categorical Variables: These typically have object or string data types and represent discrete categories. They may require encoding before modeling.
  • Continuous Variables: These are numeric features that can take any value within a range. These usually undergo scaling or binning (discrete intervals or bins) depending on the model requirements.

4. Cardinality Consideration

  • High Cardinality: Features like zip_code, user_id, or product_id that have thousands of unique values. These need special handling—such as grouping rare categories, hashing, or using embedding layers in deep learning.
  • Low Cardinality: Features with a small number of distinct values. These are generally easier to encode using one-hot or label encoding methods.

5. Ordinal Variables

Some categorical variables have a natural order, such as "easy", "medium", "hard" or "low", "medium", "high". These are called ordinal variables, and their order must be preserved during encoding (e.g., via custom mapping or ordinal encoding).

🌳 Decision Trees

What is a Decision Tree?

A decision tree is a machine learning model that makes predictions by learning simple decision rules inferred from the data features. It splits the data into subsets based on feature values, forming a tree-like structure where:

  • Each internal node represents a test on a feature.
  • Each branch represents the outcome of that test.
  • Each leaf node represents a final prediction or class label (in classification) or value (in regression).

How Does a Decision Tree Work?

  1. Recursive Partitioning: The algorithm starts at the root of the tree and tries to split the data into two groups such that the resulting groups are as “pure” as possible with respect to the target variable.
  2. Splitting Criteria:
    • For regression trees, it uses metrics like Mean Squared Error (MSE) or Mean Absolute Error (MAE).
    • For classification trees, it uses measures like Gini impurity or Information Gain (based on entropy).
    • (Gini impurity is a measure of how often a randomly chosen element from a set would be incorrectly labeled if it was randomly labeled according to the distribution of labels in that subset).
    • (Information Gain (IG) measures how much “information” a feature provides about the class labels. In other words, it tells us how well a feature separates the data into classes.)
  3. This process continues recursively until a stopping criterion is met (e.g., maximum depth, minimum number of samples per leaf).

Pros & Cons of Decision Trees

Pros Cons
Easy to interpret and visualize Prone to overfitting
Handles both numerical and categorical data well Unstable: small changes in data can lead to very different trees
No need for feature scaling May not generalize well

Ensemble Learning: Bagging & Random Forests

What is an Ensemble Method?

An ensemble method combines multiple models to improve performance and reduce variance and bias.

🧺 Bagging (Bootstrap Aggregating)

Bagging works by:

  1. Randomly sampling subsets of the training data with replacement (called bootstrap samples).
  2. Training a base model (usually a decision tree) on each subset.
  3. Averaging predictions (for regression) or using majority voting (for classification) across all models.

This reduces variance and helps prevent overfitting.

🌲 Random Forests

Random Forests are an extension of bagging with one key difference: - At each split in the tree, only a random subset of features is considered for splitting.

This further decorrelates the trees, improving performance.

Why Are Random Forests Effective?

  • Each tree sees slightly different data and features.
  • Errors from individual trees tend to cancel out when averaged.
  • They’re robust to noise, outliers, and overfitting.

Out-of-Bag (OOB) Error

What is OOB Error?

In random forests, since each tree is trained on a bootstrapped sample (~63% of the data), the remaining ~37% of rows not used in training a particular tree are called Out-of-Bag (OOB) samples.

Why Is OOB Error Useful?

  • You can use these OOB samples to estimate how well the model performs without needing a separate validation set.
  • For each row, you average predictions only from trees that did not include that row during training.

🧠 Intuition: OOB error is like having a built-in cross-validation mechanism for random forests.

Model Interpretation

Even though ensemble models like random forests are more complex than single decision trees, they still allow us to understand how and why they make their predictions.

1. Feature Importance

  • Measures how much each feature contributes to reducing uncertainty (or error) in predictions.
  • Computed by averaging the reduction in impurity (like Gini or MSE) brought by each feature across all trees.

🎯 Use Case: Identify which features are most useful—drop irrelevant ones or focus on them for domain insights.

2. Finding Out-of-Domain Data

  • Train a classifier to distinguish between training and test sets.
  • If the classifier performs well, it means the test set differs significantly from the training set.
  • Helps identify distribution shifts that may affect generalization.

3. Partial Dependence Plots (PDP)

  • Shows how a feature affects predictions on average, holding other features constant.
  • Reveals non-linear relationships and interactions.

4. Individual Conditional Expectation (ICE) Plots

  • Like PDP but shows the effect for individual data points rather than averages.
  • Helps detect heterogeneity in effects.

5. Tree Interpreter / SHAP Values

  • Explains individual predictions by decomposing the contribution of each feature.
  • Uses techniques like SHapley Additive exPlanations (SHAP) to fairly attribute prediction changes to input features.

🎯 Use Case: Explain why a specific prediction was made—for transparency, fairness, or debugging.

Ensembling Techniques

1. Bagging (as discussed above)

  • Combines many models trained on different subsets of data.
  • Reduces variance → improves generalization.

2. Boosting

  • Sequentially trains weak learners, each correcting the errors of its predecessor.
  • Final prediction is a weighted sum of all models’ predictions.

Key Idea Behind Boosting:

  1. Train a simple model (often shallow trees).
  2. Compute residuals (errors).
  3. Train a new model to predict those residuals.
  4. Repeat and add corrections iteratively.

🧠 Intuition: Boosting learns slowly and focuses on hard-to-predict cases.

Bagging vs. Boosting

Feature Bagging (e.g., Random Forest) Boosting (e.g., XGBoost)
Training Style Parallel Sequential
Focus Reduce variance Reduce bias
Overfitting Less prone More prone if not tuned
Speed Faster to train Slower due to sequential steps
Performance Strong baseline Often higher accuracy with tuning

Summary: When to Use What?

  • Use Decision Trees for interpretable, fast baselines.
  • Use Random Forests (Bagging) when you want good performance with less tuning.
  • Use Boosting when you want high performance and can afford more tuning and time.
  • Use OOB error to validate without a separate validation set.
  • Use Feature Importance / SHAP / PDP to understand and explain your model.

Model Evaluation Metrics

Model evaluation is the process of assessing how well a machine learning model performs on unseen data. There are different metrics for classification and regression tasks.

✅ Classification Metrics

Metric Description Formula / Use Case Function
Accuracy % of total correct predictions (both true positives and true negatives) TP + TN / TP + FP + TN + FN accuracy_score()
Precision How many selected items are relevant? TP / (TP + FP) precision_score()
Recall (Sensitivity) How many relevant items were selected? TP / (TP + FN) recall_score()
F1 Score Harmonic mean of precision and recall 2 * (precision * recall)/(precision + recall) f1_score()
Confusion Matrix Table showing counts of true positives, false positives, true negatives, false negatives Visual summary of performance confusion_matrix()
ROC-AUC Area under the ROC curve; measures model’s ability to distinguish between classes Higher AUC = better performance roc_auc_score()
Note

📌 Use ROC-AUC when dealing with imbalanced datasets.
For class imbalance, prefer F1 score over accuracy.

📈 Regression Metrics

Metric Description Formula / Use Case Function
Mean Absolute Error (MAE) Average of absolute errors Mean of actual - predicted
Mean Squared Error (MSE) Average of squared errors Mean of (actual - predicted)² mean_squared_error()
R² Score (R-squared) Proportion of variance explained by the model Best possible score is 1.0 r2_score()

📌 MAE is more interpretable; MSE penalizes large errors more heavily.
R² tells how well your model fits the data compared to a baseline.

🔧 Hyperparameter Tuning

Hyperparameters are settings that control how models learn. Unlike model parameters (like weights), hyperparameters are set before training.

Techniques:

  1. Grid Search
    • Tries every combination of given hyperparameters.
    • Exhaustive but slow if too many parameters.
    • Use: When you have few parameters or want exhaustive search.
  2. Random Search
    • Randomly samples combinations from specified ranges.
    • Often faster than Grid Search and sometimes finds better results.
  3. Bayesian Optimization
    • Uses probabilistic models to choose next parameter set.
    • Efficient for high-dimensional spaces.
    • Requires external libraries like scikit-optimize.

🔁 Cross-validation

Cross-validation helps estimate how well your model will perform on unseen data by evaluating it on multiple random splits of the dataset.

K-Fold Cross-Validation:

  • Splits data into K parts (folds).
  • Trains on K-1 folds, tests on 1 fold — repeats K times.

🧠 Ensemble Methods

Ensemble methods combine predictions from multiple models to improve performance.

Types of Ensemble Learning:

1. Bagging (Bootstrap Aggregating)

  • Builds multiple models on bootstrapped samples of the data.
  • Final prediction is average (regression) or majority vote (classification).
  • Reduces variance.
  • Example: BaggingClassifier(), RandomForestClassifier()

2. Boosting

  • Sequentially trains models to correct errors of previous models.
  • Focuses on hard-to-predict instances.
  • Reduces bias.
  • Examples:
    • AdaBoostClassifier()
    • GradientBoostingClassifier()
    • Popular: XGBoost, LightGBM, CatBoost

3. Voting

  • Combines predictions from multiple base classifiers.
  • Hard Voting: Majority class.
  • Soft Voting: Weighted probabilities.

4. Stacking

  • Train a “meta-model” to combine outputs of base models.
  • Base models’ predictions become new features for the meta-model.

Summary Table

Topic Key Functions/Classes Purpose
Model Evaluation accuracy_score, precision_score, etc. Assess model performance
Hyperparameter Tuning GridSearchCV, RandomizedSearchCV Find best model settings
Feature Engineering StandardScaler, OneHotEncoder, SimpleImputer Improve input quality
Pipelines Pipeline Automate preprocessing + modeling
Cross-validation cross_val_score Estimate generalization performance
Ensemble Methods BaggingClassifier, GradientBoostingClassifier, VotingClassifier, StackingClassifier Boost performance via combining models

What is Clustering?

Clustering is an unsupervised machine learning technique used to group similar data points together into clusters. It helps find patterns or structures in data without prior knowledge of the groups — unlike classification, where we already know the categories.

Why Do We Use Clustering?

Clustering helps answer questions like:

  • How many distinct groups exist in my data?
  • Are there any unusual or outlier observations?
  • Can I simplify or summarize the data by grouping similar items?

Common use cases include:

  • Customer segmentation
  • Image compression
  • Document grouping
  • Anomaly detection
  • Social network analysis

Types of Clustering Algorithms

Here are some popular clustering algorithms:

1. K-Means Clustering

  • Groups data into k number of clusters.
  • Starts with random centers and iteratively assigns points to the nearest cluster center.
  • Goal: Minimize the sum of squared distances between points and their cluster centers.

Pros: Simple, fast
Cons: Needs k specified, assumes spherical clusters

2. Hierarchical Clustering

  • Builds a tree-like structure (dendrogram) showing how clusters merge or split.
  • Two types:
    • Agglomerative (bottom-up): starts with individual points, merges them.
    • Divisive (top-down): starts with all points in one cluster, splits recursively.

Pros: No need to specify number of clusters
Cons: Computationally expensive for large datasets

3. DBSCAN (Density-Based Spatial Clustering of Applications with Noise)

  • Groups points that are close together and marks outliers as noise.
  • Doesn’t require specifying number of clusters.

Pros: Handles noise well, finds arbitrarily shaped clusters
Cons: Sensitive to parameter settings

4. Gaussian Mixture Models (GMMs)

  • Assumes data comes from a mixture of Gaussian distributions.
  • Uses probabilities to assign points to clusters.

Pros: Soft clustering (gives probability of belonging to each cluster)
Cons: Slower than K-Means

How Does Clustering Work? (Simplified)

Let’s take K-Means as an example:

  1. Choose the number of clusters k.
  2. Randomly place k centroids (center points).
  3. Assign each data point to the nearest centroid.
  4. Recalculate centroids based on the mean of assigned points.
  5. Repeat steps 3–4 until centroids stabilize.

Evaluating Clusters

There’s no “right” answer in unsupervised learning, but you can still evaluate quality using metrics like:

  • Silhouette Score: Measures how similar a point is to its own cluster vs others (ranges from -1 to +1)
  • Elbow Method: Helps choose optimal k by plotting sum of squared distances vs. number of clusters
  • Davies-Bouldin Index, Calinski-Harabasz Index, etc.

Real-Life Example

Imagine you’re a marketing analyst and want to segment customers based on spending habits:

Customer Annual Income Spending Score
A 50 40
B 60 60
C 10 90
D 80 20

Using clustering, you might discover:

  • Group 1: High income, high spenders
  • Group 2: Low income, high spenders
  • Group 3: High income, low spenders

Each group can be targeted with different marketing strategies.

Summary

Feature Clustering
Type Unsupervised
Input Data without labels
Output Groups/clusters of similar points
Popular Algorithms K-Means, DBSCAN, Hierarchical
Evaluation Metrics Silhouette score, Elbow method