• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Codemotion Magazine

We code the future. Together

  • Discover
    • Events
    • Community
    • Partners
    • Become a partner
    • Hackathons
  • Magazine
    • Backend
    • Frontend
    • AI/ML
    • DevOps
    • Dev Life
    • Soft Skills
    • Infographics
  • Talent
    • Discover Talent
    • Jobs
    • Manifesto
  • Companies
  • For Business
    • EN
    • IT
    • ES
  • Sign in

CodemotionNovember 19, 2025 9 min read

Beyond the Black Box: A Practical Guide to XAI for Developers

AI/ML
xai
facebooktwitterlinkedinreddit

Imagine building a machine learning system for a bank that needs to decide whether to approve mortgages worth hundreds of thousands of euros. The model works great: 94% accuracy, impeccable metrics. Everything’s perfect, until a rejected customer asks: “Why did you deny my loan?” And you, as a developer, have no clear answer to give.

This scenario isn’t hypothetical. It’s the daily reality of thousands of teams working with artificial intelligence. Machine learning models, especially the most complex ones like deep neural networks or gradient boosting ensembles, operate as black boxes: they receive input, produce output, but the internal decision-making process remains obscure even to those who created them.

Recommended article
November 12, 2025

Kimi K2 Thinking: China Pulls Ahead

Dario Ferrero

AI/ML

The Black Box Problem in Modern AI

When we talk about “black boxes” in the context of artificial intelligence, we refer to models that don’t allow us to understand the reasoning that leads to a particular result. Let’s take a concrete example: you’ve trained a neural network to diagnose diseases from radiographic images. The model identifies a tumor with 97% confidence. Excellent, right? But what happens if the doctor asks: “What did it base this diagnosis on?” The answer is frustrating: we don’t know for certain.

This opacity creates concrete problems in several areas:

In the financial sector, regulations like the European GDPR establish individuals’ right to obtain explanations about automated decisions that concern them. A bank using AI to assess credit risk must be able to justify every rejection. It’s not enough to say “the algorithm decided so.”

In the medical field, the stakes are even higher. A system that recommends cancer treatment or rules out a diagnosis must be transparent. Doctors need to understand whether the model has identified clinically relevant patterns or is basing itself on spurious correlations present in the training data.

In recruiting, CV screening algorithms can perpetuate biases hidden in historical data. If your model systematically discards candidates of a certain gender or background, you need to be able to identify and correct this. But how do you do it if you don’t understand what the model is looking at?

In predictive justice systems, used in some countries to assess the risk of recidivism, the lack of transparency raises fundamental ethical questions. Is deciding someone’s freedom based on an incomprehensible algorithm acceptable?

XAI: Making AI Understandable to Humans

This is where XAI comes in, an acronym for Explainable Artificial Intelligence. It’s not a single technique, but an entire field of research with an ambitious goal: making artificial intelligence models understandable to humans, without sacrificing their performance.

XAI is based on a fundamental principle: transparency isn’t a luxury, it’s a necessity. Not only for ethical or regulatory reasons, but also for practical ones. An interpretable model is easier to debug, improve, and put into production with confidence.

There are two main approaches in XAI:

Glass-box models are inherently interpretable. Their structure allows you to directly understand how they arrive at decisions. Think of linear regression: you can see exactly how much each variable contributes to the final result. The traditional trade-off was that these models sacrificed accuracy in exchange for interpretability.

Post-hoc explainers work with already trained black-box models. Once you have your ‘opaque’ but performant model, you apply explanation techniques after the fact. It’s like having an interpreter who translates the model’s decisions into understandable language.

InterpretML: Accessible XAI for Everyone

Among the libraries available for XAI in Python, InterpretML stands out for a rare balance between power and ease of use. Developed by Microsoft Research, this open-source library offers tools for both glass-box models and post-hoc explanations, with a unified interface that dramatically lowers the barrier to entry.

Installation is immediate:

python

pip install interpret

InterpretML particularly shines in two aspects: interactive visualization of explanations through an automatic web interface, and implementation of the Explainable Boosting Machine (EBM), a model that manages to combine competitive accuracy with the best black-box algorithms and total interpretability.

Explainable Boosting Machine: The Best of Both Worlds

EBM is a technique that deserves particular attention. It’s a Generalized Additive Model (GAM) type algorithm enhanced with boosting techniques. It sounds complex, but the concept is elegant: the model learns separate functions for each feature, then combines them additively to make the final prediction.

What does this mean in practice? That you can see exactly how each variable influences the result. If you’re predicting the risk of loan default, you can visualize how annual income, debt-to-income ratio, credit history, and other variables individually contribute to the final decision.

The advantage of EBM over simple linear models is that it can capture complex non-linear relationships. A variable can have a positive effect in one range and negative in another, and the model represents this clearly. And unlike random forests or neural networks, you can see and understand these relationships.

Practical Case: Iris Classification with Explanation

Let’s see InterpretML in action with a concrete example. We’ll use the classic Iris dataset, which contains measurements of 150 flowers belonging to three different species: setosa, versicolor, and virginica. For each flower we have four measurements: sepal length and width, petal length and width.

Although it’s a simple dataset, used mainly for teaching, it allows us to explore concepts that you’ll then apply to much more complex real problems.

python

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from interpret.glassbox import ExplainableBoostingClassifier
from interpret import show
import pandas as pd

<em># Load and prepare data</em>
iris = load_iris()
X = pd.DataFrame(iris.data, columns=iris.feature_names)
y = pd.Series(iris.target, name='species')

<em># Train/test split</em>
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

<em># Train EBM model</em>
ebm = ExplainableBoostingClassifier(random_state=42)
ebm.fit(X_train, y_train)

<em># Evaluation</em>
accuracy = ebm.score(X_test, y_test)
print(f"Test set accuracy: {accuracy:.2%}")Code language: HTML, XML (xml)

So far, nothing different from any other sklearn classifier. The magic begins when we generate the explanations.

Global Explanations: Understanding the Model as a Whole

Global explanations show us how the model works in general, across the entire dataset. Which variables are most important? How do they influence predictions?

python

<em># Generate global explanation</em>
ebm_global = ebm.explain_global(name="EBM - Iris Global")
show(ebm_global)Code language: HTML, XML (xml)

When you run show(ebm_global), InterpretML automatically starts a local server and opens the browser to an interactive dashboard. This interface is surprisingly rich considering you haven’t written a line of visualization code.

The first thing you’ll see is a bar chart showing the importance of each feature. In the case of the Iris dataset, you’ll typically discover that petal width is the most discriminating variable, followed by petal length. Sepal characteristics have less importance.

But InterpretML goes beyond. Clicking on each feature, you can visualize its “shape function”: a graph showing exactly how that variable influences the prediction. For petal width, for example, you’ll see that:

  • Values below 0.8 cm strongly predict the “setosa” class
  • Values between 1.0 and 1.8 cm indicate “versicolor”
  • Values above 1.8 cm suggest “virginica”

This is information you can share with domain experts. A botanist could confirm that these thresholds make sense from a biological perspective, or identify anomalies in the data.

Local Explanations: Understanding Individual Predictions

Global explanations are powerful, but often you need to understand why the model made a specific prediction on a particular example. This is where local explanations come in.

python

<em># Explanations for the first 5 test set examples</em>
ebm_local = ebm.explain_local(
    X_test.iloc[:5], 
    y_test.iloc[:5], 
    name="EBM - Iris Local"
)
show(ebm_local)Code language: PHP (php)

The local visualization is even more fascinating. For each example, you see a waterfall chart showing:

  • The base value (the model’s average prediction)
  • How each feature shifts the prediction toward one class or another
  • The final prediction

Let’s take a concrete example. Imagine a flower with these characteristics:

  • Sepal length: 5.8 cm
  • Sepal width: 2.7 cm
  • Petal length: 5.1 cm
  • Petal width: 1.9 cm

The model predicts “virginica” with high confidence. The local explanation shows you that:

  • Petal width (1.9 cm) contributes strongly toward “virginica” (+0.45)
  • Petal length (5.1 cm) also supports this prediction (+0.32)
  • Sepal length has a neutral effect (+0.03)
  • Sepal width slightly pushes toward “versicolor” (-0.08)

The net contribution leads to a strong prediction for “virginica.” You can see exactly which characteristics “voted” for which class and with what strength.

Beyond the Example: Real Applications

The Iris dataset is educational, but the concepts extend directly to real problems. Let’s look at some scenarios where I’ve seen InterpretML make a difference.

Credit Scoring with EBM

On a project for a fintech, we needed to build a credit scoring model. The requirements were clear: competitive accuracy with the best models and full interpretability for regulatory compliance.

python

from interpret.glassbox import ExplainableBoostingClassifier
import pandas as pd

<em># Typical features for credit scoring</em>
features = [
    'annual_income', 'debt_to_income_ratio', 'credit_history_length',
    'number_of_open_accounts', 'credit_utilization', 'payment_history_score',
    'number_of_inquiries', 'employment_length'
]

<em># Train EBM</em>
ebm_credit = ExplainableBoostingClassifier(
    max_bins=512,  <em># More bins to capture complex relationships</em>
    interactions=10,  <em># Consider feature interactions</em>
    learning_rate=0.01,
    max_rounds=5000
)

ebm_credit.fit(X_train[features], y_train)Code language: PHP (php)

Global explanations revealed interesting insights. The debt-to-income ratio had, as expected, a strong negative impact above 43%. But we discovered that the number of open accounts had an inverted U relationship: too few (0-2) or too many (>8) increased risk, while 3-7 accounts were optimal.

This type of insight is valuable. It not only satisfies regulatory requirements, but suggests targeted feature engineering and helps identify potential data issues.

Medical Diagnostics with Post-Hoc Explanations

For a medical image classification project, we had a black-box CNN model with excellent performance. We couldn’t sacrifice accuracy, but needed explainability. InterpretML also offers post-hoc explainers for these cases.

python

from interpret.blackbox import LimeTabular
from sklearn.ensemble import RandomForestClassifier

<em># Already trained black-box model</em>
rf_model = RandomForestClassifier(n_estimators=100)
rf_model.fit(X_train, y_train)

<em># LIME explainer</em>
lime = LimeTabular(
    model=rf_model.predict_proba, 
    data=X_train, 
    random_state=42
)

<em># Local explanation</em>
lime_local = lime.explain_local(
    X_test.iloc[:1], 
    y_test.iloc[:1],
    name="LIME Explanation"
)
show(lime_local)Code language: HTML, XML (xml)

LIME (Local Interpretable Model-agnostic Explanations) creates a local linear model around each prediction, allowing you to understand which features influenced that specific decision.

Managing Feature Interactions

A limitation of classic GAMs is that they assume features contribute independently. In reality, interactions often exist: the effect of one variable depends on the value of another.

InterpretML’s EBM handles this problem through interaction terms. You can specify how many interactions you want the model to consider:

python

ebm_interactions = ExplainableBoostingClassifier(
    interactions=15,  <em># Consider top 15 interactions</em>
    max_interaction_bins=32
)
ebm_interactions.fit(X_train, y_train)

<em># Visualize discovered interactions</em>
ebm_global = ebm_interactions.explain_global()
show(ebm_global)Code language: HTML, XML (xml)

The dashboard will show you not only the importance of individual features, but also significant interactions. For example, in a churn prediction model, you might discover that the interaction between “contract duration” and “number of complaints” is very informative: many complaints are tolerated for long-term customers, but for new customers even a few complaints strongly predict abandonment.

Comparing Interpretable and Black-Box Models

A legitimate doubt: how much do we sacrifice in terms of performance using interpretable models? InterpretML makes comparison easy.

python

from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from interpret.glassbox import ExplainableBoostingClassifier
from interpret.perf import ROC

<em># Train different models</em>
models = {
    'EBM': ExplainableBoostingClassifier(random_state=42),
    'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42),
    'Gradient Boosting': GradientBoostingClassifier(random_state=42)
}

results = {}
for name, model in models.items():
    model.fit(X_train, y_train)
    results[name] = {
        'model': model,
        'accuracy': model.score(X_test, y_test),
        'predictions': model.predict_proba(X_test)
    }

<em># Compare performance</em>
for name, result in results.items():
    print(f"{name}: {result['accuracy']:.4f}")

<em># Comparative ROC visualization</em>
roc = ROC(results['EBM']['model'].predict_proba)
roc_viz = roc.explain_perf(X_test, y_test, name='EBM')
show(roc_viz)Code language: PHP (php)

In my experience, EBM generally positions itself 1-3% accuracy compared to the best black-box gradient boosting. For many use cases, this trade-off is more than acceptable considering the gain in interpretability.

Best Practices for XAI in Production

Implementing XAI doesn’t just mean installing a library. Here are some lessons learned working with these tools in production.

Start with interpretability from the beginning. Don’t wait to have a model already in production to think about explainability. Include it in project requirements from the start. It’s much easier to build with transparency in mind than to add it later.

Document explanations along with code. InterpretML’s visualizations are great for exploration, but you should also save key explanations as versioned artifacts. This creates an auditable trail.

python

<em># Save explanations for future audit</em>
from interpret import preserve

preserve(ebm_global, 'ebm_global_explanation.html')
preserve(ebm_local, 'ebm_local_explanation.html')Code language: PHP (php)

Validate explanations with domain experts. Interpretability is useless if no one with relevant expertise verifies it. Organize sessions where you show the model’s explanations to people who know the problem. Often insights or hidden issues emerge.

Monitor explanation stability. In production, don’t just monitor model performance, but also how explanations change over time. A sudden change in feature importance can signal data drift or data quality issues.

python

<em># Track feature importances over time</em>
import json
from datetime import datetime

def log_feature_importance(model, version):
    importance = dict(zip(
        model.feature_names_in_,
        model.feature_importances_
    ))
    
    log_entry = {
        'timestamp': datetime.now().isoformat(),
        'model_version': version,
        'feature_importance': importance
    }
    
    with open('feature_importance_log.jsonl', 'a') as f:
        f.write(json.dumps(log_entry) + '\n')Code language: PHP (php)

Use different explanations for different audiences. Technical explanations for the data science team are different from those for business stakeholders or end users. InterpretML allows you to generate different views of the same insights.

Limits and Challenges of XAI

XAI isn’t a panacea. It’s important to recognize the limits.

Explanations can be misleading. A plausible explanation isn’t necessarily correct. The model might be based on spurious correlations that seem reasonable superficially. Explainability must be accompanied by rigorous validation.

Complexity-interpretability trade-off. For extremely complex problems (advanced computer vision, NLP, etc.), truly interpretable models may not be sufficient. In these cases, post-hoc explainers are the only option, but they’re approximations.

Computational cost. Generating detailed explanations, especially for large black-box models, can be expensive. In production with low latency, this can be problematic. You need to balance between on-demand and pre-calculated explanations.

Doesn’t solve fundamental ethical problems. Explainability helps identify biases, but doesn’t automatically eliminate them. An interpretable model that discriminates is still problematic. XAI is a tool, not a magic solution.

The Future of XAI

The field of XAI is evolving rapidly. Some interesting directions:

Causal explanations: going beyond correlations to understand cause-effect relationships. Libraries like DoWhy are exploring this territory.

Multimodal explanations: for models working with images, text, and structured data together, explanation techniques that integrate different modalities are needed.

Formal certification: not just explanations, but formal guarantees on model behavior. Formal verification techniques applied to machine learning.

Interactive explanations: interfaces where users can explore “what-if” scenarios and see how the prediction would change by modifying different features.

Conclusion: Transparency as Competitive Advantage

XAI adoption shouldn’t be seen as a constraint imposed by regulations or ethical demands, but as a strategic advantage. Interpretable models are easier to debug, more robust, more reliable in production, and generate greater user trust.

InterpretML represents an excellent tool to start this journey. Its ease of use lowers the barrier to entry, while the power of EBM demonstrates that interpretability and performance aren’t necessarily in conflict.

The next time you train a model, before automatically reaching for a random forest or neural network, consider: do you really need that complexity? Could you get comparable results with a model you can explain and fully understand?

The answer might surprise you. And your users, stakeholders, and yourself six months from now will thank you for choosing transparency.

Codemotion Collection Background
Top of the week
Our team’s picks

Want to find more articles like this? Check out the Top of the week collection, where you'll find a curated selection of fresh, new content just for you.

Share on:facebooktwitterlinkedinreddit

Tagged as:interpretML reti neurali xai

Codemotion
Articles wirtten by the Codemotion staff. Tech news, inspiration, latest treends in software development and more.
Beyond Big Tech: building a European public digital identity
Previous Post

Footer

Discover

  • Events
  • Community
  • Partners
  • Become a partner
  • Hackathons

Magazine

  • Tech articles

Talent

  • Discover talent
  • Jobs

Companies

  • Discover companies

For Business

  • Codemotion for companies

About

  • About us
  • Become a contributor
  • Work with us
  • Contact us

Follow Us

© Copyright Codemotion srl Via Marsala, 29/H, 00185 Roma P.IVA 12392791005 | Privacy policy | Terms and conditions