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)