CS229:GDA Naive Bayes
GDA Naive Bayes
introduction
All of the learning algorithm I've learned so far are called discriminative learning algorithms.
this time generative learning algorithm
data set with two classes -> gradient descent(logistic regression)
not try to find the separation
check one class at a time, model these classes in isolation, and find which model the test case matches better.
formalization
-
learn a mapping method from (feature x) directly to 0 or 1(label y). (discriminative)
-
learn a model given label y to describe its features x. (Generative)
- also learning the probability of a certain y without giving any features. (class prior)
-
using Bayes rule to describe the probability of a test case belonging to a certain label:
GDA(Gaussian Discriminant Analysis)
Assumptions
Assume all features' distribution are Gaussian.
- multivariate Gaussian distribution where \(\mu\) is the mean vector and \(\Sigma\) is a covariance matrix, symmetric and positive semi-definite.
-
where \(\mathrm{E}[X]=\int_{x} x p(x ; \mu, \Sigma) d x=\mu\) and \(\operatorname{Cov}(Z)=\mathrm{E}\left[Z Z^{T}\right]-(\mathrm{E}[Z])(\mathrm{E}[Z])^{T}=\Sigma\)
-
elements in \(\mu\) describes where the central point of is distribution is located, while the diagonal elements of the covariance matrix \(\Sigma\) indicates the direction of its major axis and non-diagonal elements "compress" the figure in different directions and degrees depending on its sign and value. (noting that the matrix is always symmetric)
Model
Writing out the distributions, this is:
(only one covariance matrix is applied, indicating that we are treating the two classes in the same way)
The log likelihood of the data is given by
by maximizing it, the parameters is found to be:
The decision boundary is given when a new test case has the same probability to be included in the two distributions.
To predict the probability of a new case belonging to a certain class, we use Bayes rules to calculate \(P(y\mid x)\), where \(P(x\mid y)\) can be known when given the Gaussian distribution, and \(P(x)\), \(P(y)\) simply by the proportion it has among all features or labels.
Compare GDA and logistic regression
If we view the quantity \(p\left(y=1 \mid x ; \phi, \mu_{0}, \mu_{1}, \Sigma\right)\) as a function of x, we’ll find that it can be expressed in the form
where \(\theta\) is some appropriate function of \(\phi, \Sigma, \mu_{0}, \mu_{1}\). This form is exactly the form of logistic regression.
Noting that from logistic regression, we can't infer the distribution of features to be Gaussian, indicating that GDA actually put forward a stronger assumption, which means if the assumptions are correct, GDA will act better than the weaker logistic regression, if wrong, using GDA is a disaster. (For example. if the data is Poisson, GDA might do poorly, while logistic regression still works)
Choosing a model depends on the amount of data set(the smaller, the more assumptions it should be given), and also the balance of model's universality and effectiveness. That's to say, the strength of the algorithm comes from data and knowledge given when developed. If we want it to deal with a certain work, abundant data will allow us to weaken our algorithm in exchange for robustness. When data is limited, GDA is easier to compute and no iterative process needed. and its performance is proved to be better regardless of the amount of training set.
Naive Bayes
In GDA, the feature vectors \(x\) were continuous, real-valued vectors. Lets now talk about a different learning algorithm in which the \(x_{i}\) ’s are discrete-valued.
For example, when building a classifier to filter spam emails, we encode the presence of a set of certain words into a binary vector called vocabulary, resulting in too much parameters to model.
So we make a strong assumption: the \(x_{i}\) ’s are conditionally independent given \(y\). This assumption is called the Naive Bayes (NB) assumption, and we now have:
We can write down the joint likelihood of the data, using MLE, and it estimates:
so we can simply make a prediction on a new test case by calculating
and pick the higher probability.
For more widely use:
-
if \(x\) can take values in \(\{1,2,3,\cdots,k\}\), just model \(p(x\mid y)\) as multinomial rather than Bernoulli.
-
if some original input attribute are continuous valued, it is quite common to discretize it—that is, turn it into a small set of discrete values—and apply Naive Bayes.
Laplace smoothing
When a new word appears in the vocabulary, the predicting formula will encounter a \(\dfrac{0}{0}\) problem, because the algorithm has not seen it before, the probability in either class is zero.
To avoid this, we can use Laplace smoothing, which replaces the above estimate with
and we therefore obtain the following estimates of the parameters:

浙公网安备 33010602011771号