Skip to main content
. 2022 Apr 5;8:e880. doi: 10.7717/peerj-cs.880

Algorithm 1. TreeShap.

1: function TS(x, tree)
2:   ϕ = array of len(x) zeroes
3:   function RECURSE(j, m, pz, po, pi)
4:    m = EXTEND(m, pz, po, pi)
5:    if vj == leaf then
6:     for i2 to len(m) do
7:      w = sum(UNWIND(m, i).w)
8:       ϕmi.d=ϕmi.d+w(mi.omi.z)vj
9:    else
10:      h,c=xdjtj?(aj,bj):(bj,aj)
11:     iz = io = 1
12:     k = FINDFIRST(m.d, dj)
13:     if knothing then
14:      iz, io = (mk.z, mk.o)
15:      m = UNWIND(m, k)
16:    RECURSE(h, m, izrh/rj, io, dj)
17:    RECURSE(c, m, izrc/rj, 0, dj)
18:   function EXTEND(m, pz, po, pi)
19:    l = len(m)
20:    m = copy(m) {m is copied so recursions down other branches are not affected.}
21:    ml+1.(d, z, o, w) = (pi, pz, po, l = 0 ? 1 : 0)
22:    for il to 1 do
23:     mi+1.w = mi+1.w + pomi.wi/(l + 1)
24:     mi.w = pzmi.w ⋅ (l + 1 − i)/(l + 1)
25:    return m
26:   function UNWIND(m, i)
27:    l = len(m)
28:    n = ml.w
29:    m = copy(m1⋯l−1)
30:    for jl1 to 1 do
31:     if mi.o ≠ 0 then
32:      t = mj.w
33:      mj.w = nl/(jmi.o)
34:      n = tmj.wmi.z ⋅ (lj)/l
35:    else
36:     mj.w = (mj.wl)/(mi.z ⋅ (lj))
37:    for ji to l − 1 do
38:     mj.(d, z, o) = mj+1.(d, z, o)
39:    return m
40:   RECURSE(1, [], 1, 1, 0)
41:   return ϕ