Can values for X,y be categroical ? Encoding Categorical Variables

BernoulliNB()

Why Naive Bayes?;;Order doesn’t matter, features are independent. Treated it as a Bag of words. Which simplifies the above equation.

Want to use this in classifiers for ML Want to understand: Multinomial Naive bayes classifer There is also: Gaussian Naive Bayes

Issues

To avoid having 0 probability sometimes they add counts to do this.

https://youtu.be/PPeaRc-r1OI?t=169

Formula

Think of the line as “given”.

Examples

Example 1

Example 2

In the formula above P(A) is P(+), P(B)=P(NEW)

P(B|A) = P(A=0|+)*… *P(C=0|+)

P(A=0,B=1,C=0) is the same for both + and - class so remove.

Example Car accidents

What’s the probability of car having an accident given that driver is driving in summer, there is no rain, it’s a night and it’s an urban area?

Mock data:

SeasonWeatherDaytimeAreaDid Accident Occur?
SummerNo-RainingNightUrbanNo
SummerNo-RainingDayUrbanNo
SummerRainingNightRuralNo
SummerRainingNightUrbanYes
SummerRainingDayUrbanNo
SummerRainingNightRuralNo
WinterRainingNightUrbanYes
WinterRainingNightUrbanYes
WinterRainingNightRuralYes
WinterNo-RainingNightRuralNo
WinterNo-RainingNightUrbanNo
WinterNo-RainingDayUrbanYes
SpringNo-RainingNightRuralYes
SpringNo-RainingDayRuralYes
SpringRainingNightUrbanNo
SpringRainingDayNoNo
SpringNo-RainingNightUrbanNo
AutumnRainingNightUrbanYes
AutumnRainingDayRuralYes
AutumnNo-RainingNightUrbanNo
AutumnNo-RainingDayRuralNo
AutumnNo-RainingDayUrbanNo
AutumnRainingDayYesNo
AutumnRainingNightYesNo
AutumnNo-RainingNightNoNo

To handle data like this it is possible to calculate frequencies for each case:

0. Accident probability

1. Season probability

Frequency table:

SeasonAccidentNo Accident
Spring2/93/165/25
Summer1/95/166/25
Autumn2/96/168/25
Winter4/92/166/25
9/2516/25

Probabilities based on table:

2. Weather probability

Frequency table:

AccidentNo Accident
Raining6/97/1613/25
No-Raining3/99/1612/25
9/2516/25

Probabilities based on table:

3. Daytime probability

Frequency table:

AccidentNo Accident
Day3/96/169/25
Night6/910/1616/25
9/2516/25

Probabilities based on table:

4. Area probability

Frequency table:

AccidentNo Accident
Urban Area5/98/1613/25
Rural Area4/98/1612/25
9/2516/25

Probabilities based on table:

Assemble:

Calculating probablity of car accident occuring in summer, when there is no rain and during night, in urban area.

Where B equals to:

  • Season: Summer
  • Weather: No-Raining
  • Daytime: Night
  • Area: Urban

Where A equals to:

  • Accident

Using Naive Bayes:


What is the Bayes theorem?;; The formula is P(A|B) = P(B|A) * P(A) / P(B).

What are the main advantages of Naive Bayes, and when is it commonly used?;; simplicity, quick implementation, and scalability, used in text classification.

**When using Naive Bayes with numerical variables, what condition is assumed on the data?;; Naive Bayes assumes that numerical variables follow a normal distribution.

How does Naive Bayes perform with categorical variables? makes no assumptions about the data distribution.

What is Naive Bayes, and why is it called “naive”?;; Algo which uses Bayes theorem, used for classification problems. It is “naive” because it assumes that predictor variables are independent, which may not be the case in reality. The algorithm calculates the probability of an item belonging to each possible class and chooses the class with the highest probability as the output.

Naive Bayes Naive Bayes classifiers are based on Bayes’ theorem and assume that the features are conditionally independent given the class label.

Naive Bayes

  • A probabilistic classifier based on Bayes’ theorem.
  • Simple and fast, especially effective for text classification.