This tutorial will explore a variety of SHAP IQ visualisations to gain insights on how a machine-learning model makes its predictions. These visuals help break down complex model behavior into interpretable components—revealing both the individual and interactive contributions of features to a specific prediction. See the Full Codes here.
Installing dependencies
Scikit-Learn Numpy Seaborn Pandas!pip Install shapiq Overrides
from sklearn.ensemble import RandomForestRegressor
From sklearn.metrics, import the mean_squared_error and r2_score
from sklearn.model_selection import train_test_split
Import tqdm from tqdm.asyncio
import shapiq
print(f"shapiq version: {shapiq.__version__}")
Importing datasets
We’ll be using the MPG data set in this tutorial. It’s a dataset that we will load from Seaborn. This dataset provides information about different car models. It includes features such as horsepower, weight, origin, etc. Look at the Full Codes here.
Buy Seaborn as sns
df = "sns.load_dataset""mpg")
df
The dataset is processed
Label Encoding converts the columns of categorical data into numeric form, which is suitable for training models.
import pandas as pd
from sklearn.preprocessing import LabelEncoder
Rows with missing values should be removed
Dropna = drop = df()
# Coding the Origin Column
LabelEncoder()
df.loc[:, "origin"] = le.fit_transform(df["origin"])
df['origin'].unique()
for i, label in enumerate(le.classes_):
print(f"{label} → {i}")
Splitting the data into training & test subsets
Choose features to target
Drop a column by using df.drop (columns=).["mpg", "name"])
y = df["mpg"]
feature_names = X.columns.tolist()
x_data, y_data = X.values, y.values
Train test split
x_train, x_test, y_train, y_test = train_test_split(x_data, y_data, test_size=0.2, random_state=42)
Model Training
The Random Forest Regressor is trained with 10 trees and a depth maximum of 10. (n_estimators=10) A fixed random_state ensures reproducibility.
# Train Model
model = RandomForestRegressor(random_state=42, max_depth=10, n_estimators=10)
model.fit(x_train, y_train)
Model Evaluation
# Evaluate
mse = mean_squared_error(y_test, model.predict(x_test))
r2 = r2_score(y_test, model.predict(x_test))
print(f"Mean Squared Error: {mse:.2f}")
print(f"R2 Score: {r2:.2f}")
Understanding a local instance
Select a test instance that has instance_id = 7 to see how the model came up with its prediction. This will print out the predicted value and feature values. Look at the Full Codes here.
Select a local example to be described
instance_id = 7
x_explain = x_test[instance_id]
y_true = y_test[instance_id]
y_pred = model.predict(x_explain.reshape(1, -1))[0]
print(f"Instance {instance_id}, True Value: {y_true}, Predicted Value: {y_pred}")
for i, feature in enumerate(feature_names):
print(f"{feature}: {x_explain[i]}")
Generating explanations for Orders with Multiple Interactions
We use the shapiq software package to generate Shapley explanations for various interaction orders. In particular, we compute
- Order 1 (Standard Shapley): Contributions to individual features
- Order 2: Pairwise interactions and combined effects
- Order N (Full interaction): all interactions, up to and including the number of features
Create explanations to different order
Get the feature names with feature_names=list(X.columns).
n_features = len(feature_names)
si_order: dict[int, shapiq.InteractionValues] = {}
Order in Tqdm[1, 2, n_features]):
Index = "k-SII" if order > 1 else "SV" Explainer automatically sets #.
explainer = shapiq.TreeExplainer(model=model, max_order=order, index=index)
si_order[order] = explainer.explain(x=x_explain)
si_order
1. Force Chart
This powerful tool helps you understand the machine-learning model’s reasoning behind a particular prediction. It shows the baseline (i.e. expected value before seeing features) of the machine learning model, and then how each feature affects that prediction. “pushes” Predictions higher or lower.
This plot is:
- The red bars indicate features that can increase your prediction.
- Blue bars indicate those which decrease.
- Each bar is sized according to its magnitude.
When Shapley interactions values are used, the force plot is able to visualize both individual contributions and also interactions among features. The force plot is particularly useful for analyzing complex models because it allows you to see how different features combine together and influence the final outcome. See the Full Codes here.
Order sv by comparing it to order sv[1] Get the SV
Ordering si = ordering[2] The 2-SII
Mi = si_order[n_features] Moebius the transform
sv.plot_force(feature_names=feature_names, show=True)
si.plot_force(feature_names=feature_names, show=True)
mi.plot_force(feature_names=feature_names, show=True)
We can tell from the first plot that the value of the base is 23.5. Features like Cylinders (weight), Horsepower and Displacement all have a positive impact on the predictions, pushing them above the baseline. Model Year, Acceleration, and Weight all have negative impacts, causing the prediction to fall.
2. Waterfall Chart
The waterfall plot was introduced along with shap, and is a popular method of visualizing Shapley values. The waterfall plot shows the impact of different features on the predicted value. It is important to note that the waterfall plot automatically group features of very little impact into one category. “other” The chart is now organized by category to make it easier for you to read. Look at the Full Codes here.
sv.plot_waterfall(feature_names=feature_names, show=True)
si.plot_waterfall(feature_names=feature_names, show=True)
mi.plot_waterfall(feature_names=feature_names, show=True)



3. The Network Plot
The Shapley network plot illustrates how different features interrelate using interactions of first- or second-order. The size of the node reflects the impact each feature has, whereas edge width and colour show strength and direction. This is especially useful when you have many features to deal with, as it reveals complex interactions which simpler plots may miss. See the Full Codes here.
si.plot_network(feature_names=feature_names, show=True)
mi.plot_network(feature_names=feature_names, show=True)

4. SI Graph Plot
This plot is a network-like plot that visualizes all interactions at higher levels as hyper-edges. The size of the node shows impact on individual features, while color, edge width and transparency show strength and direction. The model provides an overall view of features and their influence on the predictions. Visit the Full Codes here.
We abbreviate feature names because they are plotted within the nodes
abbrev_feature_names = shapiq.plot.utils.abbreviate_feature_names(feature_names)
sv.plot_si_graph(
feature_names=abbrev_feature_names,
show=True,
size_factor=2.5,
node_size_scaling=1.5,
plot_original_nodes=True,
)
si.plot_si_graph(
feature_names=abbrev_feature_names,
show=True,
size_factor=2.5,
node_size_scaling=1.5,
plot_original_nodes=True,
)
mi.plot_si_graph(
feature_names=abbrev_feature_names,
show=True,
size_factor=2.5,
node_size_scaling=1.5,
plot_original_nodes=True,
)



5. Bar Plot
The bar plot can be used to explain global issues. The bar plot, which can be used locally or globally, summarizes overall feature importance by displaying the average absolute Shapley values (or interaction values) across all instances. It highlights the feature interaction that contributes most to a shapiq. Take a look at the Full Codes here.
Explaining = []
explainer = shapiq.TreeExplainer(model=model, max_order=2, index="k-SII")
If you want to find the instance_id using tqdm (20 ranges):
x_explain = x_test[instance_id]
si = explainer.explain(x=x_explain)
explanations.append(si)
shapiq.plot.bar_plot(explanations, feature_names=feature_names, show=True)

“Distance” The following are some examples of how to get started: “Horsepower” The bar plot shows that these features have the highest mean absolute Shapley interaction values, which means they are most important for the predictions of the model. It is clear from the high Shapley absolute mean values on the bar graph.
When looking at interactions of second order (i.e. two features interacting together), combinations are also considered. “Horsepower × Weight” The following are some examples of how to get started: “Distance × Horsepower” They have a significant influence on each other. They have a combined attribution around 1.4. This indicates that their interactions are important in shaping the predictions of the model beyond what they contribute individually. The non-linear relationship between the features of the model is highlighted by this.
Click here to find out more Full Codes here. Check out our website to learn more. GitHub Page for Tutorials, Codes and Notebooks. Also, feel free to follow us on Twitter Don’t forget about our 100k+ ML SubReddit Subscribe now our Newsletter.



