<p>Masker class provides a background data to &quot;train&quot; your explainer against. I.e., in:</p>
<pre><code>explainer = shap.LinearExplainer(model, masker = masker)
</code></pre>
<p>you're using background data determined by masker (you may see what data is used by accessing <code>masker.data</code> attribute). You may read more about &quot;true to model&quot; or &quot;true to data&quot; explanations <a href="https://arxiv.org/pdf/2006.16234.pdf" rel="nofollow noreferrer">here</a> or <a href="https://github.com/slundberg/shap/issues/1098" rel="nofollow noreferrer">here</a>.</p>
<p>Given above, caluclation-wise you may do both:</p>
<pre><code>masker = shap.maskers.Independent(data = X_train)
</code></pre>
<hr />
<p>or</p>
<hr />
<pre><code>masker = shap.maskers.Independent(data = X_test)
explainer = shap.LinearExplainer(model, masker = masker)
</code></pre>
<p>but conceptually, imo the following makes more sense:</p>
<pre><code>masker = shap.maskers.Independent(data = X_train)
explainer = shap.LinearExplainer(model, masker = masker)
</code></pre>
<p>This is akin usual <code>train/test</code> paradigm, where you train your model (and explainer) on train data, and try to predict (and explain) your test data.</p>
<hr />
<p>Unrelated to the question. An alternative to masker, which samples data for you, would be to explicitly provide background that may allow comparing 2 datapoints: a point against which compare, and the point of interest, like in <a href="https://github.com/slundberg/shap/blob/master/notebooks/tabular_examples/tree_based_models/Explaining%20a%20simple%20OR%20function.ipynb" rel="nofollow noreferrer">this</a> notebook. In such a manner one may find out why 2 seemingly similar datapoints were classified differently.</p>
What do maskers really do in SHAP package and fit them to train or test?
<p>I have been trying to work with the <code>shap</code> package. I want to determine the shap values from my logistic regression model. Contrary to the <code>TreeExplainer</code>, the <code>LinearExplainer</code> requires a so-called masker. What exactly does this masker do and what is the difference between the independent and partition maskers?</p>
<p>Also, I am interested in the important features from the test-set. Do I then fit the masker on the training set or the test set? Below you can see a snippet of code.</p>
<pre><code>model = LogisticRegression(random_state = 1)
model.fit(X_train, y_train)

masker = shap.maskers.Independent(data = X_train)
**or**
masker = shap.maskers.Independent(data = X_test)

explainer = shap.LinearExplainer(model, masker = masker)
shap_val = explainer(X_test)```

</code></pre>