Xử lý dữ liệu thiếu

Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

Dữ liệu thiếu (ví dụ)

 ... |       name       | score |               inspection_type                | ... 
 ----+------------------+-------+----------------------------------------------+-----
 ... | ...              | ...   | ...                                          | ...
 ... | SCHNIPPERS       | 27    | Cycle Inspection / Initial Inspection        | ...
 ... | ATOMIC WINGS     |       | Administrative Miscellaneous / Re-inspection | ...
 ... | WING LING        | 44    | Cycle Inspection / Initial Inspection        | ...
 ... | JUAN VALDEZ CAFE | 24    | Cycle Inspection / Initial Inspection        | ...
 ... | FULTON GRAND     | 22    | Cycle Inspection / Initial Inspection        | ...
 ... | ...              | ...   | ...                                          | ...

Biểu diễn giá trị thiếu:

  • NULL (chung)
  • '' - chuỗi rỗng (dùng cho cột kiểu chuỗi)
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Nguyên nhân dữ liệu thiếu

Nguyên nhân gây dữ liệu thiếu? Mảnh ghép đen thiếu một miếng

  • Hình bộ não người trong bóng đầu lỗi con người
  • Hình bánh răng vấn đề hệ thống
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Các loại dữ liệu thiếu

Ba hình minh họa các loại dữ liệu với từng danh mục và viết tắt

Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Các loại dữ liệu thiếu

Ba hình minh họa các loại dữ liệu với từng danh mục và viết tắt, bổ sung mô tả Missing Completely at Random

Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Các loại dữ liệu thiếu

Ba hình minh họa các loại dữ liệu với từng danh mục và viết tắt, bổ sung mô tả Missing at Random

Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Các loại dữ liệu thiếu

 ... |       name       | score |               inspection_type                | ... 
 ----+------------------+-------+----------------------------------------------+-----
 ... | ...              | ...   | ...                                          | ...
 ... | SCHNIPPERS       | 27    | Cycle Inspection / Initial Inspection        | ...
 ... | ATOMIC WINGS     |       | Administrative Miscellaneous / Re-inspection | ...
 ... | WING LING        | 44    | Cycle Inspection / Initial Inspection        | ...
 ... | JUAN VALDEZ CAFE | 24    | Cycle Inspection / Initial Inspection        | ...
 ... | FULTON GRAND     | 22    | Cycle Inspection / Initial Inspection        | ...
 ... | ...              | ...   | ...                                          | ...
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Các loại dữ liệu thiếu

Ba hình minh họa các loại dữ liệu với từng danh mục và viết tắt, bổ sung mô tả Missing Not at Random

Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Nhận diện dữ liệu thiếu

SELECT
  *
FROM
  restaurant_inspection
WHERE
  score IS NULL;
SELECT
  COUNT(*)
FROM
  restaurant_inspection
WHERE
  score IS NULL;
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Nhận diện dữ liệu thiếu

SELECT
  inspection_type,
  COUNT(*) as count
FROM
  restaurant_inspection
WHERE
  score IS NULL
GROUP BY
  inspection_type
ORDER BY
  count DESC;
                  inspection_type                  | count 
 --------------------------------------------------+-------
 Administrative Miscellaneous / Initial Inspection |   104
 Smoke-Free Air Act / Initial Inspection           |    29
 Calorie Posting / Initial Inspection              |    22
 Administrative Miscellaneous / Re-inspection      |    22
 Trans Fat / Initial Inspection                    |    18
 Smoke-Free Air Act / Re-inspection                |     7
 Trans Fat / Re-inspection                         |     3
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Khắc phục dữ liệu thiếu

  • Tốt nhất: tìm và bổ sung giá trị thiếu

    • Có thể không khả thi
    • Có thể không đáng công
  • Gán giá trị (trung bình, trung vị, v.v.)

  • Loại bỏ bản ghi

Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Thay giá trị thiếu với COALESCE()

COALESCE(arg1, [arg2, ...])

SELECT
  name,
  COALESCE(score, -1),
  inspection_type
FROM
  restaurant_inspection;
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Thay giá trị thiếu với COALESCE()

 ... |       name       | score |               inspection_type                | ... 
 ----+------------------+-------+----------------------------------------------+-----
 ... | ...              | ...   | ...                                          | ...
 ... | SCHNIPPERS       | 27    | Cycle Inspection / Initial Inspection        | ...
 ... | ATOMIC WINGS     | -1    | Administrative Miscellaneous / Re-inspection | ...
 ... | WING LING        | 44    | Cycle Inspection / Initial Inspection        | ...
 ... | JUAN VALDEZ CAFE | 24    | Cycle Inspection / Initial Inspection        | ...
 ... | FULTON GRAND     | 22    | Cycle Inspection / Initial Inspection        | ...
 ... | ...              | ...   | ...                                          | ...
Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Ayo berlatih!

Làm sạch dữ liệu trong cơ sở dữ liệu PostgreSQL

Preparing Video For Download...