0Pricing
NLP Academy · Lezione

Creare un rilevatore di spam

Addestrare il modello su messaggi etichettati

Creare un rilevatore di spam è 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.

Your First Real Model

Time to build something useful: a spam filter. You will train a classifier on labeled messages and let it judge brand-new ones.

Start With Labeled Data

Every supervised model needs examples with answers. Here each message comes with a label of spam or ham, the friendly name for not-spam.

messages = ["win cash now", "lunch at noon?", "free prize click"]
labels = ["spam", "ham", "spam"]
print(len(messages), len(labels))

Turn Text Into Numbers

The model cannot read raw words, so you count them first. A CountVectorizer converts each message into a row of word counts.

from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer()
X = vec.fit_transform(messages)

Meet MultinomialNB

For word counts, the right tool is MultinomialNB, the count-based flavor of Naive Bayes built right into scikit-learn.

from sklearn.naive_bayes import MultinomialNB
model = MultinomialNB()

Fit the Model

Training is one line: hand the model your features and labels. The fit call counts words per class and stores the probabilities.

model.fit(X, labels)
print("trained on", X.shape[0], "messages")

Predict New Mail

To judge a fresh message, vectorize it the same way and call predict. The model returns its best guess for spam or ham.

new = vec.transform(["free cash prize"])
print(model.predict(new))

Reuse the Vectorizer

New text must use the same vocabulary the model learned. Always call transform, never fit again, or the columns will not line up.

Peek at the Confidence

Beyond the label, the model can report how sure it is. The predict_proba method gives a probability for each possible class.

print(model.predict_proba(new))

Hold Out a Test Set

Never grade a model on the data it trained on. Split off a test set so you can measure how it does on unseen messages.

Score the Filter

Run predictions on the held-out messages and compare to the truth. That accuracy tells you whether your spam filter actually works. 📬

Iterate to Improve

More clean data and better preprocessing lift results fast. Treat this filter as a baseline you can steadily refine, not a finished product.

Quick Check

How should you prepare a new message before predicting on it?

Recap

You vectorized messages, trained MultinomialNB, and predicted spam on new text. Reusing the fitted vectorizer keeps your features aligned. ✅

Domande Frequenti

La lezione «Creare un rilevatore di spam» è gratuita?

Sì — il testo completo di «Creare un rilevatore di spam» è 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 «Creare un rilevatore di spam»?

Addestrare il modello su messaggi etichettati 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 «Creare un rilevatore di spam»?

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

  1. L'intuizione alla base di Naive Bayes
  2. Creare un rilevatore di spam
  3. Modelli multinomiali e Bernoulli
  4. Leggere le previsioni del modello
← Torna a NLP Academy