Naive Bayes spam filtering

Machine learning and
data mining

Problems

* Classification
* Clustering
* Regression
* Anomaly detection
* AutoML
* Association rules
* Reinforcement learning
* Structured prediction
* Feature engineering
* Feature learning
* Online learning
* Semi-supervised learning
* Unsupervised learning
* Learning to rank
* Grammar induction

Supervised learning
(classification • regression)

* Decision trees
* Ensembles
* Bagging
* Boosting
* Random forest
* k-NN
* Linear regression
* Naive Bayes
* Artificial neural networks
* Logistic regression
* Perceptron
* Relevance vector machine (RVM)
* Support vector machine (SVM)

Clustering

* BIRCH
* CURE
* Hierarchical
* k-means
* Expectation–maximization (EM)

DBSCAN
* OPTICS
* Mean-shift

Dimensionality reduction

* Factor analysis
* CCA
* ICA
* LDA
* NMF
* PCA
* t-SNE

Structured prediction

* Graphical models
* Bayes net
* Conditional random field
* Hidden Markov

Anomaly detection

* k-NN
* Local outlier factor

Artificial neural networks

* Autoencoder
* Deep learning
* DeepDream
* Multilayer perceptron
* RNN
* LSTM
* GRU
* Restricted Boltzmann machine
* GAN
* SOM
* Convolutional neural network
* U-Net

Reinforcement learning

* Q-learning
* SARSA
* Temporal difference (TD)

Theory

* Bias–variance dilemma
* Computational learning theory
* Empirical risk minimization
* Occam learning
* PAC learning
* Statistical learning
* VC theory

Machine-learning venues

* NeurIPS
* ICML
* ML
* JMLR
* ArXiv:cs.LG

Glossary of artificial intelligence

* Glossary of artificial intelligence

Related articles

* List of datasets for machine-learning research
* Outline of machine learning

* v
* t
* e

Naive Bayes classifiers are a popular statistical technique of e-mail filtering. They typically use bag-of-words features to identify email spam, an approach commonly used in text classification.

Naive Bayes classifiers work by correlating the use of tokens (typically words, or sometimes other things), with spam and non-spam e-mails and then using Bayes' theorem to calculate a probability that an email is or is not spam.

Naive Bayes spam filtering is a baseline technique for dealing with spam that can tailor itself to the email needs of individual users and give low false positive spam detection rates that are generally acceptable to users. It is one of the oldest ways of doing spam filtering, with roots in the 1990s.

## Contents

* 1 History
* 2 Process
* 3 Mathematical foundation
* 3.1 Computing the probability that a message containing a given word is spam
* 3.2 The spamliness of a word
* 3.3 Combining individual probabilities
* 3.4 Other expression of the formula for combining individual probabilities
* 3.5 Dealing with rare words
* 3.6 Other heuristics
* 3.7 Mixed methods
* 4 Discussion
* 4.1 Advantages
* 4.2 Disadvantages
* 5 General applications of Bayesian filtering
* 6 See also
* 7 References

## History

Bayesian algorithms were used for email filtering as early as 1996. Although naive Bayesian filters did not become popular until later, multiple programs were released in 1998 to address the growing problem of unwanted email.[1] The first scholarly publication on Bayesian spam filtering was by Sahami et al. in 1998.[2]

Variants of the basic technique have been implemented in a number of research works and commercial software products.[3] Many modern mail clients implement Bayesian spam filtering. Users can also install separate email filtering programs. Server-side email filters, such as DSPAM, SpamAssassin,[4] SpamBayes,[5] Bogofilter and ASSP, make use of Bayesian spam filtering techniques, and the functionality is sometimes embedded within mail server software itself. CRM114, oft cited as a Bayesian filter, is not intended to use a Bayes filter in production, but includes the ″unigram″ feature for reference.[6]

## Process

Particular words have particular probabilities of occurring in spam email and in legitimate email. For instance, most email users will frequently encounter the word "Viagra" in spam email, but will seldom see it in other email. The filter doesn't know these probabilities in advance, and must first be trained so it can build them up. To train the filter, the user must manually indicate whether a new email is spam or not. For all words in each training email, the filter will adjust the probabilities that each word will appear in spam or legitimate email in its database. For instance, Bayesian spam filters will typically have learned a very high spam probability for the words "Viagra" and "refinance", but a very low spam probability for words seen only in legitimate email, such as the names of friends and family members.

After training, the word probabilities (also known as likelihood functions) are used to compute the probability that an email with a particular set of words in it belongs to either category. Each word in the email contributes to the email's spam probability, or only the most interesting words. This contribution is called the posterior probability and is computed using Bayes' theorem. Then, the email's spam probability is computed over all words in the email, and if the total exceeds a certain threshold (say 95%), the filter will mark the email as a spam.

As in any other spam filtering technique, email marked as spam can then be automatically moved to a "Junk" email folder, or even deleted outright. Some software implement quarantine mechanisms that define a time frame during which the user is allowed to review the software's decision.

The initial training can usually be refined when wrong judgements from the software are identified (false positives or false negatives). That allows the software to dynamically adapt to the ever-evolving nature of spam.

Some spam filters combine the results of both Bayesian spam filtering and other heuristics (pre-defined rules about the contents, looking at the message's envelope, etc.), resulting in even higher filtering accuracy, sometimes at the cost of adaptiveness.

## Mathematical foundation

Bayesian email filters utilize Bayes' theorem. Bayes' theorem is used several times in the context of spam:

* a first time, to compute the probability that the message is spam, knowing that a given word appears in this message;
* a second time, to compute the probability that the message is spam, taking into consideration all of its words (or a relevant subset of them);
* sometimes a third time, to deal with rare words.

### Computing the probability that a message containing a given word is spam

Let's suppose the suspected message contains the word "replica". Most people who are used to receiving e-mail know that this message is likely to be spam, more precisely a proposal to sell counterfeit copies of well-known brands of watches. The spam detection software, however, does not "know" such facts; all it can do is compute probabilities.

The formula used by the software to determine that, is derived from Bayes' theorem

Pr⁡(S|W)=Pr⁡(W|S)⋅Pr⁡(S)Pr⁡(W|S)⋅Pr⁡(S)+Pr⁡(W|H)⋅Pr⁡(H)

where:

* Pr⁡(S|W) is the probability that a message is a spam, knowing that the word "replica" is in it;
* Pr⁡(S) is the overall probability that any given message is spam;
* Pr⁡(W|S) is the probability that the word "replica" appears in spam messages;
* Pr⁡(H) is the overall probability that any given message is not spam (is "ham");
* Pr⁡(W|H) is the probability that the word "replica" appears in ham messages.

(For a full demonstration, see Bayes' theorem.)

### The spamliness of a word

Statistics[7] show that the current probability of any message being spam is 80%, at the very least:

Pr⁡(S)=0.8;Pr⁡(H)=0.2

The filters that use this hypothesis are said to be "not biased", meaning that they have no prejudice regarding the incoming email. This assumption permits simplifying the general formula to:

Pr⁡(S|W)=Pr⁡(W|S)Pr⁡(W|S)+Pr⁡(W|H)

This is functionally equivalent to asking, "what percentage of occurrences of the word "replica" appear in spam messages?"

This quantity is called "spamicity" (or "spaminess") of the word "replica", and can be computed. The number Pr⁡(W|S) used in this formula is approximated to the frequency of messages containing "replica" in the messages identified as spam during the learning phase. Similarly, Pr⁡(W|H) is approximated to the frequency of messages containing "replica" in the messages identified as ham during the learning phase. For these approximations to make sense, the set of learned messages needs to be big and representative enough. It is also advisable that the learned set of messages conforms to the 50% hypothesis about repartition between spam and ham, i.e. that the datasets of spam and ham are of same size.[8]

Of course, determining whether a message is spam or ham based only on the presence of the word "replica" is error-prone, which is why bayesian spam software tries to consider several words and combine their spamicities to determine a message's overall probability of being spam.

### Combining individual probabilities

Most bayesian spam filtering algorithms are based on formulas that are strictly valid (from a probabilistic standpoint) only if the words present in the message are independent events. This condition is not generally satisfied (for example, in natural languages like English the probability of finding an adjective is affected by the probability of having a noun), but it is a useful idealization, especially since the statistical correlations between individual words are usually not known. On this basis, one can derive the following formula from Bayes' theorem:

p=p1p2⋯pNp1p2⋯pN+(1−p1)(1−p2)⋯(1−pN)

where:

* p is the probability that the suspect message is spam;
* p1 is the probability p(W1|S) that the first word (for example "replica") appears, given that the message is spam;
* p2 is the probability p(W2|S) that the second word (for example "watches") appears, given that the message is spam;
* etc...

Spam filtering software based on this formula is sometimes referred to as a naive Bayes classifier, as "naive" refers to the strong independence assumptions between the features. The result p is typically compared to a given threshold to decide whether the message is spam or not. If p is lower than the threshold, the message is considered as likely ham, otherwise it is considered as likely spam.

### Other expression of the formula for combining individual probabilities

Usually p is not directly computed using the above formula due to floating-point underflow. Instead, p can be computed in the log domain by rewriting the original equation as follows:

1p−1=(1−p1)(1−p2)…(1−pN)p1p2…pN

Taking logs on both sides:

ln⁡(1p−1)=∑i=1N[ln⁡(1−pi)−ln⁡pi]

Let η=∑i=1N[ln⁡(1−pi)−ln⁡pi]. Therefore,

1p−1=eη

Hence the alternate formula for computing the combined probability:

p=11+eη

### Dealing with rare words

In the case a word has never been met during the learning phase, both the numerator and the denominator are equal to zero, both in the general formula and in the spamicity formula. The software can decide to discard such words for which there is no information available.

More generally, the words that were encountered only a few times during the learning phase cause a problem, because it would be an error to trust blindly the information they provide. A simple solution is to simply avoid taking such unreliable words into account as well.

Applying again Bayes' theorem, and assuming the classification between spam and ham of the emails containing a given word ("replica") is a random variable with beta distribution, some programs decide to use a corrected probability:

Pr′⁡(S|W)=s⋅Pr⁡(S)+n⋅Pr⁡(S|W)s+n

where:

* Pr′⁡(S|W) is the corrected probability for the message to be spam, knowing that it contains a given word ;
* s is the strength we give to background information about incoming spam ;
* Pr⁡(S) is the probability of any incoming message to be spam ;
* n is the number of occurrences of this word during the learning phase ;
* Pr⁡(S|W) is the spamicity of this word.

(Demonstration:[9])

This corrected probability is used instead of the spamicity in the combining formula.

This formula can be extended to the case where n is equal to zero (and where the spamicity is not defined), and evaluates in this case to Pr(S).

### Other heuristics

"Neutral" words like "the", "a", "some", or "is" (in English), or their equivalents in other languages, can be ignored. These are also known as Stop words. More generally, some bayesian filtering filters simply ignore all the words which have a spamicity next to 0.5, as they contribute little to a good decision. The words taken into consideration are those whose spamicity is next to 0.0 (distinctive signs of legitimate messages), or next to 1.0 (distinctive signs of spam). A method can be for example to keep only those ten words, in the examined message, which have the greatest absolute value |0.5 − pI|.

Some software products take into account the fact that a given word appears several times in the examined message,[10] others don't.

Some software products use patterns (sequences of words) instead of isolated natural languages words.[11] For example, with a "context window" of four words, they compute the spamicity of "Viagra is good for", instead of computing the spamicities of "Viagra", "is", "good", and "for". This method gives more sensitivity to context and eliminates the Bayesian noise better, at the expense of a bigger database.

### Mixed methods

There are other ways of combining individual probabilities for different words than using the "naive" approach. These methods differ from it on the assumptions they make on the statistical properties of the input data. These different hypotheses result in radically different formulas for combining the individual probabilities.

For example, assuming the individual probabilities follow a chi-squared distribution with 2N degrees of freedom, one could use the formula:

p=C−1(−2ln⁡(p1p2⋯pN),2N)

where C−1 is the inverse of the chi-squared function.

Individual probabilities can be combined with the techniques of the Markovian discrimination too.

## Discussion

### Advantages

This section's factual accuracy is disputed. Please help to ensure that disputed statements are reliably sourced. (May 2013) (Learn how and when to remove this template message)

|

The spam that a user receives is often related to the online user's activities. For example, a user may have been subscribed to an online newsletter that the user considers to be spam. This online newsletter is likely to contain words that are common to all newsletters, such as the name of the newsletter and its originating email address. A Bayesian spam filter will eventually assign a higher probability based on the user's specific patterns.

The legitimate e-mails a user receives will tend to be different. For example, in a corporate environment, the company name and the names of clients or customers will be mentioned often. The filter will assign a lower spam probability to emails containing those names.

The word probabilities are unique to each user and can evolve over time with corrective training whenever the filter incorrectly classifies an email. As a result, Bayesian spam filtering accuracy after training is often superior to pre-defined rules.

### Disadvantages

Depending on the implementation, Bayesian spam filtering may be susceptible to Bayesian poisoning, a technique used by spammers in an attempt to degrade the effectiveness of spam filters that rely on Bayesian filtering. A spammer practicing Bayesian poisoning will send out emails with large amounts of legitimate text (gathered from legitimate news or literary sources). Spammer tactics include insertion of random innocuous words that are not normally associated with spam, thereby decreasing the email's spam score, making it more likely to slip past a Bayesian spam filter. However, with (for example) Paul Graham's scheme only the most significant probabilities are used, so that padding the text out with non-spam-related words does not affect the detection probability significantly.

Words that normally appear in large quantities in spam may also be transformed by spammers. For example, «Viagra» would be replaced with «Viaagra» or «V!agra» in the spam message. The recipient of the message can still read the changed words, but each of these words is met more rarely by the Bayesian filter, which hinders its learning process. As a general rule, this spamming technique does not work very well, because the derived words end up recognized by the filter just like the normal ones.[12]

Another technique used to try to defeat Bayesian spam filters is to replace text with pictures, either directly included or linked. The whole text of the message, or some part of it, is replaced with a picture where the same text is "drawn". The spam filter is usually unable to analyze this picture, which would contain the sensitive words like «Viagra». However, since many mail clients disable the display of linked pictures for security reasons, the spammer sending links to distant pictures might reach fewer targets. Also, a picture's size in bytes is bigger than the equivalent text's size, so the spammer needs more bandwidth to send messages directly including pictures. Some filters are more inclined to decide that a message is spam if it has mostly graphical contents. A solution used by Google in its Gmail email system is to perform an OCR (Optical Character Recognition) on every mid to large size image, analyzing the text inside.[13][14]

## General applications of Bayesian filtering

While Bayesian filtering is used widely to identify spam email, the technique can classify (or "cluster") almost any sort of data. It has uses in science, medicine, and engineering. One example is a general purpose classification program called AutoClass which was originally used to classify stars according to spectral characteristics that were otherwise too subtle to notice.

[15]

## See also

* Anti-spam techniques
* Bayesian poisoning
* Email filtering
* Markovian discrimination
* Mozilla Thunderbird mail client with native implementation of Bayes filters[16][17]

## References

1. ↑ Brunton, Finn (2013). Spam: A Shadow History of the Internet. MIT Press. p. 136. ISBN 9780262018876. https://books.google.com/books?id=QF7EjCRg5CIC&pg=PA136. Retrieved 2017-09-13.
2. ↑ "A Bayesian approach to filtering junk e-mail". AAAI'98 Workshop on Learning for Text Categorization. 1998. http://robotics.stanford.edu/users/sahami/papers-dir/spam.pdf.
3. ↑ "Junk Mail Controls". MozillaZine. November 2009. http://kb.mozillazine.org/Junk_Mail_Controls.
4. ↑ "Installation". Ubuntu manuals. 2010-09-18. http://manpages.ubuntu.com/manpages/gutsy/man1/sa-learn.1p.html. "Gary Robinson’s f(x) and combining algorithms, as used in SpamAssassin"
5. ↑ "Background Reading". SpamBayes project. 2010-09-18. http://spambayes.sourceforge.net/background.html. "Sharpen your pencils, this is the mathematical background (such as it is).* The paper that started the ball rolling: Paul Graham's A Plan for Spam.* Gary Robinson has an interesting essay suggesting some improvements to Graham's original approach.* Gary Robinson's Linux Journal article discussed using the chi squared distribution."
6. ↑ "Archived copy". http://crm114.sourceforge.net/docs/classify_details.txt.
7. ↑ "State of Spam, a Monthly Report - Report #33". 2009. http://eval.symantec.com/mktginfo/enterprise/other_resources/b-state_of_spam_report_09-2009.en-us.pdf.
8. ↑ Process Software, Introduction to Bayesian Filtering
9. ↑ Gary Robinson (2003). "A statistical approach to the spam problem". Linux Journal. http://www.linuxjournal.com/article/6467. Retrieved 2007-07-19.
10. ↑ Brian Burton (2003). "SpamProbe - Bayesian Spam Filtering Tweaks". http://spamprobe.sourceforge.net/paper.html.
11. ↑ Jonathan A. Zdziarski (2004). "Bayesian Noise Reduction: Contextual Symmetry Logic Utilizing Pattern Consistency Analysis". http://bnr.nuclearelephant.com/l. [yes|permanent dead link|dead link}}]
12. ↑ Paul Graham (2002), A Plan for Spam
13. ↑ "Gmail uses Google's innovative technology to keep spam out of your inbox". http://www.google.com/mail/help/intl/en_GB/fightspam/spamexplained.html.
14. ↑ Zhu, Z.; Jia, Z; Xiao, H; Zhang, G; Liang, H.; Wang, P. (2014). Li, S; Jin, Q; Jiang, X et al.. eds. "A Modified Minimum Risk Bayes and It's Application in Spam" (in en). Lecture Notes in Electrical Engineering (Dordrecht: Springer) 269: 2155–2159. doi:10.1007/978-94-007-7618-0_261.
15. ↑ Androutsopoulos, Ion; Paliouras, Georgios; Karkaletsis, Vangelis; Sakkis, Georgios; Spyropoulos, Constantine D.; Stamatopoulos, Panagiotis (2000). Gallinari, P; Rajman, M; Zaragoza, H. eds. "Learning to Filter Spam E-Mail: A Comparison of a Naive Bayesian and a Memory-Based Approach". 4th European Conference on Principles and Practice of Knowledge Discovery in Databases (PKDD-2000) (Lyon, France: Software and Knowledge Engineering Laboratory Institute of Informatics and Telecommunications National Centre for Scientific Research “Demokritos”): 1–13. Bibcode: 2000cs........9009A.
16. ↑ Hristea, Florentina T. (2013) (in en). The Naïve Bayes Model for Unsupervised Word Sense Disambiguation. London; Berlin: Springer- Verlag Heidelberg Berlin. pp. 70. ISBN 978-3-642-33692-8.
17. ↑ Zheng, J.; Tang, Yongchuan (2005). "One Generalization of the Naive Bayes to Fuzzy Sets and the Design of the Fuzzy Naive Bayes Classifier". in Mira, Jose; Álvarez, Jose R (in en). Artificial Intelligence and Knowledge Engineering Applications: A Bioinspired Approach. Lecture Notes in Computer Science. 3562. Berlin: Springer, Berlin, Heidelberg. p. 281. doi:10.1007/11499305_29. ISBN 978-3-540-26319-7.

* v
* t
* e

Unsolicited digital communication

Protocols|

| Email spam|

* Address munging
* Bulk email software
* Directory Harvest Attack
* Joe job
* DNSBL
* DNSWL
* Spambot
* Pink contract

|

Other|

* Auto dialer/Robocall
* Cold calling
* Flyposting
* Junk fax
* Messaging
* Mobile phone
* Newsgroup
* Telemarketing
* VoIP

Anti-spam|

* Disposable email address
* Email authentication
* SORBS
* SpamCop
* Spamhaus
* List poisoning
* Naive Bayes spam filtering
* Network Abuse Clearinghouse
* Distributed Checksum Clearinghouse

Spamdexing|

* Keyword stuffing
* Google bomb
* Scraper site
* Link farm
* Cloaking
* Doorway page
* URL redirection
* Spam blogs
* Sping
* Forum spam
* Blog spam
* Social spam
* Referrer spam
* Parasite hosting

Internet fraud|

* Advance-fee fraud
* Lottery scam
* Make Money Fast
* Phishing
* Vishing

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Naive Bayes spam filtering. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Naive_Bayes_spam_filtering&oldid=211540"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template