Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 71

Choosing the Right ML Algorithm: A Framework

Recommended resources

Welcome to the second-to-last lesson in our course! In our previous session, we tackled the crucial skill of hyperparameter tuning, learning how to use Grid Search and Random Search to squeeze the best performance out of a given model. This raised a fundamental question: how do you decide which model to tune in the first place?

Throughout this course, you've built a formidable arsenal of algorithms, from Linear Regression to XGBoost and K-Means. Now, it's time to synthesize that knowledge. This lesson is designed to help you develop a decision-making framework to select an appropriate machine learning algorithm based on problem type and data characteristics. We won't be learning a new algorithm, but rather a meta-skill: the strategy of choosing the right tool for the job. This is the final conceptual piece you'll need before tackling the final project.

The First Question: What Kind of Problem Are You Solving?

Your journey in selecting an algorithm begins with a single, high-level question that should feel very familiar by now: Do you have a target variable? The answer immediately places you into one of two major machine learning paradigms.

  • Yes (Supervised Learning): Your data is labeled. You know the "answer" you're trying to predict. The task will either be regression (predicting a continuous numerical value) or classification (predicting a discrete category).
  • No (Unsupervised Learning): Your data is unlabeled. You are not trying to predict a specific outcome but rather to discover inherent structure or patterns within the data itself. The most common tasks here are clustering (grouping similar data points) and dimensionality reduction (simplifying the data by reducing the number of features).

This fundamental split is the starting point for nearly every algorithm selection guide. The following short video provides a clear, concise mental model for this initial decision process.

How to choose the Right Machine Learning Algorithm

This video from Professor Nick Stugard offers an intuitive walkthrough of the high-level decision tree for algorithm selection.

First, watch the introduction which frames the problem and establishes the core distinction between supervised and unsupervised learning based on the presence of a target variable. Then, watch the summary at the end which consolidates this entire decision process into a simple flowchart.


A Visual Guide for Your Journey

This decision-making process can be visualized as a flowchart. Think of it as a map that guides you from your problem statement to a set of appropriate algorithms. The cheat sheet below is a great example of such a map.

This flowchart, similar to the popular one from scikit-learn, guides you through a series of questions about your task and data to suggest suitable algorithms for clustering, dimension reduction, classification, and regression.

As you can see, after the "START" point, the path branches based on the answers to questions like "Have Responses?" (i.e., a target variable), "Predicting Numeric?", and so on. We will now formalize the thinking process that allows you to navigate this map effectively.


A Formal Framework for Algorithm Selection

While a flowchart provides a quick reference, a robust decision requires a more systematic approach. We can break down the selection process into a series of steps.

Machine Learning Algorithm: When to Use Which One

This article from labelyourdata.com provides an excellent five-step framework that we will use to structure our thinking.

Please read the section titled "How to Choose Machine Learning Algorithm". This section outlines five key steps: understanding the goal, analyzing the data, evaluating speed, assessing linearity, and considering the number of features.

Let's expand on these five steps, integrating the knowledge you've gained throughout this course.

Step 1: Understand Your Project Goal

This aligns with the first question in our decision tree. What is the business or research objective?

  • Predicting a quantity? (e.g., house prices, future sales) -> Regression. Your initial candidates are Linear Regression, Ridge, Lasso, and tree-based regressors like Random Forest or XGBoost.
  • Assigning a label? (e.g., spam/not spam, customer churn/no churn) -> Classification. Your candidates include Logistic Regression, KNN, SVM, Naive Bayes, and tree-based classifiers.
  • Finding hidden groups? (e.g., customer segmentation) -> Clustering. Look to K-Means, Hierarchical Clustering, or DBSCAN.
  • Simplifying data or visualization? -> Dimensionality Reduction. PCA and t-SNE are your primary tools.

Step 2-5: Analyze Data, Evaluate Constraints, and Consider Complexity

Steps 2 through 5 from the article are deeply intertwined. The "right" algorithm depends not just on the problem type, but on the specific characteristics of your data and the practical constraints of your project. Here is a checklist of questions you should ask:

  • Size of Dataset: How many samples and features do you have?
    • Linear models train very quickly, even on large datasets.
    • KNN becomes computationally expensive as the number of samples grows.
    • SVMs can be effective but may be slow on datasets with >100k samples.
    • Tree-based ensembles (Random Forest, GBMs) scale well.
  • Interpretability: Do you need to explain why the model made a certain prediction?
    • High Interpretability: Linear/Logistic Regression (coefficients are explainable), Decision Trees (can be visualized).
    • Low Interpretability ("Black Box"): SVMs with non-linear kernels, Neural Networks, and to a lesser extent, large ensemble models.
  • Feature Type: Are your features numerical, categorical, or text?
    • Most scikit-learn models require features to be numeric. This is why we spent time on encoding categorical variables.
    • Some modern libraries like CatBoost have native handling for categorical features, which can be a significant advantage.
    • For text classification, Naive Bayes is a classic and highly effective baseline.
  • Linearity: Is the relationship between features and the target variable linear?
    • If yes, Linear or Logistic Regression are excellent, simple baselines.
    • If no, you need more flexible models that can capture non-linear relationships, such as SVMs with the kernel trick, or tree-based models like Random Forest and Gradient Boosting.
  • Tolerance to Data Issues: How robust is the algorithm to common data problems?
    • This is a critical consideration that distinguishes a novice from an expert. A research paper comparing several classifiers provides a fantastic, detailed summary of these characteristics. For your MSc-level background, this table offers the precise, nuanced understanding you're looking for.

Accuracy Comparison between Five Machine Learning Algorithms ...

This research paper provides a deep dive into the characteristics of several common classifiers.

First, read Section 3.5, Model Construction and Evaluation, to get a description of the classifiers and their general properties. Then, focus your attention on Table 5 in Section 4, titled "High level characteristics of classifiers". This table is a powerful reference that summarizes how KNN, Naive Bayes, Decision Tree, Logistic Regression, and Random Forest depend on or tolerate issues like normality, outliers, multicollinearity, and imbalanced classes.

Analyzing Table 5 from the paper, you can build a sophisticated mental model. For example, you can see that tree-based models (dt, rf) have high tolerance for outliers and don't require feature scaling (they are not distance-based), whereas knn is sensitive to both. This is the kind of detailed knowledge that guides expert model selection.


The Iterative Process: A Practical Case Study

It's rare to pick the perfect algorithm on the first try. The best practice is to start with a simple, fast baseline, evaluate its performance, and then try more complex models to see if you can achieve a significant improvement. This iterative workflow is a core part of the machine learning process.

The following video demonstrates this perfectly. The presenter uses the scikit-learn map to tackle a classification problem, starts with a simple model, and then iterates to a more powerful one.

93 Choosing The Right Model For Your Data 3 Classification | Scikit-learn Machine Learning Models

This tutorial from the "Machine Learning" channel provides a fantastic, code-first demonstration of the model selection workflow.

Watch how the presenter follows the map to select and implement LinearSVC as a first attempt. Next, see how they evaluate the result and correctly identify that its accuracy (below 50% for a binary problem) is poor. Observe the key decision point where, because the first model is "not working," they pivot to a more powerful ensemble model, the Random Forest Classifier. Finally, notice the dramatic performance improvement with the new model. This is a very common outcome. Pay close attention to the "tidbit" shared at the end about structured vs. unstructured data. This is an extremely valuable industry heuristic.

The key takeaway from this video is two-fold. First, model selection is an empirical and iterative process. You try something, measure it, and improve. Second, there are powerful heuristics that can help you jump to the right class of models faster. The advice to use ensemble methods for structured data (like the tables we've been using) and deep learning for unstructured data (images, audio, complex text) is a cornerstone of modern applied ML. For the scope of this course, where we focus on structured data, this strongly suggests that models like Random Forest and Gradient Boosting will often be your most powerful tools.


Conclusion

You have now assembled the final piece of your machine learning strategy. You not only know how to implement, evaluate, and tune individual algorithms, but you also have a comprehensive framework for choosing which algorithm to apply in the first place.

Key Takeaways:

  • Start with the Problem Type: The first decision is always based on your goal: regression, classification, clustering, or dimensionality reduction.
  • Analyze Your Data and Constraints: The best algorithm is a function of your data's size, linearity, feature types, and quality, balanced against project needs like interpretability and speed.
  • Use Heuristics to Guide You: For the structured data problems we've been solving, ensemble methods like Random Forest and XGBoost are often the highest-performing choices.
  • Iterate and Compare: Model selection is not a one-shot decision. Start with a simple baseline, then try more complex models to justify their increased computational cost and complexity with a significant performance gain.

What's Next:

This lesson concludes the theoretical and algorithmic portion of our course. You are now fully equipped to tackle a machine learning problem from end to end. In our next and final lesson, you will do just that. We will introduce a new, raw dataset, and your task will be to execute a full preprocessing pipeline, train and compare multiple candidate models, and select a final, tuned model to solve the problem. It's time to put everything you've learned into practice.