Getting LIME does not currently support classifier models without scores for LinearSVC
See original GitHub issue`from mosaicml import expai from sklearn.ensemble import RandomForestClassifier from sklearn import datasets from mosaicml import * from mosaicml.constants import MLModelFlavours import numpy as np from sklearn.svm import LinearSVC from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler
iris = datasets.load_iris() X = iris.data y = iris.target clf = make_pipeline(StandardScaler(), LinearSVC(random_state=0, tol=1e-5)) clf = clf.fit(X, y)
@scoring_func def score(model, request): payload = request.json[“payload”] data_list = payload data_array = np.asarray(data_list) try: prediction = model.predict(data_array) except: prediction = model.predict(data_array.reshape(1, -1)) return prediction.tolist()
import requests req = requests.Request() req.json = {“payload”:X[135]}
score(clf,req) import lime import lime.lime_tabular
import pandas as pd import numpy as np
explainer = lime.lime_tabular.LimeTabularExplainer(X,
mode=‘classification’,training_labels=y,feature_names=iris.feature_names)
i = 1 exp = explainer.explain_instance(X[0], clf.predict, num_features=4)`
Here’s my sample code for creating a LIME graph. Am I doing something wrong or is there a workaround for the same ? Thanks in advance for the help
Here’s the error I get NotImplementedError: LIME does not currently support classifier models without probability scores. If this conflicts with your use case, please let us know: https://github.com/datascienceinc/lime/issues/16
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
It doesn’t actually have to be a probability score, but you need the output of your model to have 2 dimensions. Even if you only output one number, you need the output to be of shape
(n, 1)
rather than(n,)
. You can achieve that with a simple reshape.Thank you very much for your answer. 😃
I have adapted the code accordingly, but the error message appeared again. This was not due to the .reshape, but is related to issue: https://github.com/marcotcr/lime/issues/428
The code works if it looks like this:
explanation = explainer.explain_instance(text_instance=text[1], classifier_fn=classifier_fn, labels=(0,))
‘’’ access explanation ‘’’
explanation_lime = explanation.as_list(label=0)
My problem is now solved, but I thought that this comment would be helpful for others facing this problem.