use ndarray::prelude::*; use tangram_zip::zip; pub struct ComputeShapValuesForExampleOutput { pub baseline_value: f32, pub output_value: f32, pub feature_contribution_values: Vec, } /// Compute the SHAP values for a single class for a single example. pub fn compute_shap_values_for_example( features: &[f32], bias: f32, weights: ArrayView1, means: &[f32], ) -> ComputeShapValuesForExampleOutput { let baseline_value = bias + zip!(weights, means) .map(|(weight, mean)| weight * mean) .sum::(); let feature_contributions: Vec = zip!(weights, features, means) .map(|(weight, feature, mean)| weight * (feature - mean)) .collect(); let output_value = baseline_value + feature_contributions.iter().sum::(); ComputeShapValuesForExampleOutput { baseline_value, output_value, feature_contribution_values: feature_contributions, } }