Skip to content

Instantly share code, notes, and snippets.

@prrao87
Last active September 2, 2019 01:16
Show Gist options
  • Select an option

  • Save prrao87/205edcca5bac6966d1d1341c2f802dc9 to your computer and use it in GitHub Desktop.

Select an option

Save prrao87/205edcca5bac6966d1d1341c2f802dc9 to your computer and use it in GitHub Desktop.
def explainer(method: str, path_to_file: str, text: str, num_samples: int) -> LimeTextExplainer:
"""Run LIME explainer on provided classifier"""
model = explainer_class(method, path_to_file)
predictor = model.predict
# Create a LimeTextExplainer
explainer = LimeTextExplainer(
# Specify split option for string
split_expression=lambda x: x.split(),
# Our classifer uses bigrms, trigrams or contextual ordering to classify text
# Hence, order matters, and we cannot use bag of words.
bow=False,
# Specify class names for this case
class_names=[1, 2, 3, 4, 5]
)
# Make a prediction and explain it:
exp = explainer.explain_instance(
text,
classifier_fn=predictor,
top_labels=1,
num_features=20,
num_samples=num_samples,
)
return exp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment