predict method
Lesson 14 of 19 in Coddy's Introduction to Machine Learning course.
Challenge
EasyThe predict assigns (or predicts) a class to a new data point using the following formula:
| temperature | mood | motivation | windy |
| low | happy | high | yes |
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"]Previous lesson we wrote the get_classes_probability.
Now in the predict method we need to return the output of get_classes_probability of the new data point.
Try it yourself
class NaiveBayes:
def __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 = {}
for cls in self.classes:
classes_probabilities[cls] = 1
for column in X.columns:
for distinct_value in X[column].unique():
classes_probabilities[cls] *= self.probability_features[column][distinct_value][cls]
classes_probabilities[cls] *= self.probability_target[cls]
# --------------------------
return classes_probabilities
def fit(self, X_train, y_train):
self.probability_features = {}
self.probability_target = {}
self.classes = y_train.unique()
target_variable = "target"
df = X_train.copy()
df[target_variable] = y_train
# Copy your code from previous lesson
# Don't forget to Add self. to probability_features and probability_target
distinct_target_values = df[target_variable].unique()
for column in df.columns:
if column == target_variable:
continue
self.probability_features[column] = {}
distinct_values = df[column].unique()
for value in distinct_values:
self.probability_features[column][value] = {}
for target_value in distinct_target_values:
total = len(df[(df[column] == value) & (df[target_variable] == target_value)][target_variable])
count = len(df[(df[column] == value)])
self.probability_features[column][value][target_value] = total / count
for target_value in distinct_target_values:
total = len(df[(df[target_variable] == target_value)][target_variable])
self.probability_target[target_value] = total / len(df[target_variable])
def predict(self, X_test):
pass