抽樣與點估計

Python 中的抽樣

James Chapman

Curriculum Manager, DataCamp

估計法國人口

法國地圖。

人口普查會詢問每戶住了多少人。

Python 中的抽樣

法國人口很多

帶有人物圖示的法國地圖。

人口普查成本很高!

Python 中的抽樣

抽樣住戶

帶有人物圖示且部分高亮的法國地圖。

改問少數住戶,並用統計來估計總人口,更省錢。

用整體的一部分來推論稱為「抽樣」

Python 中的抽樣

母體 vs. 樣本

「母體」是完整的資料集

  • 不一定指人
  • 通常不知道整個母體為何

 

「樣本」是你用來計算的資料子集

Python 中的抽樣

咖啡評分資料集

total_cup_points variety country_of_origin aroma flavor aftertaste body balance
90.58 NA Ethiopia 8.67 8.83 8.67 8.50 8.42
89.92 Other Ethiopia 8.75 8.67 8.50 8.42 8.42
... ... ... ... ... ... ... ...
73.75 NA Vietnam 6.75 6.67 6.5 6.92 6.83

 

  • 每列代表 1 種咖啡
  • 共 1338 列
  • 我們將其視為母體
Python 中的抽樣

總分 vs. 風味:母體

pts_vs_flavor_pop = coffee_ratings[["total_cup_points", "flavor"]]
      total_cup_points  flavor
0                90.58    8.83
1                89.92    8.67
2                89.75    8.50
3                89.00    8.58
4                88.83    8.50
...                ...     ...
1333             78.75    7.58
1334             78.08    7.67
1335             77.17    7.33
1336             75.08    6.83
1337             73.75    6.67

[1338 rows x 2 columns]
Python 中的抽樣

總分 vs. 風味:10 列樣本

pts_vs_flavor_samp = pts_vs_flavor_pop.sample(n=10)
      total_cup_points  flavor
1088             80.33    7.17
1157             79.67    7.42
1267             76.17    7.33
506              83.00    7.67
659              82.50    7.42
817              81.92    7.50
1050             80.67    7.42
685              82.42    7.50
1027             80.92    7.25
62               85.58    8.17

[10 rows x 2 columns]
Python 中的抽樣

Series 的 Python 抽樣

  • pandas DataFrame 與 Series 使用 .sample()
cup_points_samp = coffee_ratings['total_cup_points'].sample(n=10)
1088    80.33
1157    79.67
1267    76.17
...     ... 
685     82.42
1027    80.92
62      85.58
Name: total_cup_points, dtype: float64
Python 中的抽樣

母體參數與點估計

「母體參數」是對母體資料集做的計算

import numpy as np
np.mean(pts_vs_flavor_pop['total_cup_points'])
82.15120328849028

「點估計」或「樣本統計量」是對樣本資料集做的計算

np.mean(cup_points_samp)
81.31800000000001
Python 中的抽樣

用 pandas 做點估計

pts_vs_flavor_pop['flavor'].mean()
7.526046337817639
pts_vs_flavor_samp['flavor'].mean()
7.485000000000001
Python 中的抽樣

一起來練習吧!

Python 中的抽樣

Preparing Video For Download...