Ravindra BagaleCourses & study guides मराठी Track your progress

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

  1. Reuse the fitted vec and clf from 53.2.
  2. Zip feature names with coefficients.
  3. 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.