Sentiment Analysis by SHAP with Logistic Regression
Goal¶
This post aims to introduce how to do sentiment analysis using SHAP
with logistic regression.
Reference
This post aims to introduce how to do sentiment analysis using SHAP
with logistic regression.
Reference
import sklearn
from sklearn.model_selection import train_test_split
import numpy as np
import shap
import time
shap.initjs()
X_train, X_test, Y_train, Y_test = train_test_split(
*shap.datasets.iris(), test_size=0.2, random_state=0)
# Predictor
X_train.head()
# Label
Y_train[:5]
clf = sklearn.neighbors.KNeighborsClassifier()
clf.fit(X_train, Y_train)
explainer = shap.KernelExplainer(clf.predict_proba, X_train)
X_train_summary = shap.kmeans(X_train, 50)
explainer = shap.KernelExplainer(clf.predict_proba, X_train_summary)
shap_values = explainer.shap_values(X_test.iloc[0, :])
shap.force_plot(explainer.expected_value[0], shap_values[0], X_test.iloc[0, :])