Featured resource
Tech Upskilling Playbook 2025
Tech Upskilling Playbook

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

Learn more
  • Labs icon Lab
  • AI
Labs

Classification Model Explainability

In this lab, you’ll practice Interpreting classification models. When you’re finished, you’ll have an understanding of high level interpretations for different classification models..

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 25m
Last updated
Clock icon Aug 04, 2025

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Lab Introduction

    Welcome to the Classification Model Explainability lab.

    In this lab you will work through creating two classification models, a decision tree and a random forest classifier. You will learn about key features of each model and how they affect the explainability of the models. You will also review how to best retrieve explainability results from each model.

    The lab's code will be heavily guided as the most important aspects to know when creating an AI are the higher level concepts and less the specific syntax.

  2. Challenge

    Preprocessing Data

    To start with the synthetic data creation you will generate the data and rename the features for readability before splitting the data into proper test and train sets.

    X, y = make_classification(n_samples=500, n_features=6, n_informative=4, 
                               n_redundant=0, n_classes=2, random_state=42)
    feature_names = [f'Feature {i}' for i in range(X.shape[1])]
    

    Ensure the random state is always defined when splitting datasets.

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
    
  3. Challenge

    Decision Tree Creation and Interpretation

    To start interpreting the results of your decision tree you will first need to train it on the split data. In this case the code defines the max depth as 4, this can go higher or lower depending on key features such as

    • number of expected classification results
    • number of features used
    • tolerance for overfitting risk You can change the depth for this lab and experiment with the results and the effect on explainability.
    clf = DecisionTreeClassifier(max_depth=4, random_state=42)
    clf.fit(X_train, y_train)
    

    After creating the model and training it, you can test how well the model is performing with the classification report.

    y_pred = clf.predict(X_test)
    print("Classification Report:\n", classification_report(y_test, y_pred))
    

    From here you can actually display the decision tree allowing you to see exactly how the decision tree splits the data and at what thresholds, and which branches reference which data throughout the entire depth.

    plt.figure(figsize=(12, 8))
    tree.plot_tree(clf, filled=True, feature_names=[f'Feature {i}' for i in range(X.shape[1])],
                   class_names=[f'Class {i}' for i in range(len(set(y)))])
    plt.title("Decision Tree Visualization")
    plt.show()
    

    This makes decision trees extremely interpretable as you can follow the exact path of logic for every single decision the tree makes.

    Decision trees tend to have lower accuracy and flexibility for complex functions than other ensemble methods but in key areas such as in a medical field this high interpretability is worth the tradeoff.

  4. Challenge

    Random Forest Creation and Interpretation

    Starting with training:

    model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=42)
    model.fit(X_train, y_train)
    

    and evaluating:

    y_pred = model.predict(X_test)
    print("Classification Report:\n", classification_report(y_test, y_pred))
    

    the random forest model, you can get an idea of the accuracy and other key metrics of the model. Once you have an understanding of the model's key metrics you can move onto getting an idea of how this ensemble model works on a global scale. Using the feature_importances_ function you can get an idea of how each feature effects the random forest, after the model has been fully trained.

    importances = model.feature_importances_
    plt.figure(figsize=(8, 5))
    plt.barh(feature_names, importances)
    plt.title('Random Forest Feature Importances')
    plt.xlabel('Importance')
    plt.ylabel('Feature')
    plt.show()
    

    Because these values are only after the model has been fully trained we cannot see how each individual feature affected the predictions for a specific feature set, but we get an idea of how valuable each feature is to keep to assist in optimization of the model.

    If you wanted to specifically see how a model predicted a certain feature set you can select it and pass it in using SHAP's plots.force function. In this case you will test how to create the specific prediction using the first feature set in the test dataset.

    explainer = shap.TreeExplainer(model)
    shap_values_instance = explainer.shap_values(X_test[0])
    
    # Visualize the force plot for that instance
    shap.initjs() # Initialize JavaScript for interactive plots
    shap.plots.force(explainer.expected_value[0], shap_values_instance)
    

    Once the javascript graph is generated you can see how each feature affected the classification, you can also modify the X and Y axis to try and gain a better understanding of how much each feature affected the classification results in either direction.

I am, Josh Meier, an avid explorer of ideas an a lifelong learner. I have a background in AI with a focus in generative AI. I am passionate about AI and the ethics surrounding its use and creation and have honed my skills in generative AI models, ethics and applications and thrive to improve in my understanding of these models.

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.