機率有多大?

Python 統計學入門

Maggie Matsui

Content Developer, DataCamp

衡量機率

事件的機率是什麼?

$$ P(\text{event}) = \frac{\text{事件可發生的方式數}}{\text{所有可能結果的總數}} $$

範例:擲硬幣

$$ P(\text{heads}) = \frac{\text{出現正面的 1 種方式}}{\text{2 種可能結果}} = \frac{1}{2} = 50\%$$

機率數線。0% = 不可能,100% = 一定會發生

Python 統計學入門

指派業務員

盒子裡有 Amir、Brian、Claire、Damian 的名字

Python 統計學入門

指派業務員

抽出 Brian 的名字

$$P(\text{Brian}) = \frac{1}{4} = 25\%$$

Python 統計學入門

從 DataFrame 抽樣

print(sales_counts)
     name  n_sales
0    Amir      178
1   Brian      128
2  Claire       75
3  Damian       69
sales_counts.sample()
    name  n_sales
1  Brian      128
sales_counts.sample()
     name  n_sales
2  Claire       75
Python 統計學入門

設定隨機種子

np.random.seed(10)

sales_counts.sample()
    name  n_sales
1  Brian      128
np.random.seed(10)
sales_counts.sample()
    name  n_sales
1  Brian      128
np.random.seed(10)
sales_counts.sample()
    name  n_sales
1  Brian      128
Python 統計學入門

第二次會議

不放回抽樣

盒子裡有 Amir、Claire、Damian

Python 統計學入門

第二次會議

抽出 Claire 的名字

$$P(\text{Claire}) = \frac{1}{3} = 33\%$$

Python 統計學入門

Python 中抽兩次

sales_counts.sample(2)
     name  n_sales
1   Brian      128
2  Claire       75
Python 統計學入門

放回抽樣

手伸進盒子抽出 Brian 的名字,然後放回

Python 統計學入門

放回抽樣

Screen Shot 2020-04-28 at 5.21.54 PM.png

$$P(\text{Claire}) = \frac{1}{4} = 25\%$$

Python 統計學入門

Python:放回/不放回抽樣

sales_counts.sample(5, replace = True)
     name  n_sales
1   Brian      128
2  Claire       75
1   Brian      128
3  Damian       69
0    Amir      178
Python 統計學入門

獨立事件

若第二個事件的機率不受第一個事件結果影響,兩事件即為獨立

兩欄:第一次抽有 Amir、Brian、Claire、Damian。第二次抽為空欄

Python 統計學入門

獨立事件

若第二個事件的機率不受第一個事件結果影響,兩事件即為獨立

 

放回抽樣 = 每次抽取皆獨立

第一次抽的每個名字都有箭頭指向第二次抽的 Claire,機率為 25%

Python 統計學入門

相依事件

若第二個事件的機率會受第一個事件結果影響,兩事件即為相依

兩欄:第一次抽有 Amir、Brian、Claire、Damian。第二次抽為空欄

Python 統計學入門

相依事件

若第二個事件的機率會受第一個事件結果影響,兩事件即為相依

第一欄的 Claire 指向第二欄的 Claire,機率為 0%

Python 統計學入門

相依事件

若第二個事件的機率會受第一個事件結果影響,兩事件即為相依

 

不放回抽樣 → 抽取結果會相依

第一欄的 Amir、Brian、Damian 指向第二欄的 Claire,機率為 33%

Python 統計學入門

一起來練習吧!

Python 統計學入門

Preparing Video For Download...