# Spam Detection with Data-Mining Techniques

> A spam-detection application based on Naive Bayes and n-gram features, implemented independently with Python, R and WEKA and evaluated with classification metrics.

- Author: Muhammet Ali Köker
- Language: en
- Canonical: https://alikoker.com.tr/en/spam-detection-with-data-mining-techniques
- Translation: https://alikoker.com.tr/spam-detector
- Published: 2021-01-09T13:03:59+03:00
- Modified: 2026-09-13T17:12:01+03:00
- Type: project

- Spam consists of unnecessary or inappropriate messages commonly sent to many recipients through electronic channels such as e-mail, telephone, fax, SMS, ICQ, MSN, WhatsApp and Telegram.
- An e-mail sent over [ARPANET](/en/arpanet-the-predecessor-of-the-internet) on 1 May 1978 is widely described as the first known spam message. It promoted products and services. Advertising and promotion still account for a substantial portion of spam.
- Spam messages commonly deliver identical content to many recipients and often have no value to them. Some contain unlawful or misleading material and create cybersecurity risks.
- Ham and Spam
    - Addresses used for spam can be collected from websites and documents by automated bots. The messages may not provide a valid or functional return address, making retrospective tracing more difficult.
    - In the United States, ham is a preserved pork product. SPAM, originally associated with “spiced ham,” became a widely recognized brand. The product remains commercially available.
    - The term spam was later adopted for unwanted and unnecessary messages. Ham became the contrasting term for legitimate messages.
- Naive Bayes Classifier
    - The method is named after the English mathematician Thomas Bayes and is based on Bayes’ theorem.
    - Despite its simple design and simplifying assumptions, it can produce results better than expected.
    - Bayes’ theorem relates conditional and marginal probabilities for a random variable.
    - P(A|B) = P(B|A) P(A) / P(B)
    - P(A): marginal probability of event A
    - P(B): marginal probability of event B
    - P(A|B): probability of A when B has occurred
    - P(B|A): probability of B when A has occurred
    - A classification problem contains multiple features and a target variable.
    - In the corresponding formulation, C represents the target variable and F represents the features.
    - The Naive Bayes classifier ultimately combines the relevant conditional probabilities.
- N-Gram Representation
    - An n-gram is a contiguous sequence of `n` characters, words, tokens, or other symbolic units. `n` is the sequence length; “gram” is not a weight.
    - 1-gram: unigram
    - 2-gram: bigram
    - 3-gram: trigram
    - n-gram: contiguous sequence of length `n`
    - In text classification, n-gram frequencies can be used directly as features. Unigrams capture individual token frequencies, while bigrams and trigrams retain part of local ordering.
    - A feature never observed in a Naive Bayes training class can otherwise create a zero factor, so Laplace/additive smoothing may be used.
    - n-grams have been widely used in text mining and in classical language models for speech-recognition systems.
    - The statistical framework connecting Naive Bayes, n-grams, and classification metrics is developed in [Statistical Learning and Machine Learning](/en/statistical-learning-machine-learning).
- Results
    - Accuracy: 0.9904, ACC = (TP + TN) / (P + N)
    - Sensitivity: 0.9917, TPR = TP / (TP + FN)
    - Specificity: 0.9815, SPC = TN / (FP + TN)
    - Precision: 0.9972, PPV = TP / (TP + FP)
    - Negative Predictive Value: 0.9465, NPV = TN / (TN + FN)
    - False-Positive Rate: 0.0185, FPR = FP / (FP + TN)
    - False-Discovery Rate: 0.0028, FDR = FP / (FP + TP)
    - False-Negative Rate: 0.0083, FNR = FN / (FN + TP)
    - F1 Score: 0.9945, F1 = 2TP / (2TP + FP + FN)
    - Matthews Correlation Coefficient: 0.9584

## Cite This Work

Köker, M. A. (2021). Spam Detection with Data-Mining Techniques. alikoker.com.tr. https://alikoker.com.tr/en/spam-detection-with-data-mining-techniques

- BibTeX: https://alikoker.com.tr/en/spam-detection-with-data-mining-techniques.bib
- RIS: https://alikoker.com.tr/en/spam-detection-with-data-mining-techniques.ris
- CSL-JSON: https://alikoker.com.tr/en/spam-detection-with-data-mining-techniques.csl.json
