Menu
Coddy logo textTech

fit method

Lesson 13 of 19 in Coddy's Introduction to Machine Learning course.

Here is the Naive Bayes formula:
<strong>P(Class|Feature) = P(Feature|Class) * P(Class) / P(Feature)</strong>

We already calculated <strong>P(Feature|Class)</strong> and <strong>P(Class)</strong> in the previous lesson.

Now, based on mathematical principles, we can simplify our calculations by omitting P(Feature). For example, if 5 > 3 and we divide both sides by 10 (to obtain 5/10 > 3/10), the inequality remains true.

Therefore, our simplified formula becomes: P(Class|Feature) = P(Feature|Class) * P(Class).

Consider a new data point with the following attributes:
 

temperaturemoodmotivationwindy
lowhappyhighyes

To calculate for all instances where play = yes, use the formula:

P(temperature=<strong>low</strong>|play=Yes) * P(mood= <strong>happy</strong>|play=Yes) * P(motivation= <strong>high</strong>|play=Yes) * P(windy=<strong>yes</strong>|play=Yes) * p(play=yes)

All of this data is already saved in our probability_features and probability_target variables:

p_yes = probability_features["temperature"]["low"]["yes"] *
	probability_features["mood"]["happy"]["yes"] *
	probability_features["motivation"]["high"]["yes"] *
	probability_features["windy"]["yes"]["yes"] *
	probability_target["yes"]

Now we need to do that same thing for play = no.

After obtaining both probabilities, we compare them to determine the larger one.

If p_yes is greater than p_no, we classify this new data point as yes; otherwise, we classify it as no.

challenge icon

Challenge

Medium

In this challenge, complete the fit method so that it stores the probabilities in self.probability_features and self.probability_target, just as we did in the previous lesson.

y_train is our target variable, and X_train is our dataset of features.

Also, complete the get_classes_probability method. This method should take a new data point (similar to the example provided earlier) and return the probabilities for each class of the target variable (the p_yes and p_no).
The output should be in the following format (depending on the values in the target variable y_train)

classes_probabilities = {
    "yes": 0.00,
    "no": 0.00, 
}

Keep in mind that the feature names and the attribute names are not constant. Each dataset has different features and different names.

Try it yourself

class NaiveBayes:
    class __init__(self):
        pass
    
    def get_classes_probability(self, X):
        # X holds a dataframe with a single row
        # use self.classes that was saved in fit method
        # write your code below
        classes_probabilities = {}
        
        
        # --------------------------
        return classes_probabilities
    
    def fit(self, X_train, y_train):
        self.probability_features = {}
        self.probability_target = {}
        target_variable = "target"
        df = X_train.copy()
        df[target_variable] = y_train
        # Copy your code from previous lesson
        # Add self. to probability_features and probability_target
        
    
    def predict(self, X_test):
        pass

All lessons in Introduction to Machine Learning