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
On a tiny dataset, the coef_ values can be noisy and can change a lot with a few more rows. Use the top tokens to see the general pattern, but do not overclaim what they prove.
Ravindra Bagale's Tip – मराठी
छोट्या dataset वर coef_ values noisy असू शकतात आणि थोड्या जास्त rows आल्या तरी खूप बदलू शकतात. Top tokens वापरून general pattern बघा, पण ते काय सिद्ध करतात याबद्दल जास्त दावा करू नका.
Ravindra Bagale's Tip – हिंदी
छोटे dataset पर coef_ values noisy हो सकती हैं और थोड़ी ज़्यादा rows आने पर भी बहुत बदल सकती हैं. Top tokens से general pattern देखो, लेकिन वे क्या साबित करते हैं इस बारे में ज़्यादा दावा मत करो.