Addestrare il modello su feature TF-IDF
Adattare un classificatore in scikit-learn
Addestrare il modello su feature TF-IDF è una lezione NLP Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento NLP Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso NLP Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. ✅
Domande Frequenti
La lezione «Addestrare il modello su feature TF-IDF» è gratuita?
Sì — il testo completo di «Addestrare il modello su feature TF-IDF» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso NLP Academy, passa a CoddyKit PRO. Il corso NLP Academy include 4 lezioni in totale.
Cosa imparerò in «Addestrare il modello su feature TF-IDF»?
Adattare un classificatore in scikit-learn Eserciti NLP Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare NLP Academy?
Non è richiesta alcuna esperienza precedente. NLP Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Addestrare il modello su feature TF-IDF»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione NLP Academy?
Sì. Ogni lezione NLP Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Perché la regressione logistica funziona bene sul testo
- Addestrare il modello su feature TF-IDF
- Esaminare i coefficienti più rilevanti
- Regolare la forza della regolarizzazione