Skip to main content

shap_rs/plot/
decision.rs

1use crate::{Explanation, Result, ShapError};
2#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
3pub struct DecisionPath {
4    pub sample: usize,
5    pub feature_order: Vec<usize>,
6    pub cumulative_values: Vec<f64>,
7}
8pub fn data(e: &Explanation, output: usize) -> Result<Vec<DecisionPath>> {
9    if output >= e.n_outputs() {
10        return Err(ShapError::InvalidOutputIndex {
11            index: output,
12            n_outputs: e.n_outputs(),
13        });
14    }
15    let order = crate::plot::bar::data(e)
16        .into_iter()
17        .map(|x| x.0)
18        .collect::<Vec<_>>();
19    Ok((0..e.n_samples())
20        .map(|i| {
21            let mut cumulative = vec![e.base_values()[[i, output]]];
22            for &j in &order {
23                cumulative.push(cumulative.last().copied().unwrap() + e.values()[[i, j, output]])
24            }
25            DecisionPath {
26                sample: i,
27                feature_order: order.clone(),
28                cumulative_values: cumulative,
29            }
30        })
31        .collect())
32}