Docsity

Learning Linear Classifiers with Perceptron & Naive Bayes, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
Linear Classifiers and the Perceptron
William Cohen
February 4, 2008
1 Linear classifiers
Let’s assume that every instance is an n-dimensional vector of real numbers x∈ Rn, and
there are only two possible classes, y= (+1) and y= (−1), so every example is a pair
(x, y). (Notation: I will use boldface to indicate vectors here, so x=hx1, . . . , xni). A linear
classifier is a vector wthat makes the prediction
ˆy= sign(
n
X
i=1
wixi)
where sign(x) = +1 if x≥0 and sign(x) = −1 if x < 0. 1If you remember your linear
algebra this weighted sum of xi’s is called the inner product of wand xand it’s usually
written w·x, so this classifier can be written even more compactly as
ˆy= sign(w·x)
Visually, for a vector w,x·wis the distance of the result you get “if you project xonto w”
(see Figure 1).
It might seem that representing examples as real-number vectors is somewhat constrain-
ing. It seems fine if your attributes are numeric (e.g., “Temperature=72”) but what if
you have an attribute “Outlook” with three possible discrete values “Rainy”, “Sunny”, and
“Cloudy”? One answer is to replace this single attribute with three binary attributes: one
that set to 1 when the outlook is rainy, and zero otherwise; one that set to 1 when the
outlook is sunny, and zero otherwise; and one that set to 1 when the outlook is cloudy, and
zero otherwise. So a dataset like the one below would be converted to examples in R4as
shown:
Outlook Temp PlayTennis?
Day1 Rainy 85 No −→ (h1,0,0,85i,−1)
Day2 Sunny 87 No −→ (h1,0,0,87i,−1)
Day3 Cloudy 75 Yes −→ (h0,0,1,75i,+1)
1This is a little different from the usual definition, where sign(0) = 0, but I’d rather not have to deal with
the question of what to predict when Pn
i=1 wixi= 0.
1
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Learning Linear Classifiers with Perceptron & Naive Bayes and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

Linear Classifiers and the Perceptron

William Cohen

February 4, 2008

1 Linear classifiers

Let’s assume that every instance is an n-dimensional vector of real numbers x ∈ Rn, and there are only two possible classes, y = (+1) and y = (−1), so every example is a pair (x, y). (Notation: I will use boldface to indicate vectors here, so x = 〈x 1 ,... , xn〉). A linear classifier is a vector w that makes the prediction

yˆ = sign(

∑^ n

i=

wixi)

where sign(x) = +1 if x ≥ 0 and sign(x) = −1 if x < 0. 1 If you remember your linear algebra this weighted sum of xi’s is called the inner product of w and x and it’s usually written w · x, so this classifier can be written even more compactly as

ˆy = sign(w · x)

Visually, for a vector w, x · w is the distance of the result you get “if you project x onto w” (see Figure 1). It might seem that representing examples as real-number vectors is somewhat constrain- ing. It seems fine if your attributes are numeric (e.g., “Temperature=72”) but what if you have an attribute “Outlook” with three possible discrete values “Rainy”, “Sunny”, and “Cloudy”? One answer is to replace this single attribute with three binary attributes: one that set to 1 when the outlook is rainy, and zero otherwise; one that set to 1 when the outlook is sunny, and zero otherwise; and one that set to 1 when the outlook is cloudy, and zero otherwise. So a dataset like the one below would be converted to examples in R^4 as shown:

Outlook Temp PlayTennis? Day1 Rainy 85 No −→ (〈 1 , 0 , 0 , 85 〉, −1) Day2 Sunny 87 No −→ (〈 1 , 0 , 0 , 87 〉, −1) Day3 Cloudy 75 Yes −→ (〈 0 , 0 , 1 , 75 〉, +1) (^1) This is a little different from the usual definition, where sign(0) = 0, but I’d rather not have to deal with

the question of what to predict when

∑n i=1 wixi^ = 0.

Figure 1: A geometric view of the inner product.

Another answer, of course, is to abandon these discrete values and instead focus on numeric attributes—e.g., let “Outlook” be encoded as a real number representing the prob- ability of rain, so that Day2 would be encoded as 〈 0. 01 , 87 〉, where the 0.01 means a 1/ chance of rain. However, encoding your examples as pure numeric vectors vectors has one small problem. As I’ve defined it, a linear classifier is doomed to predict ˆy = 1 on a perfectly sunny day (Outlook=0) if the temperature is also zero—regardless of what weight vector w you pick, w · 〈 0 , 0 〉 will be zero, and ˆy will be one. Since zero-degree weather isn’t conducive to playing tennis, no matter how clear it is, we can add one more trick to our encoding of examples and add an extra dimension to each vector, with a value that is always one. Let’s label that extra dimension 0: then

yˆ = sign(w 0 x 0 +

∑^ n

i=

wixi) = sign(w 0 +

∑^ n

i=

wixi) (1)

the second half of the equation holding because for every example x, x 0 = 1. This trick gives our linear classifier a bit more expressive power, and we can still write our classifier using the super-compact notation ˆy = sign(wx) if we like. The weight w 0 is sometimes called a bias term.

A slightly tricky way to get rid of the if-thens in Equation 3 is to write it as

f (xi) ≡ xipi + (1 − xi)qi

(This is essentially the same trick as Tom used in deriving the MLE for a binomial - do you see why?) This of course can be written as

f (xi) = xi(pi − qi) + qi (4)

and then plugging Equation 4 into Equation 2 we get

ˆy = sign

( (^) n ∑

i=

(xi(pi − qi) + qi) + log

P (Y=+1)

P (Y=-1)

) (5)

Now, we’re almost done. Let’s define wi = pi − qi and define :

wi ≡ pi − qi

w 0 ≡

∑

i

qi + log

P (Y=+1)

P (Y=-1)

where w 0 is that “bias term” we used in Equation 1. Now the Naive Bayes prediction from Equation 5 becomes

ˆy = sign(

∑^ n

i=

xiwi + w 0 )

Putting it all together: for binary vectors x′^ = 〈x 1 ,... , xn〉 Naive Bayes can be imple- mented as a linear classifier, to be applied to the augmented example x = 〈x 0 = 1, x 1 ,... , xn〉. The weight vector w has this form:

wi ≡ log

P (Xi = 1|Y=+1) P (Xi = 1|Y=-1)

− log

P (Xi = 0|Y=+1) P (Xi = 0|Y=-1)

w 0 ≡

∑

i

(log

P (Xi = 0|Y=+1) P (Xi = 0|Y=-1)

) + log

P (Y=+1)

P (Y=-1)

and the Naive Bayes classification is

ˆy = sign(w · x)

3 Online learning for classification

3.1 Online learning

Bayesian probability is the most-studied mathematical model of learning. But there are other models that also are experimentally successful, and give useful insight. Sometimes these models are also mathematically more appropriate: e.g., even if all the probabilistic assumptions are correct, a MAP estimate might not minimize prediction errors.

Another useful model is on-line learning. In this document, I’ll consider on-line learners that can do two things. The first is to make a prediction ˆy on an example x, where ˆy ∈ {− 1 , +1}. The second is to update the learner’s state, by “accepting” a new example 〈x, y〉. As some background, remember that if θ is the angle between x and u,

cos θ =

x · u ||x||||u||

This is important if you’re trying to keep a picture in your mind of what all these dot- products mean. A special case is that x · u = ||x|| cos θ when ||u|| = 1. Basically this means that for unit-length vectors, when dot products are large, the angle between the vectors must be small—i.e., the vectors must point in the same direction. Now consider this game, played between two players A and B. Player A provides examples x 1 ,... , xT for each round (chosen arbitrarily). In each round, B first makes a prediction ˆyi (say, using a learner L). Then A picks a label yi, arbitrarily, to assign to xi. If sign(yi) 6 = sign(ˆyi)...or if you prefer, if yi ˆyi < 0...then B has made a mistake. A is trying to force B to make as many mistakes as possible. To make this reasonable, we need a few more constraints on what A is allowed to do, and what B will do.

3.2 The perceptron game

Here’s one version of the online-learning game. There are three extra rules, to make the game “fair” for B.

Margin γ. A must provide examples that can be separated with some vector u with margin γ > 0, ie ∃u : ∀(xi, yi) given by A, (u · x)yi > γ and furthermore, ||u|| = 1. To make sense of this, recall that if θ is the angle between x and u, then

cos θ =

x · u ||x||||u||

so if ||u|| = 1 then ||x|| cos θ = x·u. In other words, x·u is exactly the result of projecting x onto vector u, and x · u > γ means that x is distance γ away from the hyperplane h that is perpendicular to u. This hyperplane h is the separating hyperplane. Notice that y(x · u) > γ means that x is distance γ away from h on the “correct” side of h.

Radius R. A must provide examples “near the origin”, ie

∀xi given by A, ||x||^2 < R

B’s strategy. B uses this learning strategy.

  1. B’s initial guess is v 0 = 0.

3.4 The mistake bound

If you do follow the rules, this simple algorithm is simple to analyze. Amazingly this doesn’t depend at all on the dimension of the x’s, and the proof is very simple.

Theorem 1 Under these rules, it is always the case that k < R/γ^2. In other words, the number of B’s mistakes is bounded by R, the radius the examples fall into, and 1 /γ^2 , the square of the inverse of the margin.

The trick is to show two things: that vk · u grows, but ||vk|| stays small.

Lemma 1 ∀k, vk · u ≥ kγ. In other words, the dot product between vk and u increases with each mistake, at a rate depending on the margin γ.

Proof:

vk+1 · u = (vk + yixi) · u ⇒ vk+1 · u = (vk · u) + yi(xi · u) ⇒ vk+1 · u ≥ vk · u + γ ⇒ vk · u ≥ kγ

The first step is natural to consider—we’re just taking the definition of vk+1, and since we care about the dot product with u, we dot-product both sides with u and then grind away. These steps follow from “B’s strategy”; distributivity; the “margin γ” assumption; and then induction; respectively. That’s the first lemma. Now, vk · u could grow even if vk points “away from” u—i.e., the angle between them stays large—if ||vk|| grows as well. Again, let’s start with the definition of vk+1 and “square” each side (in the dot-product sense) to see what happens to the norm.

Lemma 2 ∀k, ||vk||^2 ≤ kR. In other words, the norm of vk grows “slowly”, at a rate depending on R.

Proof:

vk+1 · vk+1 = (vk + yixi) · (vk + yixi) ⇒ ||vk+1||^2 = ||vk||^2 + 2yixi · vk + y i^2 ||xi||^2 ⇒ ||vk+1||^2 = ||vk||^2 + [something negative] + 1||xi||^2 ⇒ ||vk+1||^2 ≤ ||vk||^2 + ||xi||^2 ⇒ ||vk+1||^2 ≤ ||vk||^2 + R ⇒ ||vk||^2 ≤ kR

These steps follow from: “B’s strategy”; distributivity; “B’s strategy” again (in partic- ular, the fact that there was a mistake, and the fact that yi ∈ {− 1 , +1}); a simplification; the “radius R” assumption; and induction. Finally, we can wrap this up and prove the theorem. Notice that Lemma 2 bounds the norm of vk, while Lemma 1 bounds the dot product of vk and u. Think about this a second:

if the norm of vk is small and the dot product with u is large, then vk might be converging to u—but it depends on the details of how fast these different quantities grow. So the question is, how fast two these trends go? How do we compare them? It’s not obvious, but the way to combine these is to start by squaring the inequality from Lemma 1:

(kγ)^2 ≤ (vk · u)^2 ⇒ k^2 γ^2 ≤ ||vk||^2 ||u||^2 ⇒ k^2 γ^2 ≤ ||vk||^2

The last step follows since conveniently, ||u|| = 1. Now we have a bound on k^2 in terms of the norm of vk. Now put this together with Lemma 2, which has a bound on vk in terms of kR.

k^2 γ^2 ≤ ||vk||^2 ≤ kR

How sweet—now we have a quadratic function in k that’s upper-bounded by a linear function in k. That certainly ought to lead to a bound on k, right? To follow through...

⇒ k^2 γ^2 ≤ kR ⇒ kγ^2 ≤ R ⇒ k ≤ (^) γR 2

And that’s the whole proof for the mistake bound.

3.5 From on-line to “batch” learning

Interestingly, we’ve shown that B won’t make too many mistakes, but we haven’t said anything about how accurate the “last” guess vkmax is on i.i.d data. When the data is separable, it seems like it should be ok—but the mistake-bound analysis can be extended to handle non-separable data as well, and in that case it’s not at all clear what we should do. Imagine we run the on-line perceptron and see this result. i guess input result 1 v 0 x 1 X (a mistake) 2 v 1 x 2

(correct!) 3 v 1 x 3

4 v 1 x 4 X (a mistake) 5 v 2 x 5

6 v 2 x 6

7 v 2 x 7

8 v 2 x 8 X 9 v 3 x 9

10 v 3 x 10 X