Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • AI
Labs

Optimizing Models with Hyperparameter Tuning

In this lab, you'll practice optimizing machine learning models through hyperparameter tuning. When you're finished, you'll have compared multiple tuning techniques and identified optimal model parameters.

Lab platform
Lab Info
Level
Beginner
Last updated
Jan 17, 2026
Duration
35m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use, and consent to receive marketing emails from Pluralsight.
Table of Contents
  1. Challenge

    Introduction

    Introduction

    Welcome to the Hyperparameter Tuning Code Lab. In this hands-on lab, you'll optimize machine learning models using various hyperparameter tuning techniques including manual tuning, Grid Search, Random Search, and Bayesian Optimization. You'll train baseline models, systematically search for optimal parameters, and compare the efficiency of different tuning approaches.

    Background

    You're a data scientist at Globomantics tasked with improving the performance of a customer churn prediction model. The baseline model has 75% accuracy, but the business needs at least a 10% improvement in prediction accuracy to justify the investment in their customer retention program. Your task is to apply hyperparameter tuning techniques to optimize the model and find the best configuration while avoiding overfitting.

    Your team has decided to evaluate multiple tuning strategies: manual tuning to understand parameter impacts, Grid Search for exhaustive exploration, Random Search for efficient sampling, and Bayesian Optimization for intelligent parameter selection. Success in this optimization effort will enable the deployment of an effective customer retention system.

    Familiarizing with the Program Structure

    The lab environment includes the following key files:

    1. data_loader.py: Loads and preprocesses the customer churn dataset
    2. baseline_model.py: Trains and evaluates a baseline Random Forest model
    3. manual_tuning.py: Implements manual hyperparameter adjustments
    4. grid_search_tuning.py: Implements Grid Search with cross-validation
    5. random_search_tuning.py: Implements Randomized Search
    6. bayesian_tuning.py: Implements Bayesian Optimization
    7. xgboost_tuning.py: Implements XGBoost with early stopping
    8. evaluate_models.py: Compares all tuning approaches and generates reports

    The environment uses Python 3.10+ with scikit-learn for ML algorithms, XGBoost for gradient boosting with early stopping, scikit-optimize for Bayesian optimization, NumPy for numerical operations, and Matplotlib for visualization. All dependencies are pre-installed in the lab environment.

    To run scripts, use the terminal with commands like python3 baseline_model.py. Results will be saved to the output/ directory.

    Important Note: Complete tasks in order. Each task builds on the previous one. Test your code frequently by running the provided scripts to catch errors early.

    info > If you get stuck on a task, there are solution files provided for you located in the solution directory in your filetree.

  2. Challenge

    Establishing the Baseline

    Understanding Data Loading and Baseline Models

    Before optimizing any model, you need a baseline to measure improvement against. In this task, you'll configure the data pipeline and create a baseline Random Forest model with default parameters. The data_loader.py module handles loading and preprocessing the Globomantics customer churn dataset, while baseline_model.py trains an initial model. This baseline accuracy (typically around 75%) becomes your reference point for evaluating whether your hyperparameter tuning efforts are actually improving performance.

  3. Challenge

    Manual and Grid Search Tuning

    Understanding Manual Hyperparameter Tuning

    Now that you have a baseline, the next step is understanding how individual hyperparameters affect model performance. The manual_tuning.py module implements a systematic approach to testing one parameter at a time. By iterating through different values for n_estimators and max_depth, you'll observe their direct impact on accuracy. This manual exploration builds intuition about parameter sensitivity before moving to automated search methods. ### Understanding Grid Search

    Manual tuning is insightful but tedious for multiple parameters. Grid Search automates this by exhaustively testing every combination of parameters you specify. The grid_search_tuning.py module uses scikit-learn's GridSearchCV to systematically evaluate all combinations with cross-validation. In Tasks 3 and 4, you'll first configure the parameter grid and cross-validation settings, then execute the search. Grid Search guarantees finding the best combination within your specified values, but becomes computationally expensive as the search space grows.

  4. Challenge

    Advanced Search Strategies

    Understanding Random Search

    Grid Search tests every combination, which becomes impractical for large search spaces. Random Search offers a more efficient alternative by sampling random combinations from parameter distributions. The random_search_tuning.py module uses RandomizedSearchCV to explore a broader range of values with fewer iterations. Research has shown that Random Search often finds equally good or better parameters than Grid Search in less time, especially when only a few parameters significantly impact performance. ### Understanding Bayesian Optimization

    Both Grid Search and Random Search treat each evaluation independently, ignoring information from previous trials. Bayesian optimization takes a smarter approach by building a probabilistic model of the objective function and using it to select promising parameter combinations. The bayesian_tuning.py module uses BayesSearchCV from scikit-optimize to intelligently explore the search space, learning from each evaluation to focus on regions likely to yield better results. This makes it ideal when model training is expensive.

  5. Challenge

    Early Stopping and Final Comparison

    Understanding Early Stopping with XGBoost

    The tuning methods so far optimize parameters before training begins. Early stopping is a different technique that optimizes during training by monitoring validation performance and stopping when no improvement is seen. The xgboost_tuning.py module demonstrates this with XGBoost, a powerful gradient boosting algorithm. By setting early_stopping_rounds, the model automatically stops training before overfitting occurs, saving computation time while often achieving better generalization. ### Understanding Model Comparison

    After implementing multiple tuning strategies, you need to compare their effectiveness. The evaluate_models.py module brings together results from all approaches to create a comprehensive comparison. You'll analyze trade-offs between accuracy improvement and computational cost, helping you understand when to use each method. This final evaluation demonstrates that there's no single "best" tuning approach---the right choice depends on your specific constraints and requirements.

  6. Challenge

    Conclusion

    Conclusion

    Congratulations on successfully completing the Optimizing Models with Hperparameter Tuning lab! You've implemented and compared multiple hyperparameter tuning strategies:

    1. Manual Tuning: Understand individual parameter effects
    2. Grid Search: Exhaustive but computationally expensive
    3. Random Search: Efficient for large search spaces
    4. Bayesian Optimization: Intelligent exploration with fewer iterations
    5. Early Stopping: Prevents overfitting and saves computation

    Key Takeaways

    • Start with a baseline model to measure improvement
    • Use cross-validation for reliable performance estimates
    • Random Search often matches Grid Search with less computation
    • Bayesian optimization is ideal when evaluations are expensive
    • Early stopping is essential for iterative algorithms
About the author

Angel Sayani is a Certified Artificial Intelligence Expert®, CEO of IntellChromatics, author of two books in cybersecurity and IT certifications, world record holder, and a well-known cybersecurity and digital forensics expert.

Real skill practice before real-world application

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight