Intermediate

Traditional ML Models

Traditional machine learning models - the non-deep-learning statistical and algorithmic approaches - remain the workhorses of production ML. For tabular data, small datasets, and situations where interpretability matters, these models often outperform deep learning while being faster to train, easier to deploy, and simpler to debug.

What Are Traditional ML Models?

Traditional ML models are machine learning algorithms that don't use deep neural networks. They include linear models, tree-based methods, kernel methods, and instance-based learning. These algorithms were developed primarily between the 1950s and 2010s, before the deep learning revolution.

Despite the hype around deep learning, traditional ML dominates many real-world applications. Kaggle competition winners consistently use XGBoost and LightGBM for tabular data. Banks, insurance companies, and healthcare systems rely on interpretable models like logistic regression and decision trees for regulatory compliance. Traditional ML is far from obsolete.

Supervised Learning Models

Supervised learning uses labeled training data to learn a mapping from inputs to outputs. These are the most commonly deployed traditional ML models.

Linear and Logistic Regression

Linear regression predicts a continuous value as a weighted sum of input features. Logistic regression extends this to classification by passing the linear combination through a sigmoid function to produce probabilities.

  • Strengths: Fast training and inference, highly interpretable (each coefficient shows feature importance), solid theoretical foundation, works well with regularization (L1/Lasso, L2/Ridge)
  • Weaknesses: Assumes linear relationships, can't capture complex feature interactions without manual feature engineering
  • Best for: Baseline models, interpretable predictions, high-dimensional sparse data (text classification with TF-IDF), credit scoring

Decision Trees

Decision trees split the data into subsets based on feature values, creating a tree-like structure of if-then rules. Each leaf node contains a prediction. They are the foundation for the most powerful traditional ML models (random forests and gradient boosting).

  • Strengths: Extremely interpretable (you can visualize the decision path), handles both numerical and categorical features, no feature scaling needed, captures non-linear relationships
  • Weaknesses: Prone to overfitting (a deep tree memorizes training data), unstable (small data changes can produce very different trees), generally lower accuracy than ensemble methods
  • Best for: Explainable decisions, rule extraction, quick prototyping

Random Forests

An ensemble of many decision trees, each trained on a random subset of the data (bagging) and a random subset of features. The final prediction is the average (regression) or majority vote (classification) of all trees. This dramatically reduces overfitting compared to a single tree.

  • Strengths: Robust to overfitting, handles missing values, provides feature importance rankings, works well out-of-the-box with minimal tuning
  • Weaknesses: Slower inference than a single tree (must evaluate hundreds of trees), less interpretable than a single tree, memory-intensive
  • Best for: General-purpose classification/regression, when you want good performance with minimal tuning

Gradient Boosting (XGBoost, LightGBM, CatBoost)

Gradient boosting builds trees sequentially, where each new tree corrects the errors of the previous ensemble. This is fundamentally different from random forests which build trees independently. The three dominant implementations are:

  • XGBoost: The original powerhouse. Regularized gradient boosting with efficient tree construction. Dominated Kaggle competitions from 2015-2020.
  • LightGBM: Microsoft's implementation using histogram-based splitting and leaf-wise tree growth. Faster training than XGBoost, especially on large datasets. Often the best choice for production.
  • CatBoost: Yandex's implementation with native categorical feature handling. No need to one-hot encode categories. Produces excellent results with minimal preprocessing.
💡
Industry reality: For structured/tabular data (spreadsheets, databases, feature tables), gradient boosting models like XGBoost and LightGBM consistently match or outperform deep learning. A 2022 study by Grinsztajn et al. confirmed that tree-based models are "still the best" for tabular data in most scenarios.

Support Vector Machines (SVM)

SVMs find the hyperplane that maximally separates classes in feature space. The "kernel trick" allows SVMs to handle non-linearly separable data by mapping it to higher-dimensional spaces without explicitly computing the transformation.

  • Strengths: Effective in high-dimensional spaces, works well with small datasets, robust to overfitting with proper regularization
  • Weaknesses: Slow on large datasets (O(n^2) to O(n^3) training), sensitive to feature scaling, difficult to tune kernel parameters
  • Best for: Text classification with TF-IDF features, small-to-medium datasets, high-dimensional data

k-Nearest Neighbors (kNN)

The simplest ML algorithm: classify a new point by looking at the K closest training examples and taking a majority vote. No training phase - all computation happens at prediction time (lazy learning).

  • Strengths: No training needed, intuitive, naturally handles multi-class problems, non-parametric (no assumptions about data distribution)
  • Weaknesses: Slow prediction on large datasets, sensitive to irrelevant features and feature scaling, memory-intensive (stores entire training set)
  • Best for: Small datasets, anomaly detection, recommendation systems, quick baselines

Naive Bayes

Applies Bayes' theorem with a "naive" assumption that features are conditionally independent. Despite this unrealistic assumption, Naive Bayes works surprisingly well in practice, especially for text classification.

  • Strengths: Extremely fast training and prediction, works well with high-dimensional sparse data, handles missing data naturally, good with small training sets
  • Weaknesses: The independence assumption limits accuracy on complex data, can't learn feature interactions
  • Best for: Spam filtering, text classification, real-time classification where speed is critical

Unsupervised Learning Models

Unsupervised learning finds patterns in unlabeled data. These models discover structure, group similar items, and reduce dimensionality without being told what to look for.

K-Means Clustering

Partitions data into K clusters by iteratively assigning points to the nearest centroid and updating centroids. Simple, fast, and widely used, but requires specifying K in advance and assumes roughly spherical clusters.

  • Use cases: Customer segmentation, image compression, document clustering, anomaly detection (points far from any centroid)
  • Tip: Use the elbow method or silhouette score to choose K. Consider K-Means++ initialization for better convergence.

DBSCAN

Density-Based Spatial Clustering of Applications with Noise. Groups together points that are closely packed and marks points in low-density regions as outliers. Unlike K-Means, DBSCAN doesn't require specifying the number of clusters and can find arbitrarily shaped clusters.

  • Use cases: Spatial data analysis, anomaly/outlier detection, discovering clusters of irregular shapes
  • Key parameters: epsilon (neighborhood radius) and min_samples (minimum points for a dense region)

Principal Component Analysis (PCA)

PCA reduces dimensionality by finding the directions (principal components) of maximum variance in the data. It projects high-dimensional data onto a lower-dimensional subspace while preserving as much information as possible.

  • Use cases: Dimensionality reduction before modeling, data visualization, noise reduction, feature extraction
  • Practical tip: Choose the number of components to retain 95% of variance. Always standardize features before applying PCA.

t-SNE and UMAP

t-SNE (t-distributed Stochastic Neighbor Embedding) and UMAP (Uniform Manifold Approximation and Projection) are non-linear dimensionality reduction techniques primarily used for visualization. They excel at revealing cluster structure in high-dimensional data by mapping it to 2D or 3D.

  • t-SNE: Great visualizations but slow on large datasets, non-deterministic, and distances between clusters are not meaningful
  • UMAP: Much faster than t-SNE, preserves more global structure, and can be used for general-purpose dimensionality reduction (not just visualization)

Comparison Table

A comprehensive comparison to help you choose the right model for your task:

ModelTypeStrengthsWeaknessesBest For
Linear/Logistic RegressionSupervisedFast, interpretableLinear onlyBaselines, credit scoring
Decision TreeSupervisedInterpretable, visualOverfits easilyRule extraction
Random ForestSupervisedRobust, minimal tuningSlow inferenceGeneral-purpose
XGBoost/LightGBMSupervisedTop accuracy on tabularMore tuning neededTabular data, competitions
SVMSupervisedHigh-dimensionalSlow on large dataText, small datasets
kNNSupervisedNo training, simpleSlow predictionSmall data, anomaly detection
Naive BayesSupervisedVery fast, sparse dataIndependence assumptionSpam filtering, text
K-MeansUnsupervisedFast, scalableMust choose KCustomer segmentation
DBSCANUnsupervisedFinds outliers, any shapeSensitive to parametersSpatial data, anomalies
PCAUnsupervisedReduces dimensionsLinear onlyPreprocessing, visualization

When Traditional ML Beats Deep Learning

Despite deep learning's dominance in headlines, traditional ML wins in many practical scenarios:

  • Tabular/structured data: For data that lives in spreadsheets or databases (customer records, financial data, sensor readings), XGBoost and LightGBM consistently match or beat deep learning models.
  • Small datasets: With fewer than 10,000 training examples, deep learning models overfit. Traditional models with proper regularization generalize better from limited data.
  • Interpretability required: In healthcare, finance, and legal domains, you need to explain why a model made a decision. A logistic regression coefficient or decision tree path is inherently interpretable.
  • Low latency requirements: A single decision tree or linear model can make predictions in microseconds. Deep learning models typically need milliseconds, which matters at scale.
  • Limited compute: Traditional models train on a CPU in seconds to minutes. No GPU required, making them accessible to any team.
  • Rapid iteration: You can train, evaluate, and tune a traditional model in minutes, enabling much faster experimentation cycles.

The scikit-learn Ecosystem

scikit-learn is the most important library in traditional ML. It provides a unified API for virtually every traditional ML algorithm, along with tools for preprocessing, model selection, and evaluation. Every ML practitioner should be fluent in scikit-learn.

Key components of the ecosystem:

  • scikit-learn: Core library with all standard algorithms, preprocessing, and model selection tools
  • XGBoost / LightGBM / CatBoost: Gradient boosting libraries that follow the scikit-learn API
  • pandas: Data manipulation and analysis (DataFrames)
  • NumPy: Numerical computing foundation
  • Optuna / Hyperopt: Advanced hyperparameter optimization
  • SHAP / LIME: Model interpretability and explainability

Feature Engineering Importance

With traditional ML, feature engineering is everything. The quality of your features determines the ceiling of your model's performance. Unlike deep learning which learns features from raw data, traditional models need you to create meaningful representations.

Key feature engineering techniques:

  • Numerical transformations: Log transforms for skewed distributions, polynomial features for non-linear relationships, binning for creating categorical features from continuous ones
  • Categorical encoding: One-hot encoding, target encoding, frequency encoding, ordinal encoding for ordered categories
  • Temporal features: Day of week, hour of day, is_weekend, days_since_last_event, rolling averages
  • Interaction features: Products or ratios of features that capture relationships (price_per_square_foot = price / area)
  • Aggregation features: Group-level statistics (user's average spend, product's mean rating, category count)

Code Example: XGBoost Classification

A complete example showing data preprocessing, model training, evaluation, and feature importance with XGBoost and scikit-learn:

Python - XGBoost Classification with scikit-learn
import xgboost as xgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import (classification_report, confusion_matrix,
                               roc_auc_score)
from sklearn.preprocessing import StandardScaler
import numpy as np

# Load dataset
data = load_breast_cancer()
X, y = data.data, data.target
feature_names = data.feature_names

# Split data
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# Train XGBoost classifier
model = xgb.XGBClassifier(
    n_estimators=200,
    max_depth=6,
    learning_rate=0.1,
    subsample=0.8,
    colsample_bytree=0.8,
    reg_alpha=0.1,        # L1 regularization
    reg_lambda=1.0,       # L2 regularization
    eval_metric="logloss",
    random_state=42,
)
model.fit(
    X_train, y_train,
    eval_set=[(X_test, y_test)],
    verbose=False,
)

# Evaluate
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1]

print("Classification Report:")
print(classification_report(y_test, y_pred,
      target_names=["malignant", "benign"]))
print(f"AUC-ROC: {roc_auc_score(y_test, y_prob):.4f}")

# Cross-validation score
cv_scores = cross_val_score(model, X, y, cv=5, scoring="accuracy")
print(f"CV Accuracy: {cv_scores.mean():.4f} (+/- {cv_scores.std():.4f})")

# Feature importance (top 10)
importances = model.feature_importances_
indices = np.argsort(importances)[::-1][:10]
print("\nTop 10 Most Important Features:")
for i in indices:
    print(f"  {feature_names[i]}: {importances[i]:.4f}")

Hyperparameter Tuning

Traditional ML models have hyperparameters that dramatically affect performance. Tuning them properly is essential for getting the best results.

Grid Search

Exhaustively tries every combination of specified parameter values. Reliable but slow. Best for small parameter spaces. Use GridSearchCV in scikit-learn for automatic cross-validation during search.

Random Search

Samples random combinations from parameter distributions. Often finds good results faster than grid search because it explores more of the parameter space. Use RandomizedSearchCV with a specified number of iterations.

Optuna

A modern Bayesian optimization framework that intelligently explores the parameter space, learning from previous trials to focus on promising regions. Supports pruning (early stopping of bad trials), distributed optimization, and visualization of results. This is the recommended approach for production models.

Practical tip: Start with random search to identify promising regions of the parameter space. Then use Optuna to fine-tune within those regions. For XGBoost, the most impactful parameters to tune are: max_depth, learning_rate, n_estimators, subsample, and colsample_bytree.

Model Interpretability

Understanding why a model makes a prediction is often as important as the prediction itself. Two frameworks have become standard for model interpretation:

SHAP (SHapley Additive exPlanations)

Based on game theory, SHAP assigns each feature a contribution value for every individual prediction. It shows exactly how much each feature pushed the prediction higher or lower compared to the average. SHAP values are additive (they sum to the difference between the prediction and the average) and consistent (a feature that contributes more always gets a higher SHAP value).

  • Global explanations: Summarize feature importance across all predictions
  • Local explanations: Explain individual predictions ("this loan was rejected because income was too low and debt-to-income ratio was too high")
  • Interaction effects: Reveal how features interact in making predictions

LIME (Local Interpretable Model-agnostic Explanations)

LIME explains individual predictions by fitting a simple, interpretable model (like linear regression) to the prediction's local neighborhood. It perturbs the input, observes how predictions change, and fits a linear approximation. Faster than SHAP but less theoretically grounded.

💡
Regulatory requirement: In the EU (GDPR) and many financial regulations, automated decisions that significantly affect individuals must be explainable. SHAP and LIME make any ML model - even black-box models like XGBoost - interpretable enough to meet these requirements.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.