Using non-standard models from libraries like kernlab with lime

I'm trying to use lime in order to explain text classification models in R (V4.0.5, Windows). With the logistic regression (library RWeka 0.4-43), I have no problems. The code

m_wlogistic <- Logistic(ID ~ ., data = training)
explainer <- lime(sentence_to_explain, model = as_classifier(m_wlogistic), preprocess = testdata_preprocessor)
explanation <- explain(sentence_to_explain, explainer, n_labels = 1, n_features = 2)

works. However, when I use a kernel SVM (library kernlab 0.9-29), I get a warning message for the lime command and an error for the explain command:

m_svm <- ksvm(ID ~ ., data = training, kernel = "rbfdot", C=rbf_C, kpar=list(sigma=rbf_sigma))
explainer <- lime(sentence_to_explain, model = as_classifier(m_svm), preprocess = testdata_preprocessor)
explanation <- explain(sentence_to_explain, explainer, n_labels = 1, n_features = 2)

The warning message is:

Warning message: In class(x) <- c("lime_classifier", class(x)) : Setting class(x) to multiple strings ("lime_classifier", "ksvm", ...); result will no longer be an S4 object

The error is as follows:

Error in UseMethod("predict") : 
 no applicable method for "predict" applied to an object of class "c('lime_classifier', 'ksvm')"

As I found out, there are methods to handle this with commands like "model_type()" and "predict_model()". However, unfortunately I didn't understand that approach and I didn't find a working example yet.

I have created a minimal runnable (respectively reproducing the error) example. Since it also needs pre-trained models and I cannot provide the training data, I have saved a test model as RDS and also created a text file as requested. The program, the data and the needed RDS files can be downloaded from the link below.

Working Example

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.