본문 바로가기
Study (Data Science)/ML

Day2. 데이터 전처리

by 콜라찡 2022. 12. 5.

 

10.2 null 값 처리하기

  1. 결측치(Missing Data)
    • 결측치가 있는 데이터를 제거한다.
    • 결측치를 어떤 값으로 대체한다
      • 특정값 지정 : .fillna() 
        • but, 결측치가 많은 경우, 모두 같은 값으로 대체한다면 데이터의 분산이 실제보다 작아지거나 편향되어 문제가 될 수 있음.
      • 평균, 중앙값등으로 대체 :  .fillna(df.mean())
        • 1특정 값으로 대체했을 때와 마찬가지로 결측치가 많은 경우 데이터의 분산이 실제보다 작아지는 문제가 발생할 수 있음. 
      • 다른 데이터를 이용해 예측값으로 대체할 수 있습니다. 예를 들어 머신러닝 모델로 2020년 4월 미국의 예측값을 만들고, 이 값으로 결측치를 보완할 수 있습니다.
      • 시계열 특성을 가진 데이터의 경우 앞뒤 데이터를 통해 결측치를 대체할 수 있습니다. 예를 들어 기온을 측정하는 센서 데이터에서 결측치가 발생할 경우, 전후 데이터의 평균으로 보완할 수 있습니다.
# .fillna()
fill_Dividend_df = sample_df.copy()
fill_Dividend_df['Dividend'] = sample_df['Dividend'].fillna(0)
fill_Dividend_df

# .fillna(df.mean())
fill_df = fill_Dividend_df.fillna(fill_Dividend_df.mean())
fill_df.iloc[222:225]

# groupby()응용
# x값을 lambda로 x의 mean값으로 채움
fill_Dividend_df.groupby('Category').mean()
fill_RoePer_df = fill_Dividend_df.groupby('Category').apply(lambda x: x.fillna(x.mean()))
fill_RoePer_df

https://brain-nim.tistory.com/20

 

3. 판다스를 활용한 Null값 대체(특정값, 평균, 특정함수)

지난 포스트에서는 데이터에 존재하는 결측값(Null값)을 일방적으로 제거했습니다. 2. 판다스를 활용한 Null값 제거 (결측행, 결측열 제거) 지난번 다루었던 주식데이터, stock_market.csv의 몇 칼럼에

brain-nim.tistory.com

10.4 첨부파일 참고 

(221205) z-score로 이상치 제거 후 histogram 변화추이.ipynb
0.05MB

10.5.1 Standardization(Z-score), Normalization(MinMax Scaler)

  • Standardization으로 이상치의 값을 추려내고,
  • Normalization으로 값을 스케일링.
  • Normalization != MinMax Scaler, Normalilzation 안에 하나의 방법으로 Min-Max Scaler가 들어가는 것.
  • 번외) Regularization : 제약을 거는 것. (e.g. L1, L2, Regularization)
  • z-score는 n이 10 이하이면 사용이 거의 불가함. 표본이 되기가 힘듬.

참고) https://heeya-stupidbutstudying.tistory.com/entry/%ED%86%B5%EA%B3%84-%EC%A0%95%EA%B7%9C%ED%99%94%EC%99%80-%ED%91%9C%EC%A4%80%ED%99%94

 

[통계] 정규화(Normalization) vs 표준화(Standardization)

ML을 공부하는 사람이라면 feature scaling이 얼마나 중요한 지 알것이다. scikit-learn에는 많은 스케일링 메서드들이 모듈화 되어있는데, 기본적으로 정규화와 표준화가 무엇인지 이해해야 과제를 수

heeya-stupidbutstudying.tistory.com

https://iq.opengenus.org/standardization-regularization-vs-normalization/

 

Differences between Standardization, Regularization, Normalization in ML

We have covered the Differences between Standardization, Regularization, Normalization in depth along with the introductory knowledge and complete explanation of the key terms.

iq.opengenus.org

 

https://m.blog.naver.com/qbxlvnf11/221476122182

 

머신 러닝 - Normalization, Standardization, Regularization 비교

Normalization, Standardization, Regularization의 공통점은 'Overfitting을 방지하기 위해 사용...

blog.naver.com

https://machinelearningmastery.com/standardscaler-and-minmaxscaler-transforms-in-python/

 

10.5.2 MinMax Scaler 

train과 test 모두 minmax scaler를 동일하게 하지 않으면, 

값의 편차가 너무 커서 정확한 예측이 힘듬.

 

10.5.3 그밖의 Scaler들 

scaler 종류들

https://mingtory.tistory.com/140

 

[Python/sklearn] Scaler 별 특징 / 사용법 / 차이 / 예시

Scaler 각 종류에 대해서 알아보기 이 전에 Scaler를 사용하는 이유에 대해 먼저 알아보자면 데이터가 가진 크기과 편차가 다르기 때문에 한 피처의 특징을 너무 많이 반영하거나 패턴을 찾아내는

mingtory.tistory.com

sklearn MinMax Scaler 사용법

https://zephyrus1111.tistory.com/218

 

[Scikit-Learn] 2. 최대 최소(Min Max) 변환하기 feat. MinMaxScaler

예측 모델링을 할 때 학습 데이터에 대하여 변환하는 경우가 굉장히 많다. 그중에서 각 데이터를 특정 범위로 제한시키는 Min Max 변환을 많이 사용한다. 최대 최소(Min Max) 변환은 원 데이터를 우

zephyrus1111.tistory.com

 

 

10.6. One-Hot Encoding

  • How to Conver Categorical Data to Numerical Data
    • Integer Encoding
    • One-Hot Encoding
  • Many machine learning algorithms cannot operate on label data directly. They require all input variables and output variables to be numeric.
    • a decision tree can be learned directly from categorical data with no data transform required (this depends on the specific implementation)

 

https://john-analyst.medium.com/%EB%8D%B0%EC%9D%B4%ED%84%B0-%EC%A0%84%EC%B2%98%EB%A6%AC-%EB%A0%88%EC%9D%B4%EB%B8%94-%EC%9D%B8%EC%BD%94%EB%94%A9%EA%B3%BC-%EC%9B%90%ED%95%AB-%EC%9D%B8%EC%BD%94%EB%94%A9-f0220df21df1

 

데이터 전처리 : 레이블 인코딩과 원핫 인코딩

기본적으로 사이킷런의 머신러닝 알고리즘은 문자열 값을 입력 값으로 허락하지 않는다.

john-analyst.medium.com

https://machinelearningmastery.com/why-one-hot-encode-data-in-machine-learning/

 

 

(참고) 머신러닝 모델링의 흔한 실수들

https://jehyunlee.github.io/2021/09/02/Python-DS-83-aifestival2021/210902_%EC%9D%B4%EC%A0%9C%ED%98%84_%EB%A8%B8%EC%8B%A0%EB%9F%AC%EB%8B%9D%EB%AA%A8%EB%8D%B8%EB%A7%81%ED%9D%94%ED%95%9C%EC%8B%A4%EC%88%982.pdf

 

728x90

'Study (Data Science) > ML' 카테고리의 다른 글

Batch size, Epoch, Iteration  (0) 2022.12.06
여러가지 머신러닝 모델들  (0) 2022.12.06
Day1. 데이터, 데이터베이스  (0) 2022.12.04
EDA (Explratory Data Analysis)  (1) 2022.12.02
Matplotlib  (0) 2022.12.02

댓글