Reproducible error: SHAP ExplainerError: Additivity check failed in TreeExplainer
Nobody has claimed this yet.
- Dominant language
- Jupyter Notebook
- Stars
- 4.8k
- Forks
- 827
- PR merge metrics
- No merged PRs in 30d
Description
This is related to issue #866
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
# Create a sample DataFrame X
np.random.seed(0) # for reproducibility
data = {
'Category': np.random.randint(2, size=100),
'Value': np.random.randint(100, size=100)
}
X = pd.DataFrame(data)
# Creating binary arrays T and y
T = np.random.randint(2, size=100)
y = np.random.randint(2, size=100)
# Splitting the data into training and testing sets
X_train, X_test, T_train, T_test, y_train, y_test = train_test_split(X, T, y, test_size=0.2, random_state=42)
to_scale = ["Value"]
scaler = MinMaxScaler()
# Fit scaler on training data and transform both training and testing data
X_train[to_scale] = scaler.fit_transform(X_train[to_scale])
X_test[to_scale] = scaler.transform(X_test[to_scale])
est = CausalForestDML(model_y=RandomForestClassifier(), model_t=DummyClassifier(strategy='uniform'), random_state=123, discrete_outcome=True,discrete_treatment=True)
est.fit(y_train, T_train, X=X_train)
shap_values = est.shap_values(X_train[:20])
shap.plots.beeswarm(shap_values['Y0']['T0_1'])
If you try to comment out or remove the lines:
to_scale = ["Value"]
scaler = MinMaxScaler()
# Fit scaler on training data and transform both training and testing data
X_train[to_scale] = scaler.fit_transform(X_train[to_scale])
X_test[to_scale] = scaler.transform(X_test[to_scale])
the code runs correctly.
It seems that the scaler induces an issue, it is the same with StandardScaler(). My guess is that float rounding errors are responsible for this behavior. I believe that there is a PR to add check_additivity = False, which seems to be the only way to resolve this, unless I am missing something.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied CausalForestDML and TreeExplainer reproducer with and without MinMaxScaler or StandardScaler, then compare the resulting additivity-check failure. Review the related issue #866 and the referenced check_additivity change; done means the scaled example behaves correctly or the limitation and required workaround are clearly established with regression coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- pandas, python, scikit-learn
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100