If an AI decision is a crime scene, LIME and SHAP are the forensic tools. By assigning 'Credit' to every feature, they reveal the hidden motives of the model.
1LIME: Local Fidelity
LIME (Local Interpretable Model-agnostic Explanations) assumes that even if a model is globally complex and non-linear, a small, local region of that model's decision space can be approximated with a simple Linear Model. It works by 'Perturbing' the data (making small, random changes to the features) and seeing how the prediction changes. It then builds a weighted linear regression around the point of interest, giving us a clear, local view of which features pushed the needle.
// LIME Local Approximation Concept
function explainWithLIME(model, dataPoint) {
const perturbedData = generatePerturbations(dataPoint);
const predictions = model.predict(perturbedData);
// Fit a simple linear model to the perturbed space
const explainer = new LinearRegression();
explainer.fit(perturbedData, predictions, {
weights: calculateProximity(perturbedData, dataPoint)
});
return explainer.getFeatureWeights();
}2SHAP: Fair Attribution
SHAP (SHapley Additive exPlanations) is based on Shapley Values from cooperative game theory. It treats each feature as a 'Player' in a game where the goal is to predict an outcome. SHAP calculates how much each feature contributes to the 'Payout' (the prediction) by testing all possible combinations of features. It is considered the Gold Standard of XAI because it is mathematically consistent—the sum of the SHAP values always equals the difference between the prediction and the average prediction.
// SHAP Additive Property Concept
function verifySHAPConsistency(shapValues, baseValue, prediction) {
let sumOfSHAP = 0;
for (let feature of Object.keys(shapValues)) {
sumOfSHAP += shapValues[feature];
}
// SHAP guarantees this will be true
return (baseValue + sumOfSHAP) === prediction;
}3Force Plots and Summary Maps
Both tools produce powerful visualizations. Force Plots show how individual features 'push' the prediction away from the baseline (red pushes up, blue pushes down). Summary Plots show the global importance of features by aggregating thousands of local SHAP values. These visualizations are essential for Model Debugging: if a model is using a feature it shouldn't (like a patient's name instead of their symptoms), LIME and SHAP will reveal it instantly.
// Force Plot Logic Concept
function renderForcePlot(shapValues, baseValue) {
let currentVal = baseValue;
shapValues.sort((a, b) => b.magnitude - a.magnitude);
for (let sv of shapValues) {
if (sv.val > 0) drawRedArrow(sv.feature, sv.val);
else drawBlueArrow(sv.feature, sv.val);
}
}