File size: 1,180 Bytes
e7fc8a4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import lime
import lime.lime_text
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
import numpy as np
def explain_prediction(text):
"""
Explains the prediction of a text classification model using LIME.
"""
# Create a dummy dataset
data = ["I love this product", "This is a terrible movie", "The book was okay", "I hate this"]
labels = [1, 0, 0, 0]
# Create a simple text classification pipeline
vectorizer = TfidfVectorizer()
classifier = LogisticRegression()
pipeline = make_pipeline(vectorizer, classifier)
pipeline.fit(data, labels)
# Create a LIME explainer
explainer = lime.lime_text.LimeTextExplainer(class_names=['negative', 'positive'])
# Explain the prediction for the given text
explanation = explainer.explain_instance(text, pipeline.predict_proba, num_features=6)
return explanation.as_list()
if __name__ == '__main__':
text_to_explain = "This is a fantastic and innovative AI model."
explanation = explain_prediction(text_to_explain)
print("LIME explanation:", explanation)
|