Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 62

Email Spam Detection

हम क्या बना रहे हैं

Spam detector एक machine learning program है जो message पढ़कर तय करता है वो spam है या नहीं। यह पहले से spam या safe labelled example messages से सीखता है। यह classic supervised classification project है, scikit-learn library से आसानी से बना।

💡 Steps: labelled messages लो → text को numbers में बदलो → model train करो → नए messages पर predict करो।

Spam detection कैसे काम करता है

Spam messages कुछ शब्द ज़्यादा इस्तेमाल करते हैं ("free", "win", "offer")। Model कई labelled examples देखता है, सीखता है कौन-से शब्द spam की ओर इशारा करते हैं, और उससे नए messages आँकता है। चूँकि यह labelled data से सीखता है, यह supervised learning है — ख़ासकर, दो classes में classification: spam या not spam।

Data

messages = [
    "Win a free prize now",       # spam
    "Meeting at 5 pm today",      # not spam
    "Claim your free reward",     # spam
    "Lunch tomorrow?"             # not spam
]
labels = [1, 0, 1, 0]   # 1 = spam, 0 = not spam

हमें messages और उनके labels चाहिए। यहाँ 1 यानी spam और 0 यानी not spam। असली projects हज़ारों examples इस्तेमाल करते हैं, पर विचार वही है।

Text को numbers में बदलना

from sklearn.feature_extraction.text import CountVectorizer

vec = CountVectorizer()
X = vec.fit_transform(messages)   # word counts numbers के रूप में

Models शब्द नहीं पढ़ सकते, तो CountVectorizer हर message को word counts की एक row में बदलता है। अब हर message numbers की list है जिससे model सीख सकता है।

Model train करना

from sklearn.naive_bayes import MultinomialNB

model = MultinomialNB()
model.fit(X, labels)   # examples से सीखो

MultinomialNB (Naive Bayes) text के लिए तेज़, भरोसेमंद model है। fit() इसे हमारे numbers और labels पर train करता है, तो यह सीखता है कौन-से शब्द spam का संकेत हैं।

Predictions करना

new = ["Free reward waiting for you"]
new_X = vec.transform(new)         # वही vectorizer
print(model.predict(new_X))        # [1] = spam
[1]

हम नए message को उसी vectorizer से transform करते हैं, फिर predict call करते हैं। 1 का result मतलब model को लगता है यह spam है। यह चलता हुआ spam detector है।

आम गलतियाँ

  • नए data के लिए अलग vectorizer इस्तेमाल करना। उसी fitted vectorizer के साथ transform इस्तेमाल कीजिए, नया नहीं।
  • बहुत कम data। कुछ examples अच्छा नहीं सीख सकते — असली models कई messages इस्तेमाल करते हैं।
  • Train और test न बाँटना। खुद को धोखा देने से बचने को अनदेखे data पर accuracy जाँचिए।
  • Text clean करना भूलना। Lowercasing और punctuation हटाना आमतौर पर results सुधारता है।

सारांश

  • Spam detection supervised classification है: spam या not spam।
  • Text को CountVectorizer (या TF-IDF) से numbers में बदलिए।
  • MultinomialNB model को fit() से train कीजिए।
  • नए messages को उसी vectorizer और predict() से predict कीजिए।
  • भरोसेमंद model को ढेर data और train/test split इस्तेमाल कीजिए।

आगे, ML type समझिए supervised learning में, या model quality जाँचिए overfitting और underfitting से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Machine learning spam कैसे पहचानता है?
यह पहले से "spam" या "not spam" labelled कई example messages से सीखता है। यह देखता है कौन-से शब्द spam में ज़्यादा आते हैं, फिर उस pattern से नए messages आँकता है। यह supervised classification समस्या है।
हम text को numbers में क्यों बदलते हैं?
Machine learning models numbers के साथ काम करते हैं, raw शब्दों से नहीं। हम हर message को CountVectorizer या TF-IDF से उसके शब्दों की गिनती (एक vector) में बदलते हैं, ताकि model उस पर गणित कर सके।
Spam detection के लिए कौन-सा algorithm अच्छा है?
Naive Bayes (MultinomialNB) spam जैसे text classification के लिए classic, तेज़ विकल्प है। यह word counts के साथ अच्छा चलता है और जल्दी train होता है, इसीलिए इस काम के लिए लोकप्रिय पहला model है।
Python में कौन-सी library इस्तेमाल करूँ?
scikit-learn (sklearn) vectorizer, model, और train/test tools सब एक साथ देता है। इसे pip install scikit-learn से install कीजिए।
कैसे पता चले spam model अच्छा है?
अपने data को training और test sets में बाँटिए, एक पर train करके दूसरे पर accuracy जाँचिए। अनदेखे test data पर अच्छी accuracy मतलब model सिर्फ़ रटने के बजाय generalise करता है।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicTic-Tac-Toe Game आपकी प्रगति: 62 / 66 (94%) · Lesson 63 / 66 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें