# 2.1 Machine learning lecture 2 course notes  (Page 6/6)

 Page 6 / 6
$\begin{array}{ccc}\hfill {\Phi }_{35000|y=1}& =& \frac{{\sum }_{i=1}^{m}1\left\{{x}_{35000}^{\left(i\right)}=1\wedge {y}^{\left(i\right)}=1\right\}}{{\sum }_{i=1}^{m}1\left\{{y}^{\left(i\right)}=1\right\}}=0\hfill \\ \hfill {\Phi }_{35000|y=0}& =& \frac{{\sum }_{i=1}^{m}1\left\{{x}_{35000}^{\left(i\right)}=1\wedge {y}^{\left(i\right)}=0\right\}}{{\sum }_{i=1}^{m}1\left\{{y}^{\left(i\right)}=0\right\}}=0\hfill \end{array}$

I.e., because it has never seen “nips” before in either spam or non-spam training examples, it thinks the probability of seeing it ineither type of email is zero. Hence, when trying to decide if one of these messages containing “nips” is spam, it calculates theclass posterior probabilities, and obtains

$\begin{array}{ccc}\hfill p\left(y=1|x\right)& =& \frac{{\prod }_{i=1}^{n}p\left({x}_{i}|y=1\right)p\left(y=1\right)}{{\prod }_{i=1}^{n}p\left({x}_{i}|y=1\right)p\left(y=1\right)+{\prod }_{i=1}^{n}p\left({x}_{i}|y=0\right)p\left(y=0\right)}\hfill \\ & =& \frac{0}{0}.\hfill \end{array}$

This is because each of the terms “ ${\prod }_{i=1}^{n}p\left({x}_{i}|y\right)$ ” includes a term $p\left({x}_{35000}|y\right)=0$ that is multiplied into it. Hence, our algorithm obtains $0/0$ , and doesn't know how to make a prediction.

Stating the problem more broadly, it is statistically a bad idea to estimate the probability of some event to be zero just because you haven't seen it before in your finite training set. Take the problem of estimatingthe mean of a multinomial random variable $z$ taking values in $\left\{1,...,k\right\}$ . We can parameterize our multinomial with ${\Phi }_{i}=p\left(z=i\right)$ . Given a set of $m$ independent observations $\left\{{z}^{\left(1\right)},...,{z}^{\left(m\right)}\right\}$ , the maximum likelihood estimates are given by

${\Phi }_{j}=\frac{{\sum }_{i=1}^{m}1\left\{{z}^{\left(i\right)}=j\right\}}{m}.$

As we saw previously, if we were to use these maximum likelihood estimates, then some of the ${\Phi }_{j}$ 's might end up as zero, which was a problem.To avoid this, we can use Laplace smoothing , which replaces the above estimate with

${\Phi }_{j}=\frac{{\sum }_{i=1}^{m}1\left\{{z}^{\left(i\right)}=j\right\}+1}{m+k}.$

Here, we've added 1 to the numerator, and $k$ to the denominator. Note that ${\sum }_{j=1}^{k}{\Phi }_{j}=1$ still holds (check this yourself!), which is a desirable property since the ${\Phi }_{j}$ 's are estimates for probabilities that we know must sum to 1. Also, ${\Phi }_{j}\ne 0$ for all values of $j$ , solving our problem of probabilities being estimated as zero. Under certain (arguably quite strong) conditions, it can be shown that the Laplace smoothing actually gives the optimal estimator of the ${\Phi }_{j}$ 's.

Returning to our Naive Bayes classifier, with Laplace smoothing, we therefore obtain the following estimates of the parameters:

$\begin{array}{ccc}\hfill {\Phi }_{j|y=1}& =& \frac{{\sum }_{i=1}^{m}1\left\{{x}_{j}^{\left(i\right)}=1\wedge {y}^{\left(i\right)}=1\right\}+1}{{\sum }_{i=1}^{m}1\left\{{y}^{\left(i\right)}=1\right\}+2}\hfill \\ \hfill {\Phi }_{j|y=0}& =& \frac{{\sum }_{i=1}^{m}1\left\{{x}_{j}^{\left(i\right)}=1\wedge {y}^{\left(i\right)}=0\right\}+1}{{\sum }_{i=1}^{m}1\left\{{y}^{\left(i\right)}=0\right\}+2}\hfill \end{array}$

(In practice, it usually doesn't matter much whether we apply Laplace smoothing to ${\Phi }_{y}$ or not, since we will typically have a fair fraction each of spam and non-spam messages, so ${\Phi }_{y}$ will be a reasonable estimate of $p\left(y=1\right)$ and will be quite far from 0 anyway.)

## Event models for text classification

To close off our discussion of generative learning algorithms, let's talk about one more model that is specifically for text classification. While Naive Bayes as we've presentedit will work well for many classification problems, for text classification, there is a related model that does even better.

In the specific context of text classification, Naive Bayes as presented uses the what's called the multi-variate Bernoulli event model . In this model, we assumed that the way an email is generated is that first it is randomly determined (according to the classpriors $p\left(y\right)$ ) whether a spammer or non-spammer will send you your next message. Then, the person sending the email runs through the dictionary, deciding whether to include each word $i$ in that email independently and according to the probabilities $p\left({x}_{i}=1|y\right)={\Phi }_{i|y}$ . Thus, the probability of a message was given by $p\left(y\right){\prod }_{i=1}^{n}p\left({x}_{i}|y\right)$ .

Here's a different model, called the multinomial event model . To describe this model, we will use a different notation and set of features for representing emails. We let ${x}_{i}$ denote the identity of the $i$ -th word in the email. Thus, ${x}_{i}$ is now an integer taking values in $\left\{1,...,|V|\right\}$ , where $|V|$ is the size of our vocabulary (dictionary). An email of $n$ words is now represented by a vector $\left({x}_{1},{x}_{2},...,{x}_{n}\right)$ of length $n$ ; note that $n$ can vary for different documents. For instance, if an email starts with “A NIPS ...,” then ${x}_{1}=1$ (“a” is the first word in the dictionary), and ${x}_{2}=35000$ (if “nips” is the 35000th word in the dictionary).

In the multinomial event model, we assume that the way an email is generated is via a random process in which spam/non-spam is first determined (according to $p\left(y\right)$ ) as before. Then, the sender of the email writes the email by first generating ${x}_{1}$ from some multinomial distribution over words ( $p\left({x}_{1}|y\right)$ ). Next, the second word ${x}_{2}$ is chosen independently of ${x}_{1}$ but from the same multinomial distribution, and similarly for ${x}_{3}$ , ${x}_{4}$ , and so on, until all $n$ words of the email have been generated. Thus, the overall probability of a message is given by $p\left(y\right){\prod }_{i=1}^{n}p\left({x}_{i}|y\right)$ . Note that this formula looks like the one we had earlier for the probability of a message under themulti-variate Bernoulli event model, but that the terms in the formula now mean very different things. In particular ${x}_{i}|y$ is now a multinomial, rather than a Bernoulli distribution.

The parameters for our new model are ${\Phi }_{y}=p\left(y\right)$ as before, ${\Phi }_{k|y=1}=p\left({x}_{j}=k|y=1\right)$ (for any $j$ ) and ${\Phi }_{i|y=0}=p\left({x}_{j}=k|y=0\right)$ . Note that we have assumed that $p\left({x}_{j}|y\right)$ is the same for all values of $j$ (i.e., that the distribution according to which a word is generated does not depend on its position $j$ within the email).

If we are given a training set $\left\{\left({x}^{\left(i\right)},{y}^{\left(i\right)}\right);i=1,...,m\right\}$ where ${x}^{\left(i\right)}=\left({x}_{1}^{\left(i\right)},{x}_{2}^{\left(i\right)},...,{x}_{{n}_{i}}^{\left(i\right)}\right)$ (here, ${n}_{i}$ is the number of words in the $i$ -training example), the likelihood of the data is given by

$\begin{array}{ccc}\hfill \mathcal{L}\left(\Phi ,{\Phi }_{k|y=0},{\Phi }_{k|y=1}\right)& =& \prod _{i=1}^{m}p\left({x}^{\left(i\right)},{y}^{\left(i\right)}\right)\hfill \\ & =& \prod _{i=1}^{m}\left(\prod _{j=1}^{{n}_{i}},p,\left({x}_{j}^{\left(i\right)}|y;{\Phi }_{k|y=0},{\Phi }_{k|y=1}\right)\right)p\left({y}^{\left(i\right)};{\Phi }_{y}\right).\hfill \end{array}$

Maximizing this yields the maximum likelihood estimates of the parameters:

$\begin{array}{ccc}\hfill {\Phi }_{k|y=1}& =& \frac{{\sum }_{i=1}^{m}{\sum }_{j=1}^{{n}_{i}}1\left\{{x}_{j}^{\left(i\right)}=k\wedge {y}^{\left(i\right)}=1\right\}}{{\sum }_{i=1}^{m}1\left\{{y}^{\left(i\right)}=1\right\}{n}_{i}}\hfill \\ \hfill {\Phi }_{k|y=0}& =& \frac{{\sum }_{i=1}^{m}{\sum }_{j=1}^{{n}_{i}}1\left\{{x}_{j}^{\left(i\right)}=k\wedge {y}^{\left(i\right)}=0\right\}}{{\sum }_{i=1}^{m}1\left\{{y}^{\left(i\right)}=0\right\}{n}_{i}}\hfill \\ \hfill {\Phi }_{y}& =& \frac{{\sum }_{i=1}^{m}1\left\{{y}^{\left(i\right)}=1\right\}}{m}.\hfill \end{array}$

If we were to apply Laplace smoothing (which needed in practice for good performance) when estimating ${\Phi }_{k|y=0}$ and ${\Phi }_{k|y=1}$ , we add 1 to the numerators and $|V|$ to the denominators, and obtain:

$\begin{array}{ccc}\hfill {\Phi }_{k|y=1}& =& \frac{{\sum }_{i=1}^{m}{\sum }_{j=1}^{{n}_{i}}1\left\{{x}_{j}^{\left(i\right)}=k\wedge {y}^{\left(i\right)}=1\right\}+1}{{\sum }_{i=1}^{m}1\left\{{y}^{\left(i\right)}=1\right\}{n}_{i}+|V|}\hfill \\ \hfill {\Phi }_{k|y=0}& =& \frac{{\sum }_{i=1}^{m}{\sum }_{j=1}^{{n}_{i}}1\left\{{x}_{j}^{\left(i\right)}=k\wedge {y}^{\left(i\right)}=0\right\}+1}{{\sum }_{i=1}^{m}1\left\{{y}^{\left(i\right)}=0\right\}{n}_{i}+|V|}.\hfill \end{array}$

While not necessarily the very best classification algorithm, the Naive Bayes classifier often works surprisingly well. It is often also a very good “first thing to try,”given its simplicity and ease of implementation.

what is the stm
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
what is Nano technology ?
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
why we need to study biomolecules, molecular biology in nanotechnology?
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
why?
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
what does nano mean?
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
it is a goid question and i want to know the answer as well
Maciej
Abigail
for teaching engĺish at school how nano technology help us
Anassong
How can I make nanorobot?
Lily
Do somebody tell me a best nano engineering book for beginners?
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
how can I make nanorobot?
Lily
what is fullerene does it is used to make bukky balls
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
Got questions? Join the online conversation and get instant answers!