SHAP plots taking ages with OneClassSVM

Viewed 62

I'm attempting to interpret my OneClassSVM model, but the computation time is very high. I have used cross-validation with 36 folds, so want to combine the results of all the folds on one SHAP plot so that I can fully interpret what features contribute most to the model.

So far I thought that taking a sample of the data that I want to be interpreted would speed things up (it did reduce the time), but it still will take around 8hours for one fold and there are 36 folds.

Note that my train set is ~2400 and my test set is ~1400, each with 88 features.

import shap
from sklearn.svm import OneClassSVM
import numpy as np

# These are 2d arrays, where each element is a DataFrame of the selected data for train/test for a fold
shap_train = np.load('shap_train.npy', allow_pickle=True)
shap_test = np.load('shap_test.npy', allow_pickle=True)

clf = OneClassSVM(nu=0.35)

folds = len(shap_train)
shap_values = []
shap_data_test = []

for fold in range(folds):
        explainer = shap.Explainer(clf.fit_predict, shap_train[fold])
        # Sampling 1/3 of the data
        data = shap_test[fold].sample(frac=(1/3))
        shap_values.append(explainer(data))
        shap_data_test.append(data)

# Storing SHAP values for plots later
np.save('shap_data.npy', np.array(shap_values))
np.save('shap_data_test.npy', np.array(shap_data_test))

I've questioned my methodology of needing to produce shap values for all the folds, but I know that some folds perform better than others, so want an overall view of what features are contributing most.

I deployed this script on a Debian server with a Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz and 64GB RAM.

1 Answers

After further reading and advice, I found that there's no real way to speed up this computation. Using a large sample will always take a long time.

Instead as @sergey-bushmanov mentioned, just using a small sample will work well enough. In my case, I used a sample of 200 datapoints per fold and this still took some time but a lot less then previously, whilst also still displaying the contributions of each feature.

Related