결측치 처리

PostgreSQL 데이터베이스에서 데이터 정제하기

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

결측치(예시)

 ... |       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        | ...
 ... | ...              | ...   | ...                                          | ...

결측값 표현:

  • NULL (일반)
  • '' - 빈 문자열(문자열 컬럼)
PostgreSQL 데이터베이스에서 데이터 정제하기

결측치의 원인

결측치는 왜 생기나요? 빈 퍼즐 조각이 빠진 검은 퍼즐

  • 머리 실루엣 속 뇌 이미지 사람 오류
  • 기어 이미지 시스템적 문제
PostgreSQL 데이터베이스에서 데이터 정제하기

결측치 유형

각 범주와 약어가 표시된 데이터 유형 3가지

PostgreSQL 데이터베이스에서 데이터 정제하기

결측치 유형

각 범주와 약어가 표시된 데이터 유형 3가지와 완전무작위 결측(MCAR) 설명 추가

PostgreSQL 데이터베이스에서 데이터 정제하기

결측치 유형

각 범주와 약어가 표시된 데이터 유형 3가지와 무작위 결측(MAR) 설명 추가

PostgreSQL 데이터베이스에서 데이터 정제하기

결측치 유형

 ... |       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        | ...
 ... | ...              | ...   | ...                                          | ...
PostgreSQL 데이터베이스에서 데이터 정제하기

결측치 유형

각 범주와 약어가 표시된 데이터 유형 3가지, 그리고 비무작위 결측(MNAR) 설명 추가

PostgreSQL 데이터베이스에서 데이터 정제하기

결측치 식별

SELECT
  *
FROM
  restaurant_inspection
WHERE
  score IS NULL;
SELECT
  COUNT(*)
FROM
  restaurant_inspection
WHERE
  score IS NULL;
PostgreSQL 데이터베이스에서 데이터 정제하기

결측치 식별

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
PostgreSQL 데이터베이스에서 데이터 정제하기

결측치 보정 방법

  • 최선: 결측값을 찾아 추가

    • 불가능할 수 있음
    • 노력 대비 가치가 낮을 수 있음
  • 값 대체(평균, 중앙값 등)

  • 레코드 제외

PostgreSQL 데이터베이스에서 데이터 정제하기

COALESCE()로 결측값 대체

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

SELECT
  name,
  COALESCE(score, -1),
  inspection_type
FROM
  restaurant_inspection;
PostgreSQL 데이터베이스에서 데이터 정제하기

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        | ...
 ... | ...              | ...   | ...                                          | ...
PostgreSQL 데이터베이스에서 데이터 정제하기

Ayo berlatih!

PostgreSQL 데이터베이스에서 데이터 정제하기

Preparing Video For Download...