1 of 20

Algorithms for �Classification:

The Basic Methods

2 of 20

Outline

  • Naïve Bayes

2

3 of 20

Classification

  • Task: Given a set of pre-classified examples, build a model or classifier to classify new cases.
  • Supervised learning: classes are known for the examples used to build the classifier.
  • A classifier can be a set of rules, a decision tree, a neural network, etc.
  • Typical applications: credit approval, direct marketing, fraud detection, medical diagnosis, …

3

4 of 20

Simplicity first

  • Simple algorithms often work very well!
  • There are many kinds of simple structure, eg:
    • Majority class classifier
    • One attribute does all the work
    • All attributes contribute equally & independently
    • A weighted linear combination might do
    • Instance-based: use a few prototypes
    • Use simple logical rules
  • Success of method depends on the domain

4

5 of 20

Bayesian (Statistical) modeling

  • “Opposite” of 1R: use all the attributes
  • Two assumptions: Attributes are
    • equally important
    • statistically independent (given the class value)
      • I.e., knowing the value of one attribute says nothing about the value of another�(if the class is known)
  • Independence assumption is almost never correct!
  • But … this scheme works well in practice

5

6 of 20

Probabilities for weather data

6

Outlook

Temperature

Humidity

Windy

Play

Yes

No

Yes

No

Yes

No

Yes

No

Yes

No

Sunny

2

3

Hot

2

2

High

3

4

False

6

2

9

5

Overcast

4

0

Mild

4

2

Normal

6

1

True

3

3

Rainy

3

2

Cool

3

1

Sunny

2/9

3/5

Hot

2/9

2/5

High

3/9

4/5

False

6/9

2/5

9/14

5/14

Overcast

4/9

0/5

Mild

4/9

2/5

Normal

6/9

1/5

True

3/9

3/5

Rainy

3/9

2/5

Cool

3/9

1/5

Outlook

Temp

Humidity

Windy

Play

Sunny

Hot

High

False

No

Sunny

Hot

High

True

No

Overcast

Hot

High

False

Yes

Rainy

Mild

High

False

Yes

Rainy

Cool

Normal

False

Yes

Rainy

Cool

Normal

True

No

Overcast

Cool

Normal

True

Yes

Sunny

Mild

High

False

No

Sunny

Cool

Normal

False

Yes

Rainy

Mild

Normal

False

Yes

Sunny

Mild

Normal

True

Yes

Overcast

Mild

High

True

Yes

Overcast

Hot

Normal

False

Yes

Rainy

Mild

High

True

No

7 of 20

Probabilities for weather data

  • A new day:

7

Outlook

Temp.

Humidity

Windy

Play

Sunny

Cool

High

True

?

Likelihood of the two classes

For “yes” = 2/9 × 3/9 × 3/9 × 3/9 × 9/14 = 0.0053

For “no” = 3/5 × 1/5 × 4/5 × 3/5 × 5/14 = 0.0206

Conversion into a probability by normalization:

P(“yes”) = 0.0053 / (0.0053 + 0.0206) = 0.205

P(“no”) = 0.0206 / (0.0053 + 0.0206) = 0.795

Outlook

Temperature

Humidity

Windy

Play

Yes

No

Yes

No

Yes

No

Yes

No

Yes

No

Sunny

2

3

Hot

2

2

High

3

4

False

6

2

9

5

Overcast

4

0

Mild

4

2

Normal

6

1

True

3

3

Rainy

3

2

Cool

3

1

Sunny

2/9

3/5

Hot

2/9

2/5

High

3/9

4/5

False

6/9

2/5

9/14

5/14

Overcast

4/9

0/5

Mild

4/9

2/5

Normal

6/9

1/5

True

3/9

3/5

Rainy

3/9

2/5

Cool

3/9

1/5

8 of 20

Bayes’s rule

  • Probability of event H given evidence E :���
  • A priori probability of H :
    • Probability of event before evidence is seen
  • A posteriori probability of H :
    • Probability of event after evidence is seen

8

Thomas Bayes

Born: 1702 in London, England�Died: 1761 in Tunbridge Wells, Kent, England

from Bayes “Essay towards solving a problem in the doctrine of chances” (1763)

9 of 20

Naïve Bayes for classification

  • Classification learning: what’s the probability of the class given an instance?
    • Evidence E = instance
    • Event H = class value for instance
  • Naïve assumption: evidence splits into parts (i.e. attributes) that are independent

9

10 of 20

Weather data example

10

Outlook

Temp.

Humidity

Windy

Play

Sunny

Cool

High

True

?

Evidence E

Probability of

class “yes”

11 of 20

The “zero-frequency problem”

  • What if an attribute value doesn’t occur with every class value?�(e.g. “Humidity = high” for class “yes”)
    • Probability will be zero!
    • A posteriori probability will also be zero!�(No matter how likely the other values are!)

  • Remedy: add 1 to the count for every attribute value-class combination (Laplace estimator)

  • Result: probabilities will never be zero!�(also: stabilizes probability estimates)

11

12 of 20

*Modified probability estimates

  • In some cases adding a constant different from 1 might be more appropriate
  • Example: attribute outlook for class yes

  • Weights don’t need to be equal �(but they must sum to 1)

12

Sunny

Overcast

Rainy

13 of 20

Missing values

  • Training: instance is not included in frequency count for attribute value-class combination
  • Classification: attribute will be omitted from calculation
  • Example:

13

Outlook

Temp.

Humidity

Windy

Play

?

Cool

High

True

?

Likelihood of “yes” = 3/9 × 3/9 × 3/9 × 9/14 = 0.0238

Likelihood of “no” = 1/5 × 4/5 × 3/5 × 5/14 = 0.0343

P(“yes”) = 0.0238 / (0.0238 + 0.0343) = 41%

P(“no”) = 0.0343 / (0.0238 + 0.0343) = 59%

14 of 20

Numeric attributes

  • Usual assumption: attributes have a normal or Gaussian probability distribution (given the class)
  • The probability density function for the normal distribution is defined by two parameters:
    • Sample mean μ

    • Standard deviation σ

    • Then the density function f(x) is

14

Karl Gauss, 1777-1855

great German mathematician

15 of 20

Statistics for�weather data

  • Example density value:

15

Outlook

Temperature

Humidity

Windy

Play

Yes

No

Yes

No

Yes

No

Yes

No

Yes

No

Sunny

2

3

64, 68,

65, 71,

65, 70,

70, 85,

False

6

2

9

5

Overcast

4

0

69, 70,

72, 80,

70, 75,

90, 91,

True

3

3

Rainy

3

2

72, …

85, …

80, …

95, …

Sunny

2/9

3/5

μ =73

μ =75

μ =79

μ =86

False

6/9

2/5

9/14

5/14

Overcast

4/9

0/5

σ =6.2

σ =7.9

σ =10.2

σ =9.7

True

3/9

3/5

Rainy

3/9

2/5

16 of 20

Classifying a new day

  • A new day:

  • Missing values during training are not included in calculation of mean and standard deviation

16

Outlook

Temp.

Humidity

Windy

Play

Sunny

66

90

true

?

Likelihood of “yes” = 2/9 × 0.0340 × 0.0221 × 3/9 × 9/14 = 0.000036

Likelihood of “no” = 3/5 × 0.0291 × 0.0380 × 3/5 × 5/14 = 0.000136

P(“yes”) = 0.000036 / (0.000036 + 0. 000136) = 20.9%

P(“no”) = 0.000136 / (0.000036 + 0. 000136) = 79.1%

17 of 20

*Probability densities

  • Relationship between probability and density:

  • But: this doesn’t change calculation of a posteriori probabilities because ε cancels out
  • Exact relationship:

17

18 of 20

Naïve Bayes: discussion

  • Naïve Bayes works surprisingly well (even if independence assumption is clearly violated)
  • Why? Because classification doesn’t require accurate probability estimates as long as maximum probability is assigned to correct class
  • However: adding too many redundant attributes will cause problems (e.g. identical attributes)
  • Note also: many numeric attributes are not normally distributed (→ kernel density estimators)

18

19 of 20

Naïve Bayes Extensions

  • Improvements:
    • select best attributes (e.g. with greedy search)
    • often works as well or better with just a fraction of all attributes

  • Bayesian Networks

19

20 of 20

Summary

  • Naïve Bayes – use all attributes and Bayes rules to estimate probability of the class given an instance.

  • Simple methods frequently work well, but …
    • Complex methods can be better (as we will see)

20