Bird
Raised Fist0
ML Pythonml~20 mins

Model interpretability (SHAP, LIME) in ML Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Model Interpretability Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding SHAP values
Which statement best describes what SHAP values represent in model interpretability?
ASHAP values show how much each feature contributes to pushing the model output from the average prediction to the current prediction.
BSHAP values are the raw input features used to train the model.
CSHAP values are the predicted probabilities of the model for each class.
DSHAP values are the weights of the neurons in a neural network.
Attempts:
2 left
💡 Hint
Think about how SHAP explains individual predictions by comparing to a baseline.
❓ Predict Output
intermediate
2:00remaining
Output of LIME explanation code
What will be the output of the following Python code snippet using LIME for a binary classification model?
ML Python
import numpy as np
from sklearn.linear_model import LogisticRegression
from lime.lime_tabular import LimeTabularExplainer

X_train = np.array([[0,1],[1,1],[0,0],[1,0]])
y_train = np.array([0,1,0,1])
model = LogisticRegression().fit(X_train, y_train)

explainer = LimeTabularExplainer(X_train, feature_names=['f1','f2'], class_names=['class0','class1'], discretize_continuous=False)
exp = explainer.explain_instance(np.array([1,0]), model.predict_proba, num_features=2)
print(exp.as_list())
A[('f2 <= 0.5', 0.5), ('f1 > 0.5', 0.3)]
B[('f1', 0.5), ('f2', -0.1)]
C[('f1', 0.3), ('f2', 0.2)]
D[('f1 = 1', 0.4), ('f2 = 0', 0.6)]
Attempts:
2 left
💡 Hint
LIME returns feature contributions as tuples with feature name and weight.
❓ Model Choice
advanced
1:30remaining
Choosing between SHAP and LIME
You want to explain predictions of a complex black-box model on tabular data with many features. Which method is best if you need consistent global and local explanations?
AUse LIME because it provides consistent global explanations for all features.
BUse LIME because it is faster and does not require retraining the model.
CUse SHAP because it provides consistent additive feature attributions for both local and global explanations.
DUse neither; use partial dependence plots instead.
Attempts:
2 left
💡 Hint
Consider which method has a solid theoretical foundation for consistent explanations.
❓ Hyperparameter
advanced
1:30remaining
Effect of number of samples in LIME
In LIME, increasing the 'num_samples' parameter during explanation will most likely:
ADecrease explanation accuracy and increase speed.
BCause the model to retrain on more data.
CHave no effect on explanation quality or speed.
DIncrease explanation accuracy but increase computation time.
Attempts:
2 left
💡 Hint
Think about how sampling affects the local surrogate model fit.
🔧 Debug
expert
2:00remaining
Debugging SHAP value computation error
You run SHAP's TreeExplainer on a scikit-learn RandomForestClassifier but get the error: 'ValueError: Model output type not supported'. What is the most likely cause?
AYou used TreeExplainer on a model that is not tree-based.
BYou passed a multi-output regression model instead of classification.
CYou passed raw model predictions instead of probabilities to SHAP.
DYou did not convert the input data to a numpy array.
Attempts:
2 left
💡 Hint
Check if the model type matches the explainer requirements.

Practice

(1/5)
1. What is the main purpose of using SHAP or LIME in machine learning?
easy
A. To improve the speed of model training
B. To reduce the number of features in the dataset
C. To increase the size of the training dataset
D. To explain why a model made a specific prediction

Solution

  1. Step 1: Understand model interpretability tools

    SHAP and LIME are designed to explain model predictions by showing feature importance.
  2. Step 2: Identify their main use

    They help users understand why a model made a certain prediction, increasing trust.
  3. Final Answer:

    To explain why a model made a specific prediction -> Option D
  4. Quick Check:

    Interpretability = Explanation [OK]
Hint: SHAP and LIME explain predictions, not speed or data size [OK]
Common Mistakes:
  • Confusing interpretability with model training speed
  • Thinking SHAP/LIME reduce dataset size
  • Assuming they automatically improve model accuracy
2. Which of the following is the correct way to import the SHAP library in Python?
easy
A. from shap import explain
B. import SHAP
C. import shap
D. import SHAP as sp

Solution

  1. Step 1: Recall Python import syntax

    Python imports are case-sensitive; the SHAP library is lowercase 'shap'.
  2. Step 2: Check each option

    import shap uses correct lowercase import. import SHAP uses uppercase which is invalid. from shap import explain tries to import a non-existent module 'explain'. import SHAP as sp uses uppercase which is invalid.
  3. Final Answer:

    import shap -> Option C
  4. Quick Check:

    Python imports are case-sensitive [OK]
Hint: Python imports match exact package name in lowercase [OK]
Common Mistakes:
  • Using uppercase letters in import statements
  • Importing non-existent submodules
  • Using uncommon aliases without explanation
3. Given the following Python code using LIME, what will exp.as_list() return?
import lime.lime_tabular
explainer = lime.lime_tabular.LimeTabularExplainer(training_data, feature_names=features)
exp = explainer.explain_instance(test_instance, model.predict_proba)
print(exp.as_list())
medium
A. A list of tuples showing feature names and their importance scores for the test instance
B. The predicted class label for the test instance
C. The training data used to build the explainer
D. A plot object visualizing the model's decision

Solution

  1. Step 1: Understand LIME explanation output

    The explain_instance method returns an explanation object. Calling as_list() gives feature importance as a list of (feature, weight) tuples.
  2. Step 2: Match output to options

    A list of tuples showing feature names and their importance scores for the test instance correctly describes this list. The predicted class label for the test instance is the predicted label, which as_list() does not return. The training data used to build the explainer is training data, unrelated here. A plot object visualizing the model's decision is a plot, which requires a different method.
  3. Final Answer:

    A list of tuples showing feature names and their importance scores for the test instance -> Option A
  4. Quick Check:

    LIME explanation list = feature importance [OK]
Hint: LIME's as_list() shows feature importance tuples [OK]
Common Mistakes:
  • Confusing explanation list with predicted label
  • Expecting training data from explanation
  • Thinking as_list() returns a plot
4. You run SHAP on a model but get an error: AttributeError: 'TreeExplainer' object has no attribute 'shap_values'. What is the likely cause?
medium
A. You used an outdated SHAP version missing the shap_values method
B. The model is not a tree-based model supported by TreeExplainer
C. You forgot to import the SHAP library
D. The input data is missing feature names

Solution

  1. Step 1: Understand TreeExplainer requirements

    TreeExplainer works only with tree-based models like XGBoost or LightGBM. Using it on unsupported models causes attribute errors.
  2. Step 2: Analyze error cause

    The error suggests the method shap_values is missing, likely because the model type is unsupported by TreeExplainer.
  3. Final Answer:

    The model is not a tree-based model supported by TreeExplainer -> Option B
  4. Quick Check:

    TreeExplainer needs tree models [OK]
Hint: TreeExplainer only supports tree models, else error occurs [OK]
Common Mistakes:
  • Assuming missing import causes this error
  • Blaming outdated SHAP version without checking model type
  • Ignoring model compatibility with explainer
5. You want to explain predictions of a complex neural network on tabular data. Which approach best combines SHAP and LIME to get both global and local interpretability?
hard
A. Use SHAP's DeepExplainer for global feature importance and LIME for local explanations on individual samples
B. Use LIME only, as SHAP does not support neural networks
C. Use SHAP's TreeExplainer for local explanations and LIME for global explanations
D. Use neither; neural networks cannot be interpreted

Solution

  1. Step 1: Identify SHAP and LIME strengths

    SHAP's DeepExplainer supports neural networks and provides global feature importance. LIME excels at explaining individual predictions locally.
  2. Step 2: Combine for best interpretability

    Using SHAP for global insights and LIME for local explanations gives a full understanding of model behavior.
  3. Final Answer:

    Use SHAP's DeepExplainer for global feature importance and LIME for local explanations on individual samples -> Option A
  4. Quick Check:

    SHAP global + LIME local = best combo [OK]
Hint: SHAP global + LIME local = full interpretability [OK]
Common Mistakes:
  • Thinking SHAP does not support neural networks
  • Confusing TreeExplainer with DeepExplainer
  • Believing neural networks cannot be interpreted
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard