Skip to main content

Welcome to a Grasp lesson

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

Lesson 78

Choosing the Right ML Model

Recommended resources

Hello! Welcome to your next lesson in machine learning.

In our last session, you learned how to build a complete modeling workflow using scikit-learn's Pipeline object. This is a crucial skill because it allows you to create clean, reproducible, and error-free code. You now know how to construct a workflow that combines data preprocessing and a model into a single unit.

Today, we'll focus on the big question: Which model should you put inside that pipeline? The learning outcome for this lesson is to implement a solution for a representative problem by selecting and justifying the choice of ML model.

This is where the "science" in data science truly comes into play. It's not about randomly picking algorithms but about making informed decisions based on the problem, the data, and the trade-offs involved.

By the end of this lesson, you will be able to:

  • Understand a systematic framework for choosing a machine learning model.
  • Use the scikit-learn algorithm map to guide your initial model selection.
  • Implement and compare different models for a given problem.
  • Justify your final model choice based on performance and well-established heuristics.
  • Use cross-validation to robustly compare model performance.

Let's begin this exciting step from practitioner to strategist.

1. A Framework for Choosing Your Model

Before we write any code, it's essential to have a mental framework for approaching model selection. Choosing a model isn't a single step but a process of answering a series of questions.

How to Choose the Right Machine Learning Model for Your Project

This article from Emma Yasenchak on Medium provides an excellent, beginner-friendly guide to the model selection process. It outlines the key steps and considerations that you should think through before settling on a model.

Read the sections titled Step 1: Define the Problem Clearly, Step 2: Understand Your Data, Step 3: Choose the Right Model Based on Problem Type, and Step 4: Balance Accuracy, Interpretability, and Complexity. As you read, focus on the key questions you need to ask at each stage.

Based on the article, here is a summary of the core decision-making framework:

  1. Define the Problem: What are you trying to achieve?

    • Classification: Predicting a category (e.g., 'spam' or 'not spam', 'has heart disease' or 'does not').
    • Regression: Predicting a continuous value (e.g., house price, temperature).
    • Clustering: Grouping similar data points without labels (e.g., customer segmentation).
    • Dimensionality Reduction: Reducing the number of features.
  2. Understand the Data: What are you working with?

    • Size: How many samples and features do you have? Some models need lots of data (like neural networks), while others work well on small datasets.
    • Type: Is your data structured (like a table in a CSV file) or unstructured (like images, text, or audio)? This is a major deciding factor.
    • Characteristics: Does it have missing values? Are the classes imbalanced?
  3. Balance the Trade-offs: What are your priorities?

    • Accuracy: Do you need the absolute best performance, even if the model is complex?
    • Interpretability: Do you need to explain why the model made a certain prediction? Simpler models like Linear Regression or Decision Trees are easier to interpret.
    • Complexity/Resources: How much time and computing power do you have? Simple models are fast to train; complex ones (like large neural networks) can take days.

Keep this framework in mind as we move to a more practical tool.


2. A Practical Guide: The Scikit-learn Algorithm Map

The scikit-learn development team created a brilliant flowchart to help practitioners navigate the library's algorithms. It operationalizes the framework we just discussed into a simple, visual decision tree.

Scikit-learn Algorithm Cheat Sheet Flowchart
This flowchart is your map for navigating the world of scikit-learn models. You start at the top and follow the paths by answering questions about your data and your problem.

Let's familiarize ourselves with it. You start at the "START" bubble.

  • The first question is about the number of samples.
  • Then, you branch based on your problem type (predicting a category, a quantity, etc.).
  • Following the arrows leads you to a suggested algorithm, which is a great starting point for your experiments.
Test your understanding!

Imagine you are given a new project. Your goal is to group a website's visitors into different types based on their browsing behavior (pages visited, time on site, etc.). You have data for 10,000 visitors, but you don't have any predefined labels for the visitor types.

Using the scikit-learn algorithm cheat sheet, which model might you try first? Trace your path and explain your reasoning.

Show answer
  1. Start: We begin at the top.
  2. >50 Samples? Yes, we have 10,000 visitors.
  3. Predicting a category? No. We are not predicting a predefined category; we are trying to discover groups.
  4. Predicting a quantity? No.
  5. Just looking for structure? Yes! This is exactly what "grouping visitors" means. This leads us to the Clustering branch.
  6. Know the number of categories? Let's assume we want to find a specific number of groups, say 3 or 5. So, yes.
  7. <10K samples? Yes, we have 10,000 samples, which fits the condition for this path.
  8. Suggestion: The map points to K-Means.

Therefore, a great first model to try would be K-Means. If the clusters are not spherical or if you have a lot of noise, the map suggests you could then explore other options like DBSCAN or GMM.


3. Practical Application: Choosing a Model for Heart Disease Prediction

Now, let's apply this process to a real-world problem. We will use the popular Heart Disease dataset. The goal is to predict whether a patient has heart disease based on various medical attributes.

This is a classic binary classification problem with structured (tabular) data.

The following video provides an excellent, hands-on demonstration of using the scikit-learn map to select, implement, and iterate on models for this exact problem.

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

Watch this video from the Machine Learning channel. It walks through the entire thought process, from looking at the map to implementing the models and interpreting the results. We will break it down step-by-step.

We will watch this video in parts. For now, watch from the beginning to 07:02. Pay close attention to: How the problem is set up (the data X and the target y). The path taken through the scikit-learn map to arrive at the first model choice: Linear SVC. The implementation of the LinearSVC model. The surprising result of the model's performance.

3.1. First Attempt: Linear SVC

As you saw in the video, following the map for a classification problem with labeled data and fewer than 100k samples leads us to LinearSVC (Support Vector Classification).

However, after training and evaluating the model, the accuracy was only about 47%. In a binary classification problem (two classes: 'has heart disease' or 'does not'), a score of 50% can be achieved by simply guessing. A score of 47% is actually worse than random guessing!

This is a critical lesson: Your first choice is a starting point, not necessarily the final answer. The map gives you a reasonable baseline, but you must evaluate its performance and be prepared to iterate.

3.2. Second Attempt: Iterating and Justifying a New Choice

The LinearSVC model didn't work well. What now? We go back to the map. The presenter makes a jump to "Ensemble Classifiers" and chooses RandomForestClassifier. Why?

This choice is based on a very powerful and widely used heuristic in machine learning:

For structured (tabular) data, ensemble methods like Random Forest and Gradient Boosting are often the best-performing models.

They are robust, handle complex relationships well, and usually provide high accuracy with minimal tuning. Because our Heart Disease dataset is structured data in a table, jumping to a Random Forest is an excellent strategic move.

Let's see the result of this decision.

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

Now, continue watching the same video.

Watch from 07:02 to 11:34. This section covers: The justification for moving to an ensemble classifier (RandomForestClassifier). The implementation of the new model. The dramatic improvement in the model's accuracy.

The result is a huge improvement! The RandomForestClassifier achieves around 85% accuracy on the same data.

This demonstrates the complete selection and justification process:

  1. Select a baseline: We used the map to select LinearSVC.
  2. Evaluate: The performance was poor.
  3. Iterate and Justify: We chose a new model, RandomForestClassifier, justifying the choice with a strong heuristic (ensemble models for structured data).
  4. Evaluate again: The new model performed significantly better.

Our justified choice for this problem is the RandomForestClassifier.


4. Robust Comparison with Cross-Validation

In the video, the models were compared based on a single train-test split. This is fine for a quick experiment, but the results can be misleading. The performance might change if you happen to get a "lucky" or "unlucky" split of the data.

To make a more robust and reliable comparison, we should use k-fold cross-validation. This technique involves splitting the data into 'k' folds (e.g., 5 or 10), then training and testing the model 'k' times, using a different fold for testing each time. The final score is the average of the scores from all 'k' folds.

Machine Learning Fundamentals: Cross Validation

This classic video from StatQuest with Josh Starmer provides the best intuitive explanation of why we need cross-validation and how it works.

Watch from 00:28 to 04:45. Focus on understanding: Why using the same data for training and testing is a bad idea. The limitation of a single train-test split. How k-fold cross-validation solves this by using every part of the data for both training and testing, just at different times.

In scikit-learn, we can easily perform cross-validation using the cross_val_score function. This is how you would use it to get a more reliable estimate of the performance of our two models:

from sklearn.model_selection import cross_val_score
from sklearn.svm import LinearSVC
from sklearn.ensemble import RandomForestClassifier
# Assume X and y are already loaded and preprocessed

# Create the models
model_svc = LinearSVC(max_iter=5000, dual="auto") # Increased max_iter to help convergence
model_rf = RandomForestClassifier(n_estimators=100)

# Perform 5-fold cross-validation for each model
svc_scores = cross_val_score(model_svc, X, y, cv=5)
rf_scores = cross_val_score(model_rf, X, y, cv=5)

# Print the mean accuracy and standard deviation
print(f"LinearSVC Accuracy: {svc_scores.mean():.4f} (+/- {svc_scores.std():.4f})")
print(f"RandomForestClassifier Accuracy: {rf_scores.mean():.4f} (+/- {rf_scores.std():.4f})")

# Expected Output (will vary slightly due to randomness):
# LinearSVC Accuracy: 0.7788 (+/- 0.0984)
# RandomForestClassifier Accuracy: 0.8250 (+/- 0.0343)

The cross-validation results give us a much more confident justification. We can now say that, on average, the Random Forest model is not only more accurate but also more stable (lower standard deviation) than the Linear SVC for this problem.


Conclusion

You've done an excellent job today, moving from simply building workflows to making strategic decisions about what goes in them. This is a massive step in your journey as a data scientist.

Key Takeaways:

  • Model selection is a systematic, iterative process, not a one-shot decision.
  • Start by defining your problem, understanding your data, and considering trade-offs like accuracy vs. interpretability.
  • The scikit-learn algorithm map is an invaluable tool for choosing a good baseline model.
  • A powerful heuristic is to use ensemble methods (like Random Forest) for structured data and deep learning for unstructured data (images, text).
  • Always justify your model choice with evidence. A common justification is: "Model A was tried as a baseline, but its performance was poor. Model B was chosen because it is known to work well for this type of data, and it achieved significantly higher accuracy, as confirmed by robust cross-validation."
  • Use k-fold cross-validation for reliable model evaluation and comparison.

Preview of the Next Lesson:
In today's lesson, the video mentioned a key heuristic: use ensemble methods for structured data, and "use deep learning or transfer learning" for unstructured data. This sets the stage perfectly for our next topic. We will begin our journey into the world of deep learning by learning to describe the structure of a multi-layer feedforward neural network, the foundational building block for more complex deep learning models.