데이터 준비

엔드 투 엔드 Machine Learning

Joshua Stapleton

Machine Learning Engineer

데이터 준비 단계

데이터셋 구성:

  • 누락값
  • 이상치
  • 불균형
  • 빈 열
  • 중복

데이터 준비:

  • EDA 인사이트 기반
  • 다운스트림 모델 성능에 핵심
엔드 투 엔드 Machine Learning

Null / 빈 값

  • 결측/희소 행·열 삭제
  • Null은 모델을 깨뜨릴 수 있음
  • 열은 df.drop() 사용
  • 행은 df.dropna(how='all') 사용
# count missing values
print(df['oldpeak'].isnull().sum())

# Drop empty column(s) and row(s)
columns_dropped = heart_disease_df.drop(['oldpeak'], axis='columns')
rows_and_columns_dropped = columns_dropped.dropna(how='all')
엔드 투 엔드 Machine Learning

Null / 빈 값 처리

  • 정리/삭제는 EDA 결과에 따름

 

  • 특정 열의 결측이 너무 많으면:
    • 열 삭제

 

  • 타깃 열에 결측이 있으면:
    • 결측 타깃 행 삭제
    • 또는 별도 범주로 처리
엔드 투 엔드 Machine Learning

대치(Imputation)

결측이 소수일 때는?

  • 대치:

    • 결측을 대체값으로 채움
  • 전략

    • 평균·중앙값으로 채우기
    • 상수 또는 이전값 사용
# Calculate the mean cholestrol value 
mean_value = heart_disease_df['chol'].mean()

# Fill missing cholestrol values with the mean
heart_disease_df['chol'].fillna(mean_value, inplace=True)
엔드 투 엔드 Machine Learning

고급 대치

고급 기법:

  • K-최근접 이웃
  • SMOTE(소수 클래스 합성 오버샘플링)
from sklearn.impute import KNNImputer

# Initialize KNNImputer
imputer = KNNImputer(n_neighbors=2, weights="uniform")

# Perform the imputation on your DataFrame
df_imputed['oldpeak'] = imputer.fit_transform(df['oldpeak'])
엔드 투 엔드 Machine Learning

중복 제거

 

  • 데이터는 깨끗하고 간결하며 풍부해야 함
  • 중복 정보는 도움이 되지 않음
  • 중복은 모델을 편향·혼란시킬 수 있음
  • 고유 식별자를 기준으로 행/레코드 삭제 여부 판단

 

# Drop duplicate rows
heart_disease_duplicates_dropped = heart_disease_column_dropped.drop_duplicates()
엔드 투 엔드 Machine Learning

연습해 봅시다!

엔드 투 엔드 Machine Learning

Preparing Video For Download...