計算機率
where9am 資料框包含 91 天(13 週)的資料,Brett 記錄了他每天上午 9 點的 location,以及 daytype 是週末或平日。
使用下方的條件機率公式,你可以計算在已知是平日的情況下,Brett 在辦公室上班的機率。
$$ P(A|B) = \frac{P(A \text{ and } B)}{P(B)} $$
這類計算就是你在後續練習中要開發的 Naive Bayes 目的地預測模型的基礎。
本練習屬於課程
R 監督式學習:分類
練習說明
- 使用
nrow()和subset()計算資料集中的列數來求出 P(office),並將結果儲存為p_A。 - 再次使用
nrow()和subset()求出 P(weekday),並將結果儲存為p_B。 - 最後再用一次
nrow()和subset()求出 P(office and weekday),將結果儲存為p_AB。 - 計算 P(office | weekday),並將結果儲存為
p_A_given_B。 - 列印
p_A_given_B的值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Compute P(A)
p_A <- ___
# Compute P(B)
p_B <- ___
# Compute the observed P(A and B)
p_AB <- ___
# Compute P(A | B) and print its value
p_A_given_B <- ___
___