Phương thức predict
Bài học 14 trên 19 trong khóa học Giới thiệu về học máy của Coddy.
Thử thách
Dễpredict gán hoặc dự đoán một lớp cho điểm dữ liệu mới bằng công thức sau:
| 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"]Ở bài học trước, chúng ta đã viết get_classes_probability.
Bây giờ, trong phương thức predict, chúng ta cần trả về đầu ra của get_classes_probability cho điểm dữ liệu mới.
Tự làm thử
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):
passTất cả bài học trong Giới thiệu về học máy
Tự luyện tập: Trình biên dịch Python online