Docsity

The 10 Commandments for Java Programmers - Lecture Slides | CS 351L, Exams of Computer Science

Partial preview of the text

Download The 10 Commandments for Java Programmers - Lecture Slides | CS 351L and more Exams Computer Science in PDF only on Docsity!

The 10 Commandments

for Java Programmers

VII: Thou shalt study thy libraries

with care and strive not to reinvent

them without cause, that thy code

may be short and readable and thy

days pleasant and productive.

Loosely adapted from “10 Commandments for C Programmers”, by Henry Spencer

Bayesian Decision Making 101

  • Deciding between 2 classes, C N

and C

S

  • Based on some data X (email message)
  • Decision criterion is probability test:

Pr[ C

S

| X ] <?> Pr[ C

N

| X ]

  • fi SPAM; < fi NORMAL

  • Pr[ C S

| X ] and Pr[ C

N

| X ] are posterior

probabilities (after the fact) -- I.e., after

you see the data X

Finding the Posteriors

  • Problem: LABELED email only tells you a

little bit -- namely Pr[ C

i

| X ]=1 or 0 for a

specific X.

  • But through the magic of Bayes’ rule, we can turn the question around:

Pr[ C

i

| X ]=Pr[ X | C

i

]*Pr[ C

i

]/Pr[ X ]

  • This is called the “Posterior probability” of

C

i (after the data)

The Parts of the Posterior

  • Now we have 3 parts. Is life better?
  • Look at them one at a time:
  • Pr[ X ]
    • This is the “data prior”
    • Represents the probability of the data “independent of SPAM or NORMAL”.
    • I.e., averaged over SPAM/NORMAL
  • This one is irrelevant to us. Why?

The Class Prior

  • Ok, now what about Pr[ C S

] and Pr[ C

N

]?
  • These are called CLASS PRIORS, or sometimes just PRIORS (from “before the data” -- before you’ve looked at a specific

X )

  • These are easy -- just the probability of any message being SPAM or NORMAL
  • E.g.:

Pr[ C

S ]=(#SPAM emails) (#all emails)

2 Down, 1 to Go…

  • That leaves Pr[ X | C i
]
  • This is called the generative model
  • It basically says:
    • Think of two machines -- a SPAM machine and a NORMAL machine
    • If you turn on the SPAM machine, it spits out an email message.
    • Given that you turned on the SPAM machine, what’s the chance you saw this particular X?

Approximating Pr[ X | C

i

]

  • Massive, ruthless approximation time…
  • Break up X into a bunch of small pieces

called features: X =· x

1

,x

2

,…,x

k

Ò
  • Features might be:
    • Words in an email
    • N-grams (sequences of N characters)
    • Line lengths
    • Etc.
  • Now write: Pr[ X | C i
]=∏

j= k

Pr[ x

j

| C

i

]
  • This is the “naïve Bayes” approximation

Measuring Feature Probs

  • Now you need Pr[ x j

| C

i

]
  • But that’s easy to measure: frequency of a given feature (e.g., a word) in the TRAINING data
  • E.g., suppose features are “every word you might see”.
  • Then,

Pr[“viagra”| C

S ]=(# “viagra” in SPAM) (# all words in SPAM)

Practical Issue

  • Underflow
  • Look back at the generative model equation again:

Pr[ C

S

| X ]μPr[ C

S

]∏

j= k

Pr[ x

j

| C

S

]
  • What happens when, say k=500? (E.g., your message has 500 different words)
  • Product of 500 factors, each between 0. and 1.0…
  • The variables that you’re storing Pr[ C i

| X ]

in (e.g., doubles) quickly go to 0 …