# Ensembles of Random SHAPs
## 4 Modifications of SHAP
### Ensemble of Random SHAPs Generated by the Random Forest
The distribution \(P\) is used to select features from instance \(\mathbf{x}\) for constructing the vectors \(\mathbf{z}_{1},\ldots,\mathbf{z}_{N}\), namely \(t\) features are selected from \(\mathbf{x}\) with replacement \(N\) times in accordance with the distribution \(P\). SHAPs are used to find the Shapley values of vectors \(\mathbf{z}_{1},\ldots,\mathbf{z}_{N}\). They are combined similarly to ERW-SHAP by means of averaging as follows:  
\[\phi_{i}=\frac{1}{N_{i}}\sum_{k:i\in I_{k}}\phi_{i}^{(k)},\ i=1,\ldots,m. \tag{6}\]  
It is important that the number \(N_{i}\) of the \(i\)-th feature selections among all iterations \(N\) is used instead of \(N\). The whole algorithm can be divided into two stages which are separated in time. According to the first stage, neighbors \(\mathbf{h}_{1},\ldots,\mathbf{h}_{M}\) are generated for obtaining predictions \(y_{1}^{*},\ldots,y_{M}^{*}\) by the black-box model and for training the random forest which provides probabilities of features. This stage is depicted by dashed lines in Figure 3. The second stage is to use these probabilities for using SHAPs. This stage is depicted by solid lines in Figure 3.  
The random forest should be built with a large depth of trees and with a small number of trees in order to avoid a rather sparse probability distribution of features when a large part of probabilities will be equal to zero or close to zero. Another way for avoiding small probabilities of features is to apply calibration methods and to recalculate the obtained probabilities, for example, by using the temperature scaling as the simplest extension of Platt scaling [61]:  
\[p_{k}^{*}=\frac{\exp(p_{k}/T)}{\sum_{i=1}^{m}\exp(p_{i}/T)},\ k=1,\ldots,m, \tag{7}\]  
where \(T\) is the temperature which controls the smoothness of the probability distribution, but it does not change the relationship of probabilities \(p_{k}\), \(i=1,\ldots,m\). Algorithm 2 implementing ER-SHAP-RF can be viewed as an extension of ER-SHAP. Figure 3: A scheme of the ER-SHAP-RF. ```
0: Training set \(D\); point of interest \(\mathbf{x}\); the number of iterations \(N\); the number of selected features \(t\); the black-box model for explaining \(f(\mathbf{x})\); parameters of the random forest (the number and depth of trees, number of instances for building trees)
0: The Shapley values \(S=\{\phi_{1},\ldots\phi_{m}\}\)
1: Generate \(M\) instances \(\mathbf{h}_{1},\ldots,\mathbf{h}_{M}\) which are from the neighborhood of \(\mathbf{x}\) or from the whole training set
2: Compute the class label \(y_{j}^{*}=f(\mathbf{h}_{j})\) for every generated instance by using the black-box model
3: Train the random forest on \((\mathbf{h}_{j},y_{j}^{*})\), \(j=1,\ldots,M\)
4: Compute the probability distribution \(P\) of features by using the random forest
5:for\(k=1\), \(k\leq N\)do
6: Select randomly \(t\) features from \(\mathbf{x}\) in accordance with the probability distribution \(P\) and form the index set \(l_{k}\) of features
7: Use SHAP for computing \(\phi_{i}^{(k)}\), \(i\in l_{k}\) and form the set \(S_{k}=\{\phi_{i}^{(k)}:i\in l_{k}\}\)
8:endfor
9: Combine sets \(S_{k}\), \(k=1,\ldots,N\), to compute \(S\), for example, by using a simple averaging: \(\phi_{i}=N_{i}^{-1}\sum_{k:i\in l_{k}}\phi_{i}^{(k)}\), where \(N_{i}=\sum_{k:i\in l_{k}}1\). ```  
**Algorithm 2** ER-SHAP-RF  
It is interesting to point out that the fourth algorithm can also be proposed, which is represented as a combination of ERW-SHAP and ER-SHAP-RF. \(N\) points are generated for implementing diversity in accordance with ERW-SHAP, and \(M\) points are generated for training the random forest in accordance with ER-SHAP-RF and for computing the prior probability distribution \(P\) of features. Then, the random features are selected not from the vector \(\mathbf{x}\), as it is done in ER-SHAP-RF, but from every vector \(\mathbf{h}_{k}\) with the probability distribution \(P\), \(k=1,\ldots,N\). However, this algorithm is not studied because it can be regarded as the combination of ERW-SHAP and ER-SHAP-RF, which are analyzed in detail.