Lesson: Examining Shape-IQ recognition – MarktechPost

In this lesson, we will investigate the width of the Shape-IQ Viilalations which provide understanding on how the machine's learning model arrives at the cost. These material things help a complex model decrease into changing elements – that reflect both guidelines for interactive features. Look Full codes here.
To include leaning
!pip install shapiq overrides scikit-learn pandas numpy seaborn
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
from tqdm.asyncio import tqdm
import shapiq
print(f"shapiq version: {shapiq.__version__}")
Importing Dataset
In this lesson, we will use the MPG (miles with a gallon) dataset, which will upload directly to a sail librarian. This data contains information about a variety of car models, including features such as horse, weight, and origin. Look Full codes here.
import seaborn as sns
df = sns.load_dataset("mpg")
df
Processing Dataset
We use incarcut incision to modify the column (s) to distinguish into the number format, making them ready for model training.
import pandas as pd
from sklearn.preprocessing import LabelEncoder
# Drop rows with missing values
df = df.dropna()
# Encoding the origin column
le = LabelEncoder()
df.loc[:, "origin"] = le.fit_transform(df["origin"])
df['origin'].unique()
for i, label in enumerate(le.classes_):
print(f"{label} → {i}")
To distinguish data from training and testing relatives
# Select features and target
X = 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)
Example Training
We train the random forest Regreasor with high depths of 10 trees and 10 decisions (n_estimators = 10). The random Medrate_state guarantees the reinstate.
# Train model
model = RandomForestRegressor(random_state=42, max_depth=10, n_estimators=10)
model.fit(x_train, y_train)
A model test
# 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}")
Defining the instance of a place
We prefer a specific test example (for example_id = 7) To check how the model reaches its predictions. We will print the actual amount, foretold amount, and the feature values of this example. Look Full codes here.
# select a local instance to be explained
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]}")
Descriptions of multiple contact orders are made
We generate Shapley-based definitions with different contact order using ShapeQ package. Directly, we do not include:
- Order 1 (Normal Shapley Prices): Each feature contributions
- Order 2 (Text Collector): Combined Results of Tips for feature
- Order n (full partnership): All meeting until the number of features
# create explanations for different orders
feature_names = list(X.columns) # get the feature names
n_features = len(feature_names)
si_order: dict[int, shapiq.InteractionValues] = {}
for order in tqdm([1, 2, n_features]):
index = "k-SII" if order > 1 else "SV" # will also be set automatically by the explainer
explainer = shapiq.TreeExplainer(model=model, max_order=order, index=index)
si_order[order] = explainer.explain(x=x_explain)
si_order
1. Force Chart
Force Plot is a powerful tool to see the powerful sight that helps us to understand how model for a machine is to reach something. Displays the foundation for the foundation (ie, expected model value before seeing any features), and indicates that the “Press” feature is high or low.
In this structure:
- Red bars represent characteristics or interactions that promote predictions.
- Blue bars represents those who are decreasing.
- Each length of the bar is in line with its effect.
When using Shapley's connection, force Plot can imagine each individual's contributions but also collaboration between features. This makes it especially understandable when interpreting complex models, as it seems that the combinations of the features work together to influence the effect. Look Full codes here.
sv = si_order[1] # get the SV
si = si_order[2] # get the 2-SII
mi = si_order[n_features] # get the Moebius 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)
From the first conspiracy, we see that the basic amount is 23.5. Items such as weighing, cylinders, horses, and from getting of a positive influence, pushing us beyond the foundation. On the other hand, a model year and acceleration has a negative impact, drawing the prediction to the ground.
2. The waterfall chart
It is like the Force Plot, Waterfall Plot is another popular way to see Shapley Values, at the beginning of the Shape Library. It shows how different aspects stress the higher or low predictions compared to the basis. Another important benefit of the waterfall structure that the groups automatically has features that have the smallest impacts in the “Other” section, which makes it clean. Look 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 layer
Network PLOT indicates how the features interact with each other use the first and second order share. Node size shows the impact of each aspect, when the scalp and color shows the power to communicate and guide. It is especially useful when faced with many features, producing the sophisticated interaction of beautiful spels. Look Full codes here.
si.plot_network(feature_names=feature_names, show=True)
mi.plot_network(feature_names=feature_names, show=True)
4.
The SI graph structure expands network plot by seeing all the top-order partners as a hyper-edges connecting many features. Node size shows the impact of individuals, while width, color, and clarity shows the power and coordination of communication. It gives the perfect view of how factors contributes to the model to predict. Look Full codes here.
# we abbreviate the feature names since, they are plotted inside 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. Consolidation Bar
Bar Plot is made for worldwide descriptions. While some sites can be used both local and globally, Bar Plot summarizes the full value of the features (or characteristics) by showing total complete amounts (or partners) in all cases. In Shapiq, it highlights which work of interacting mostly donate. Look Full codes here.
explanations = []
explainer = shapiq.TreeExplainer(model=model, max_order=2, index="k-SII")
for instance_id in tqdm(range(20)):
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)
The “Grade” and “Horsepower” is the most influential factors, which means they have the most powerful influence on the prediction of the model. This is evident from their higher top prices to clean plastic prices.
Additionally, when viewing the second order meeting (ie, the two factors together), a combination of “a horse-weight” and “the conjunction of the 1.4 manners. This highlights the existence of non-line between characteristics.
Look Full codes here. Feel free to look our GITHUB page for tutorials, codes and letters of writing. Also, feel free to follow it Sane and don't forget to join ours 100K + ml subreddit Then sign up for Our newspaper.

I am the student of the community engineering (2022) from Jamia Millia Islamia, New Delhi, and I am very interested in data science, especially neural networks and their application at various locations.




