Making Machine Learning Models Explainable with the Python Shapash Library

Machine learning models have become increasingly powerful and complex, achieving breakthrough performance on challenging tasks like image recognition, natural language processing, and predictive analytics. However, many state-of-the-art ML models are essentially "black boxes"—even the data scientists and developers who create them struggle to understand and explain their inner workings and outputs. This opaqueness poses major challenges for debugging models, building trust with stakeholders, and complying with regulations in industries like healthcare and finance.

Fortunately, a growing ecosystem of tools for ML explainability and interpretability has emerged to illuminate the black box. One of the most promising is Shapash, an open source Python library that enables data scientists to visualize how supervised machine learning models make predictions. With just a few lines of code, Shapash provides clear and understandable insights into a model‘s global logic and its decision-making process for individual predictions.

In this article, we‘ll dive deep into the Shapash library and walk through how to use it to analyze and explain a machine learning model. Whether you‘re a data scientist looking to build more transparent and trustworthy models or a business leader seeking to harness the power of AI and ML, this guide will help you understand machine learning more easily using Shapash. Let‘s get started!

The Challenge of Interpreting Machine Learning Models

Today‘s most accurate machine learning models, like deep neural networks and ensembles of decision trees, are incredibly complex, with millions of parameters and nonlinear interactions between variables. While these sophisticated architectures are highly effective for learning patterns from data and making predictions, they are not inherently interpretable or explainable to humans.

This lack of transparency creates significant issues, especially when applying ML to high-stakes domains like medical diagnosis, credit decisioning, and autonomous vehicles. Without the ability to understand and explain model outputs, it‘s difficult to:

  • Debug and improve model performance
  • Validate that a model is fair and unbiased
  • Comply with "right to explanation" regulations like GDPR
  • Build trust and adoption with end-users
  • Make reliable decisions based on model predictions

To address these challenges and make machine learning more accessible to a wider audience, we need tools and techniques to explain ML models in clear and understandable terms. That‘s where Shapash comes in.

Introducing the Shapash Library for ML Explainability

Shapash is an open source Python library that provides a simple interface for visualizing and understanding how supervised ML models—including popular frameworks like scikit-learn, XGBoost, LightGBM, and CatBoost—make predictions. With just a few lines of code, you can use Shapash to:

  • Explore global feature importance and feature contributions
  • Dive deep into local, instance-level explanations for individual predictions
  • Compare and contrast model behaviors across different subpopulations
  • Build interactive web apps and reports to share insights with stakeholders

Under the hood, Shapash uses a powerful technique for ML interpretability called SHapley Additive exPlanations (SHAP). SHAP values quantify the marginal contribution of each feature to a model‘s prediction for an individual instance. By aggregating and visualizing SHAP values across a dataset, Shapash provides clear and intuitive explanations of how a model works.

Let‘s take a closer look at some of Shapash‘s key features and capabilities.

Key Features and Capabilities of Shapash

Shapash provides a comprehensive set of tools for analyzing and explaining any supervised machine learning model through a simple, unified API. Some of the key features include:

  • Support for popular ML frameworks: Shapash seamlessly integrates with models built in scikit-learn, XGBoost, LightGBM, CatBoost, and more. As long as your model exposes standard methods like .predict() and .predict_proba(), Shapash can explain it.

  • Intuitive visualizations: Shapash automatically generates clear and visually appealing plots, charts, and interactive dashboards to communicate key insights about your models. No need to be a matplotlib expert!

  • Unified interface for global and local explanations: With the same Shapash object, you can explore both global feature importance across your entire dataset and local feature contributions for individual predictions.

  • Consistent support for different data and model types: Whether you‘re working with numerical, categorical, or text data for regression or classification tasks, Shapash has you covered with intelligible, human-friendly feature labels and visualizations.

  • Flexible output formats: Shapash can generate static HTML reports, interactive web apps with Dash, notebook widgets for Jupyter, and simplified text explanations for sharing key takeaways with non-technical audiences.

  • Extensible and customizable: Shapash plays nicely with existing data science and machine learning workflows in Python. You can use Shapash objects as building blocks for more advanced explainability techniques or easily extend the library with your own custom visualizations.

Now that we‘ve seen the high-level capabilities of Shapash, let‘s walk through an example of how to use the library to explain a machine learning model.

Tutorial: Analyzing a Model with Shapash

To see Shapash in action, we‘ll use a public dataset of home sale prices to train a regression model predicting the sale price of a house based on features like its size, age, and location. We‘ll then apply Shapash to understand what the model has learned and how it makes predictions.

Step 1: Install Shapash

First, make sure you have Shapash installed in your Python environment:

pip install shapash

If using Jupyter notebooks, you‘ll also need a couple of additional dependencies:

pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension

Step 2: Train a Model

Next, let‘s train a simple regression model using scikit-learn. We‘ll use a random forest regressor and preprocess the data using Shapash‘s built-in SmartPredictor class:

from shapash.data.data_loader import DataLoader
from shapash.explainer.smart_predictor import SmartPredictor
from sklearn.ensemble import RandomForestRegressor

house_df, house_dict = DataLoader().load_house_prices()
y_df = house_df[‘SalePrice‘].to_frame()
X_df = house_df.drop([‘SalePrice‘], axis=1)

predictor = SmartPredictor(RandomForestRegressor(n_estimators=50))
predictor.fit(X_df, y_df)

Step 3: Instantiate a Shapash Explainer

With a trained model in hand, we can initialize a Shapash explainer object that will be our primary interface for generating visualizations and insights:

from shapash.explainer.smart_explainer import SmartExplainer

explainer = SmartExplainer(predictor) 

Step 4: Compute and Visualize Feature Importances

To understand which features have the greatest overall impact in the model, we can plot the global feature importances:

explainer.plot.features_importance()

Shapash Feature Importance Plot

This plot shows us that the size of the house (GrLivArea), the overall quality rating (OverallQual), and the year built (YearBuilt) are the most significant drivers of price, while other factors like the type of road access (Street) and zoning classification (MSZoning) are less relevant.

Step 5: Dive Into Feature Contributions

While global feature importance gives us a high-level view, we can also use Shapash to visualize how each feature contributes to individual predictions locally. This is especially useful for debugging outliers or unexpected results.

To explain the model‘s output for a particular house, we can generate a summary plot of the top feature contributions:

explainer.plot.contribution_plot("OverallQual")

Shapash Feature Contribution Plot

Here we see that for houses with high overall quality ratings (8-10), this feature pushes the price prediction significantly higher (green bars), while lower quality ratings (1-5) drag the price down below the baseline (red bars). We can see how the model has learned a positive, nonlinear relationship between quality and price.

Step 6: Explore Instance-Level Predictions

To really dig into the "local" explanations for a single data point, Shapash provides an interactive visualization for exploring feature contributions:

explainer.plot.local_plot(X_df.iloc[123])

Shapash Local Plot

In this example, we see how the model arrives at a predicted price of $140,000 for a specific house. The waterfall chart shows the cumulative contributions of each feature moving from left to right. The total living area of 1,500 square feet pushes the predicted price up significantly above the baseline, while the relatively old age (YearBuilt=1920) and low overall quality rating (OverallQual=5) drag it back down. With this plot, we can see exactly how the model combines information from different features to reason about a particular instance.

Step 7: Analyze Cohorts and Subpopulations

Shapash also provides tools for comparing and contrasting model behaviors across different subpopulations of data. This can be useful for assessing bias and fairness issues.

For example, we might want to analyze how our house price model performs for homes in different neighborhoods:

explainer.plot.compare_plot(
    X_df.iloc[[123, 456]], 
    subtitle=["Victorian Village", "Upper Arlington"]
)  

Shapash Compare Plot

This plot allows us to see how the same features can push the model‘s predictions in different directions depending on the neighborhood context. In Upper Arlington, an affluent suburb, the 1,500 square foot size contributes to a lower than average predicted price, while in the more urban Victorian Village neighborhood, this would be considered a relatively large home.

Through just this small set of examples, we‘ve already seen how Shapash allows us to inspect and explain machine learning models from multiple angles at different levels of granularity. By generating these kinds of insights, data scientists can build more robust and trustworthy models, while stakeholders can feel confident in the model‘s reliability and absence of bias.

Sharing Insights with Shapash Web Apps and Reports

In addition to interactive visualizations in the Python environment, Shapash also enables data scientists to easily package up insights for wider audiences through self-contained web apps and reports.

Using the Dash framework, we can launch an interactive Shapash dashboard to let end-users explore model explanations with just a few lines of code:

from shapash.webapp.smart_app import SmartApp

app = SmartApp(explainer)
app.run(port=8050, host= "127.0.0.1")

Shapash Web App

The Shapash web app provides a no-code interface for dynamically generating visualizations like feature importance, feature contributions, and instance-level explanations based on user inputs. Stakeholders can slice and dice the data to their hearts‘ content without writing a single line of Python or making a GitHub account.

If you prefer a more narrative, report-style format, Shapash can also export interactive HTML documents that walk readers through key insights and takeaways:

app.generate_report(output_file=‘report.html‘)  

These kinds of multimedia communication tools are essential for democratizing access to machine learning insights and driving adoption in real-world applications. Rather than handing over an inscrutable .pkl file or Jupyter notebook, data scientists can use Shapash to make their work accessible, engaging, and persuasive to decision-makers.

Benefits and Use Cases

We‘ve covered a lot of ground in this article, but you may still be wondering: what are the concrete benefits and use cases of using a tool like Shapash to explain machine learning models? There are many, but a few of the most significant include:

  • Regulatory compliance: In industries like credit and healthcare, "black box" models are increasingly unacceptable to both government agencies and public opinion. Shapash enables data scientists in these domains to provide clear explanations of how models operate to comply with current and future regulations around transparency, fairness, and privacy.

  • Debugging and improving model performance: By analyzing feature importances and contributions, data scientists can identify potential sources of bias, overfitting, data leakage, and other issues. Shapash facilitates a tight feedback loop between explanations and iterations to make models more robust and reliable.

  • Building trust with stakeholders: For non-technical audiences like executives, regulators, and customers, Shapash‘s intuitive visualizations provide a powerful communication tool to justify model behaviors and build confidence in automated decisioning systems. People are more likely to adopt and trust models they feel they understand.

  • Informing human-in-the-loop decisioning: Even when models are put into production, Shapash allows human operators to dig into the "reasoning" behind individual predictions to validate or override them. Explainable AI is not just about machines explaining themselves to humans, but about humans and algorithms enhancing each other‘s intelligence.

Getting Started with Shapash

If you‘re excited to start using Shapash to interpret your own machine learning models, you can find detailed installation instructions, examples, and documentation on the project‘s GitHub page: https://github.com/MAIF/shapash

The Shapash team has provided a comprehensive suite of Jupyter notebooks that walk through key functionality and a public demo web app at https://shapash-demo.ossbymaif.fr/.

The library is under active development, with new features and improvements being added regularly. The maintainers are very responsive to issues and pull requests on GitHub, and there is a growing community of Shapash users and contributers sharing knowledge on platforms like Stack Overflow and Twitter with the #shapash tag.

Whether you‘re a seasoned practitioner looking to add Shapash to your toolbox or a beginner just getting started with data science and machine learning, the library provides an intuitive jumping off point to start exploring and explaining AI.

Conclusion

As machine learning models grow more sophisticated, the need for tools to interpret them and communicate their insights grows more acute. The Shapash library provides an accessible but powerful framework for data scientists to explain how any supervised ML model works under the hood using techniques like feature importance, feature contributions, local explanations, and cohort analysis.

By making the inner workings of machine learning algorithms more transparent and understandable to humans, Shapash and related XAI tools can help organizations reap the benefits of AI while mitigating its risks and blindspots. They are not a panacea for the many challenges of integrating AI into human systems and institutions, but they are an essential component.

Achieving a future in which AI and machine learning enhance rather than subvert or constrain human agency will require ongoing collaboration between disciplines. Data scientists, software engineers, ethicists, social scientists, legal experts, activists, and the broader public will need to work together to develop technical and normative frameworks for making algorithms interpretable, auditable, and accountable.

The Shapash library is an exciting step forward on this journey, providing a flexible set of building blocks for data scientists to make machine learning more explainable for any audience. We hope this article inspires you to give it a try and start explaining your own models!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts