Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data Wrangling-Dangling

Buzzvil
August 18, 2021

Data Wrangling-Dangling

By Venzino

Buzzvil

August 18, 2021
Tweet

More Decks by Buzzvil

Other Decks in Programming

Transcript

  1. Dev Seminar
    Data Wrangling-Dangling
    VenZiNo
    Copyright ⓒ All Right Reserved by Buzzvil

    View Slide

  2. Copyright ⓒ All Right Reserved by Buzzvil
    Motivation
    1. 데이터를 다루는 방법에 대해 소개
    2. 버즈빌의 데이터를 좀더 들여다 보고 싶었다.
    3. 데이터 관련된 툴 소개하고 싶었음
    4. 다른분들을 자극해서 더 많은 데이터 프러덕을 ...

    View Slide

  3. Copyright ⓒ All Right Reserved by Buzzvil
    SQL vs. Python
    SQL
    ● 다양한 라이브러리
    ● 높은 자유도
    ● local 환경에서 작업시 메모리 제약
    Python
    둘다 쓰긴 써야함…
    ● Bigdata 다룰때 빠르다
    ● DB환경에 영향을 받음
    ● Debugging이 어려움

    View Slide

  4. Copyright ⓒ All Right Reserved by Buzzvil
    EDA
    ● 데이터를 다양한 각도에서 관찰하고 이해
    ● 데이터의 분포를 관찰하여 데이터가 보여주는 현상을 이해
    ● 다양한 각도에서 살펴봄으로써 패턴을 발견할 수 있음 → 새로운 가설을 수립하거나
    기존의 가설 수정
    ● 통계분석, 시각화, 이상치, 결측치 확인 등의 방법 적용

    View Slide

  5. Copyright ⓒ All Right Reserved by Buzzvil
    분석할때 생기는 다양한 니즈
    ● DB내의 데이터에 복잡한 함수를 쓰고싶다
    ● Athena/S3/Redshift 데이터를 한번에 보고싶다
    ● DB client SQL → CSV 다운로드 → Jupyter notebook python 처리
    ● 테이블만 보는거 말고 시각화도 해보고 싶다

    View Slide

  6. Copyright ⓒ All Right Reserved by Buzzvil
    Pandas 를 AWS 인프라와 함께 사용할 수 있다면?

    View Slide

  7. Copyright ⓒ All Right Reserved by Buzzvil
    Pandas 를 aws 인프라에서 쓸 수 있다면?
    https://github.com/awslabs/aws-data-wrangler

    View Slide

  8. Copyright ⓒ All Right Reserved by Buzzvil
    AWS Data Wrangler
    ● S3 - CSV, Parquet, Json ..
    ● Redshift, MySQL, PostgreSQL
    ● Athena
    ● EMR
    ● Dynamo DB
    다양한 데이터를 Pandas Dataframe 으로 처리 할 수 있다.

    View Slide

  9. Copyright ⓒ All Right Reserved by Buzzvil
    Setup
    ● Jupyter notebook - VS code 에서도 사용 가능
    ○ jupyter extension 설치
    ○ python interpreter 설정 (command palette)
    ● AWS data wrangler 설치 (pip install awswrangler)
    ○ padas, boto3 등 관련 라이브러리도 필요
    ● AWS credential 설정
    ● Glue Connection 생성 (Redshift)

    View Slide

  10. Copyright ⓒ All Right Reserved by Buzzvil
    S3 : Read Write parquet file
    ● 단일 혹은 복수의 parquet file 을 하나의 dataframe 으로 받을 수 있음

    View Slide

  11. Copyright ⓒ All Right Reserved by Buzzvil
    S3 : Read Write parquet file
    ● info(), describe() 등의 메소드를 통해 요약된 정보를 볼 수 있음

    View Slide

  12. Copyright ⓒ All Right Reserved by Buzzvil
    S3 : Read Write parquet file
    ● Dataframe 을 S3 에 파일로 업로드 할 수 있음

    View Slide

  13. Copyright ⓒ All Right Reserved by Buzzvil
    Redshift read/write
    ● Redshift 접속시, session region 설정 필요
    ● Glue connection 을 사용해 DB에 접속
    ● 쿼리 결과를 Dataframe 형태로 리턴해준다.
    ● MySQL, Postgresql 에도 동일한 방식으로 사용
    ● Redshift 의 copy/unload 커멘드 수행 가능

    View Slide

  14. Copyright ⓒ All Right Reserved by Buzzvil
    Athena Query
    ● Athena 쿼리 결과를 Dataframe 으로 받아올 수 있음

    View Slide

  15. Copyright ⓒ All Right Reserved by Buzzvil
    Pandas : manage time series data
    date_range, period_range : 시간 범위를 생성

    View Slide

  16. Copyright ⓒ All Right Reserved by Buzzvil
    Pandas : manage time series data
    shift : 인덱스를 고정하고 데이터를 이동, Lead/Lag

    View Slide

  17. Copyright ⓒ All Right Reserved by Buzzvil
    Pandas : manage time series data
    resample : 시간 간격을 조정하여 up/down sampling을 수행

    View Slide

  18. Copyright ⓒ All Right Reserved by Buzzvil
    Pandas : manage time series data
    resample.ohlc 매서드 : 구간의 open, high, low, close 값을 구할 수 있다

    View Slide

  19. Copyright ⓒ All Right Reserved by Buzzvil
    Pandas : manage time series data
    date_range 를 index로 사용할 경우, 다양한 방식으로 필터링 할 수 있다.

    View Slide

  20. Copyright ⓒ All Right Reserved by Buzzvil
    Pandas : manage time series data
    rolling : window function 과 유사한 역할을 수행

    View Slide

  21. Copyright ⓒ All Right Reserved by Buzzvil
    Plotly : MZ세대 시각화 국룰
    플랏틀니
    ?

    View Slide

  22. Copyright ⓒ All Right Reserved by Buzzvil
    Plotly : interactive visualization tool
    ● 다양한 인터렉션 옵션 제공
    ● Zoom-In, Zoom-out
    ● 이미지 파일 저장하기
    ● 영역 지정 및 확대하기, 좌표 찾기
    ● 미리 준비된 스타일 템플릿 적용 가능
    ● pip install plotly
    ● jupyter notebook, colab 에서 사용가능

    View Slide

  23. Copyright ⓒ All Right Reserved by Buzzvil
    Plotly : 직관적, 다양한 옵션

    View Slide

  24. Copyright ⓒ All Right Reserved by Buzzvil
    Plotly : 3D plot

    View Slide

  25. Copyright ⓒ All Right Reserved by Buzzvil
    Unit 데이터 EDA
    ● 6월 1일 - 8월 14일 유닛별 imp, click, conv 수를 시간 단위로 집계
    ● 전체 unit 의 imp, click, conv 합

    View Slide

  26. Copyright ⓒ All Right Reserved by Buzzvil
    Unit 데이터 EDA
    ● 일부 unit 이 대다수의 트래픽을 차지

    View Slide

  27. Copyright ⓒ All Right Reserved by Buzzvil
    Unit 데이터 EDA
    ● 1일 단위의 시계열 패턴이 나타남 ( conversion 기준 상위 5개 유닛)

    View Slide

  28. Copyright ⓒ All Right Reserved by Buzzvil
    Unit 데이터 EDA
    ● impression, click, conversion 간의 상관관계를 그래프로 확인

    View Slide

  29. Copyright ⓒ All Right Reserved by Buzzvil
    Unit 데이터 EDA
    ● 405251747491406 unit 의 데이터가 상대적으로 trend가 적어보임 → 패턴 기반의 예측이 쉽지 않을까?

    View Slide

  30. Copyright ⓒ All Right Reserved by Buzzvil
    특정 유닛의 클릭 수를 예측해보자
    ● unit click, imp 에서 1일 단위의 패턴 확인
    ● 이전 데이터를 기반으로 다음 단위시간의 클릭 수 를 예측 할 수 있을까?
    ● 소진력 등을 논할때 도움이 될 수 있을지도?

    View Slide

  31. Copyright ⓒ All Right Reserved by Buzzvil
    XGBoost
    ● 트리기반의 부스팅 모델
    ● 여러개의 트리를 만들어 앙상블 하는 방식으로
    학습
    ● kaggle 에서 인기가 많다
    ● Regression 도 되고, Classification 도 된다
    ● 딥러닝을 쓰기엔 row 수가 적음

    View Slide

  32. Copyright ⓒ All Right Reserved by Buzzvil
    학습데이터 준비
    ● 이전 단위시간*n 으로 다음 단위시간 예측
    ● 직전값에 영향을 받는것을 방지하기 위해 24시간
    간격을 둠
    ● 날짜, 시간, 요일, 평균, 최대, 최소 등 변수 추가
    ● trainset, testset 구분 시, 무작위가 아닌 시간에 따라
    분류
    ● 3960 * 58 의 늠름한 데이터셋
    x y
    8.4
    8.1 ~ 8.2
    train test

    View Slide

  33. Copyright ⓒ All Right Reserved by Buzzvil
    학습 / 평가
    MAPE : 8.39

    View Slide

  34. Copyright ⓒ All Right Reserved by Buzzvil
    학습 / 평가
    ● 48시간 데이터를 사용할 경우, MAPE : 8.74
    ● 일, 요일, 시, 평균, 최소, 24시간전 데이터 등이 가장 주요한 변수로 나옴
    ● imp 는 거의 사용되지 않음
    ● xgboost 라이브러리 내 plot_importance, plot_tree 등을 활용

    View Slide

  35. Copyright ⓒ All Right Reserved by Buzzvil
    ARIMA 모델과 비교
    XGBoost
    MAPE : 8.39
    ARIMA
    MAPE : 8.23

    View Slide

  36. Copyright ⓒ All Right Reserved by Buzzvil
    느낀점
    ● 제한된 데이터를 보는 대회들과 다르게, 들여다 봐야할 데이터가 많다
    ● 다양한 디멘션으로 고려해야할 사항들이 많다 → 패턴을 찾는 것이 쉽지 않음
    ● EDA를 할때마다 고구마 줄기처럼 나오는 질문들
    ● 도메인 지식과 데이터 지식이 둘다 필요하다
    ● Digging은 언제나 즐겁다

    View Slide

  37. Copyright ⓒ All Right Reserved by Buzzvil
    Reference
    ● https://towardsdatascience.com/basic-time-series-manipulation-with-pandas-4432afee64ea
    ● https://machinelearningmastery.com/xgboost-for-time-series-forecasting/
    ● https://acdongpgm.tistory.com/102
    ● https://aws-data-wrangler.readthedocs.io/en/2.10.0/tutorials.html
    ● https://plotly.com/python/
    ● https://seong6496.tistory.com/132

    View Slide

  38. Thank you!!
    Copyright ⓒ All Right Reserved by Buzzvil

    View Slide