Simplicity of naive Bayesian algorithm
http://blog.csdn.net/xlinsist/article/details/51236454
Introduction
Previously read an article, a foreigner programmer wrote some very bull shell script, including the late work automatically send a message to his wife, automatic Chong Coffee Ah, automatically scan a DBA sent mail ah, and so on. So I also want to use what I have learned to do something interesting. My idea is as follows:
First I write a scrapy script to crawl a joke on a website.
And then write a shell script that automatically grabs the latest jokes every morning at 6.
Then use the naive Bayesian model to determine whether the current joke is an adult joke.
If it's an adult joke, use a script to send it to your brother's mailbox automatically.
If it's not an adult joke, use a script to automatically send it to your girlfriend's mailbox.
In this series of articles, you will learn:
The mathematical principles behind naive Bayes
Algorithm-specific work processes (combined with examples)
Scikit-learn Fast Implementation algorithm
This series of articles will also involve some knowledge of scrapy,pandas,numpy, I will explain in detail when encountering this knowledge. This series of articles I will be the main detailed introduction of the 3rd Step implementation process, the rest I will be the source on GitHub for everyone to reference. Below, let's enjoy this wonderful journey.
Naive Bayesian overview
As we all know, Naive Bayes is a simple but very powerful linear classifier. It has been a great success in the classification of Spam and the diagnosis of diseases. It is only so called simplicity, because it assumes that features are independent of each other, but in real life, this hypothesis is basically not tenable. It still behaves well even under hypothetical conditions, especially in the case of small-scale samples. However, if there is a strong correlation between each feature and non-linear classification problems will lead to naïve Bayesian model has a poor classification effect.
The mathematical principles behind naive Bayes
Posteriori probability (posterior probabilities)
In order to better understand how naive Bayesian classifier works, it is necessary to understand the Bayesian law. It can be simply described as the following formula:
Posteriori probability = probability of a priori probability of conditional probability
We can explain the above formula with an example of whether God is raining or not.
P (probability of rain in a given sky) =p (probability of rain in the sky) p (probability of rain) p (probability of dark clouds)
We can see that if we want to predict the probability of a certain result under a given phenomenon, we must know: 1, given this result, the probability of this phenomenon. 2, the probability of such a result. 3. The probability of the occurrence of this phenomenon.
In practical applications, we cannot have only one phenomenon, for example, in the spam classification, there may even be thousands of word eigenvectors. In the following, I will introduce some mathematical notation to unify the following expression:
Ωj indicates which category to belong to, j∈{1,2,3,..., m}
Xi represents the I feature in a eigenvectors, i∈{1,2,3,..., n}
The objective of naive Bayes is to obtain the maximum probability of P (ωj| given phenomenon) j∈{1,2,3,..., m} respectively.
Below I will explain the above three probabilities separately.
Conditional probability (Conditional probabilities)
The independence of random variables means that I tell you that the occurrence of a variable does not affect the likelihood that you believe another occurrence. One of the simplest examples is tossing a coin, that is, the first counter-positive does not affect the probability that you will throw up the counter-positive again (that is, 0.5).
In the naive Bayesian model, the characteristics are not only independent, but also conditional independent. For example: In my eigenvector x, there are n features, so I can write the probability in the following form:
P (x|ωj) =p (X1|ΩJ) p (x2|ωj) p (x3|ωj) p (xn|ωj) =∏K=1NP (XK|ΩJ)
The probability of P (X|ΩJ) can be understood as: the probability of occurrence x is observed under conditions that belong to a category. The probability of each feature in the eigenvector can be obtained by a maximum likelihood estimate (Maximum-likelihood estimate), which is simply the frequency of a particular feature in a category, as follows:
P (xi|ωj) =nxi|ωjnωj (i= (1,..., N))
Nxi|ωj: The number of feature XI occurrences in all training samples belonging to the class Ωj
Nωj: The number of occurrences of all features in all training samples belonging to the category Ωj
Now, I'm going to illustrate the concept with a simple example of classifying spam. For example, I have 4 emails, 2 of which are spam, and there are 4 features in the eigenvectors. Specifically, the following table:
Sample number lovebuydealcat is junk e-mail
11001 not
20110 Yes
30101 Yes
41011 not
Now I ask the probability of P (buy,deal|spam), through the independence of the condition and the maximum likelihood estimation, we can find this probability:
P (buy,deal|spam) =p (buy|spam) p (deal|spam) =1+11+1+1+111+1+1+1=18
Prior probability (Prior probabilities)
Priori probability is actually very simple, in the above example of the spam, we can use the maximum likelihood estimate to obtain: 24, the general formula is as follows:
P (ωj) =nωjnc
Nωj: Number of samples belonging to class Ωj
Nc: All sample numbers
Through the above-mentioned probability formula, we can know: if the prior probability is subjected to uniform distribution, then the posterior probability will depend entirely on the conditional probability and the probability of the phenomenon, but the probability of the phenomenon is constant, so the posterior probability is entirely dependent on the conditional probability.
Note: I think that in some classification applications, a priori probability should be consulted by experts in the field of application, you can not rely on the probability of a sample to obtain a priori probability. For example: if my training concentration, rainy days of the sample more than a sunny sample, you can not say that the real life of the probability of rainy days than when the weather is bigger. Therefore, for some applications, a priori probability should be consulted by experts in the field of application.
Phenomenon probability (Evidence probabilities)
The probability of the phenomenon is separate from the category. For example of the above spam, I would like to know the probability that the phenomenon of P (deal) is occurring, I just need to find out the probability of the occurrence of deal in all my samples, and it has nothing to do with which category belongs.
In fact, we do not have to work out this probability, because we want to belong to which category of the most probability, and in this process, it is the probability of each calculation category (that is, p (ωj|xi)) is a constant, so it does not affect the final decision.
Plus smoothing (Additive smoothing)
Or the example of the above message, assuming we ask P (love|spam), we will find that the word love is not in the spam message, so the probability is 0, if the probability of the condition is 0, then the whole posteriori probability is 0. To avoid the occurrence of 0 probability, we can add smoothing items. Change the formula for the above conditional probability to the following form:
P (xi|ωj) =nxi|ωj+αnωj+αn (i= (1,..., N))
Nxi|ωj: The number of feature XI occurrences in all training samples belonging to the class Ωj
Nωj: The number of occurrences of all features in all training samples belonging to the category Ωj
α: Additional smoothing item parameters. Α<1 is called Lidstone smoothing; Α=1 is called Laplace smoothing.
N: Number of features
A PHP version of Naive Bayes
http://blog.csdn.net/u012543061/article/details/53005029
Naive Bayesian algorithm data collation and PHP implementation version