0Pricing
NLP Academy · Lesson

Training on TF-IDF Features

Fit a classifier in scikit-learn.

Training on TF-IDF Features is a free NLP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Text to Numbers First

A model cannot read raw sentences. You first convert documents into TF-IDF vectors, then train logistic regression on those numbers. 🔢

Fit the Vectorizer

The TfidfVectorizer learns your vocabulary and weighting from the training texts. One call turns a list of strings into a feature matrix.

from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer()
X = vec.fit_transform(train_texts)

Labels Stay Separate

Your features X come from the text, but your labels y come from you. Each document needs its correct class, like spam or not-spam.

Split Before You Train

Hold out a test set so you can judge fairly. train_test_split keeps some data unseen until the very end of evaluation.

from sklearn.model_selection import train_test_split
X_tr, X_te, y_tr, y_te = train_test_split(X, y)

Fit Means Learn

Calling fit tells logistic regression to find the word weights that best separate your classes on the training data.

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(max_iter=1000)
clf.fit(X_tr, y_tr)

Raise max_iter If It Warns

Text has many features, so the solver may need more steps. Bumping max_iter clears the common convergence warning you will see.

Predict on New Vectors

To classify fresh text, transform it with the same fitted vectorizer, then call predict. Never refit the vectorizer on test data.

preds = clf.predict(X_te)

Transform, Do Not Fit, on Test

Test text uses transform, not fit_transform. Refitting would leak test information and quietly inflate your scores.

Check the Accuracy

A quick score call gives accuracy on the held-out set. It is your first signal that training actually worked.

print(clf.score(X_te, y_te))

Same Steps Stay in Sync

Train and prediction must share the exact same vocabulary. Using one fitted vectorizer everywhere keeps feature columns aligned.

A Pipeline Saves You

Wrapping the vectorizer and model in a Pipeline chains transform and predict automatically, so you never forget a step.

from sklearn.pipeline import make_pipeline
pipe = make_pipeline(TfidfVectorizer(), LogisticRegression())

Quick Check

How should you prepare test text before predicting?

Recap: Vectorize Then Fit

Vectorize text with TF-IDF, split, then fit logistic regression. Reuse the same vectorizer for test data, ideally inside a pipeline. ✅

Frequently asked questions

Is the “Training on TF-IDF Features” lesson free?

Yes — the full text of “Training on TF-IDF Features” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Training on TF-IDF Features”?

Fit a classifier in scikit-learn. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start NLP Academy?

No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Training on TF-IDF Features” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this NLP Academy lesson?

Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Logistic Regression Wins on Text
  2. Training on TF-IDF Features
  3. Inspecting the Strongest Coefficients
  4. Tuning Regularization Strength
← Back to NLP Academy