Lấy mẫu ngẫu nhiên

Phân tích dữ liệu khảo sát bằng Python

EbunOluwa Andrew

Data Scientist

Lấy mẫu trong phân tích khảo sát

  • Lấy mẫu = tập nhỏ từ quần thể lớn
    • Suy luận về quần thể lớn hơn
    • Lấy mẫu -> dữ liệu dễ xử lý
    • Lấy mẫu -> sai số lấy mẫu
    • Giảm sai số với cỡ mẫu lớn

Ảnh của Patrick Fore trên Unsplash - trẻ cầm kẹo

Phân tích dữ liệu khảo sát bằng Python

Lấy mẫu ngẫu nhiên

  • Mỗi phần tử có khả năng chọn như nhau
  • Giảm thiên lệch
  • Độ giá trị nội bộ cao
  • Độ giá trị bên ngoài cao

vé số

Phân tích dữ liệu khảo sát bằng Python

Phương thức .sample()

  • DataFrame.sample(n = _None_, frac = _None_, random_state = _None_)
  • n = số phần tử lấy mẫu
  • frac = tỷ lệ (0–1) phần tử trả về
  • random_state = seed để tái lập kết quả
Phân tích dữ liệu khảo sát bằng Python

Ví dụ lấy mẫu ngẫu nhiên

import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample(n=100) print(sample)
|       | employee_id | gender | onsite_work |
|-------|-------------|--------|-------------|
| 3244  | fffe330     | Female | Yes         |
| 21339 | fffe310     | Male   | Yes         |
| 1122  | fffe390     | Male   | Yes         |
| 4363  | fffe313     | Female | Yes         |
Phân tích dữ liệu khảo sát bằng Python

Ví dụ lấy mẫu ngẫu nhiên

import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample(frac = 0.1) print(sample)
|     | employee_id | gender | onsite_work |
|-----|-------------|--------|-------------|
| 142 | fffe800     | Female | Yes         |
| 710 | fffe900     | Female | Yes         |
| 242 | fffe700     | Female | Yes         |
| 114 | fffe600     | Female | Yes         |
Phân tích dữ liệu khảo sát bằng Python

Ví dụ lấy mẫu ngẫu nhiên

import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample( n = 100, random_state = 123)
import pandas as pd
survey = pd.read_csv('ABC_survey.csv')

sample = survey.sample( frac = 0.1, random_state = 123)
|       | employee_id | gender | onsite_work |
|-------|-------------|--------|-------------|
| 21383 | fffe3       | Female | Yes         |
| 82    | fffe0       | Male   | Yes         |
| 20739 | fffe2       | Male   | Yes         |
| 7662  | fffe9       | Female | Yes         |
|       | employee_id | gender | onsite_work |
|-------|-------------|--------|-------------|
| 21383 | fffe3       | Female | Yes         |
| 82    | fffe0       | Male   | Yes         |
| 20739 | fffe2       | Male   | Yes         |
| 7662  | fffe9       | Female | Yes         |
Phân tích dữ liệu khảo sát bằng Python

Ayo berlatih!

Phân tích dữ liệu khảo sát bằng Python

Preparing Video For Download...