53. Sentiment Analysis Project in Python
53.3 Inspect Top Tokens
Why. Managers ask "why negative?" Inspecting high-weight tokens builds trust.
What. After fit, LogisticRegression coef_ pairs with vectoriser feature names. Positive coefs lean positive class; negative coefs lean negative (for binary with classes 0/1).
How.
Peek at top tokens
- Reuse the fitted
vecandclffrom 53.2. - Zip feature names with coefficients.
- Sort and print a few strongest tokens each side.
import numpy as np
# assumes vec, clf from previous cell
feats = np.array(vec.get_feature_names_out())
coefs = clf.coef_[0]
order = np.argsort(coefs)
print("lean negative:", feats[order[:5]])
print("lean positive:", feats[order[-5:]])
Ravindra Bagale's Tip
Tiny data var coef_ noisy asel — pattern bagha, overclaim nako.