머신러닝 알고리즘들을 책이나 강의 등을 통해 공부한 뒤 실제로 무언가를 해보려고 했을 때에 맞닿게 되는 답답함 같은 게 있을 겁니다. 그것은 학습한 이론이 프로그래밍 언어 또는 도구로 연결되지 않기 때문일 수도 있지만, 일상에서 얻은 데이터가 책이나 강의에서 사용된 예제들처럼 잘 가공되어 피쳐화(feature) 되어있지 않거나 레이블(label) 정보가 없기 때문일 수도 있습니다.
본 발표는 19대 국회의원들의 의안표결결과, 회의록 내용, 그리고 정치자금 사용내역 등의 RAW 데이터들을 소재로 Python 을 사용하여 데이터를 가공하고 피쳐화하는 데서부터 모델링 까지의 과정을 다룹니다. 레이블 정보가 없을 때 유용한 클러스터링 기법들로 다양한 내용과 형태의 데이터들을 다루게 됩니다.
발표에서 사용할 데이터는 국회에서 공개된 내용을 크롤링하였거나 언론사에서 공개한 객관적인 자료들입니다. 이를 바탕으로 분석 기술들을 시연할 뿐, 정치적 내용은 포함하고 있지 않습니다.
Pre-requisites
- Python 문법에 대한 익숙한 이해.
- Pandas 또는 Numpy 의 기초적 사용법.
Take-away items
- 머신러닝 과정의 기초적 이해
- 머신러닝을 위한 다양한 형태의 데이터 처리 방법 in Python
- 머신러닝의 과정에서 어떤 Python 도구들이 어떻게 사용되는지 소개
사용 도구
- 시연 : Jupyter notebook
- 분석 : Pandas, Numpy, SciPy, Sci-Kit Learn, Spark
- 시각화 : matplotlib, Lightning Viz
개요
1. 의안표결 과 결과
1.1. 기초적인 데이터 전처리
1.2. 의원별 표결내용 군집화
1.3. 차원 축소 및 시각화
2. 정치자금 사용내역
2.1. 다양한 도메인의 데이터 전처리
2.2. 의원별 소비성향 클러스터링 및 시각화
3. 국회 회의록 내용
3.1. 텍스트 데이터에 대한 전처리
3.2. 텍스트 데이터의 피쳐화
3.3. 국회 회의록 클러스터링
3.4. 차원 축소 및 시각화
2. Aug 14, 2016
Who am I?
● Research Engineer at fintech startup.
– Fraud Detection System based on Machine
Learning techniques.
● Previous Employment
– Software Center at LG Electronics
– IT headquarters at KT
– Daum Communications
– NURI Telecom
3. Aug 14, 2016
질문
● 내가 만든 Python 코드는 대게 일회용이다 .
● Python 또는 다른 언어로 ML 프로그래밍을 해봤다 .
● Numpy / Pandas 의 slicing, indexing 에 익숙하다 .
● lambda expression 을 많이 사용하고 있다 .
4. Aug 14, 2016
Machine Learning
● Algorithm → learn data → model → prediction
– “The ability to learn without being explicitly
programmed”. (Authur Lee Samuel)
– Set of algorithms that can learn from and
perform predictive analysis on data.
– Such algorithms operate by building a model
from an example training set of input
observations in order to make data-driven
predictions or decisions expressed as outputs,
rather than following strictly static program
instructions.
5. Aug 14, 2016
ML Process
● Featurization → Feature Vectors
– Analyze Data
● Understand the information available that will be used to
develop a model.
– Prepare Data
● Discover and expose the structure in the dataset.
● Train Model → Model
● Evaluate Model
– Develop a robust test harness and baseline accuracy from
which to improve and spot check algorithms.
● Improve Results
– Leverage results to develop more accurate models.
6. Aug 14, 2016
ML Process
이름 특징 (feature) 성별
철수 넥타이 , 안경 , 수염 , 대머리 남
영희 스커트 , 안경 , 핸드백 , 긴머리 여
근혜 스커트 , 핸드백 , 닭머리 여
두환 군복 , 대머리 , 안경 남
홍주 긴머리 , 수염 남
… … …
Feature Vectors
(1, 0, 0, 0, 1, 0, 1, 1, 0)
(0, 1, 0, 1, 1, 1, 0, 0, 0)
(0, 1, 0, 0, 0, 1, 0, 0, 1)
(0, 1, 1, 0, 1, 0, 1, 0, 0)
(0, 0, 0, 1, 0, 0, 0, 1, 0)
(…)
Data Feature vectors Model(featurizing) (training)
7. Aug 14, 2016
What we don’t
● !Crawling
– already done and ready
– Not in this scope
– Thanks to 참여연대 , 팀포퐁 , OhmyNews
● !Evaluation
– We are not focusing on results but the
process.
● !Improving
– Just one cycle with no turning back.
8. Aug 14, 2016
What we do
● Featurization
– Analyze / Prepare data
– Pandas
● Train a model
– Unsupervised Learning : K-Means Clustering
– Scikit Learn, PySpark
● Presentation
– Matplotlib, LightingViz
● Subject
– 19 대 국회 표결 결과 / 정치자금 사용내역 / 국회 회의록
9. Aug 14, 2016
19 대 국회 의안표결
● Featurization
● Train model
– K-Means Clustering
● Presentation
10. Aug 14, 2016
19 대 국회 의안표결
● Train model
– K-Means Clustering
11. Aug 14, 2016
19 대 국회 의안표결
● https://github.com/midnightradio/pycon-apac-2016
12. Aug 14, 2016
19 대 국회 정치자금 사용내역
● Featurization
– Normalization / Standardizing
– Skewness
– Outliers
● Train model
● Presentation
13. Aug 14, 2016
19 대 국회 정치자금 사용내역
● Featurization
– Normalization / Standardizing
● Distance computation in k-means weights
each dimension equally and hence care must
be taken to ensure that unit of dimension
shouldn’t distort relative nearness of
observations.
14. Aug 14, 2016
19 대 국회 정치자금 사용내역
● Featurization
– Skewness
● Leads bias
15. Aug 14, 2016
19 대 국회 정치자금 사용내역
● Featurization
– Outliers
● Weakness of centroid based clustering (k-
means)
outlier outlier
16. Aug 14, 2016
19 대 국회 정치자금 사용내역
● Featurization
– Outliers
● IQR (Inter Quartile Range)
● 2-5 Standard Deviation
– Chicken and Eggs
● MAD (Median Absolute Deviation)
– Christophe Leys (2014). Detecting outliers: Do
not use standard deviation around the mean, use
absolute deviation around the median. Journal of
Experimental Social Psychology
17. Aug 14, 2016
19 대 국회 정치자금 사용내역
● https://github.com/midnightradio/pycon-apac-2016
18. Aug 14, 2016
19 대 국회 회의록
● Featurization
– Preprocessing
● Sentence Segmentation
– Can skip to tokenziation depending on use case
● Tokenization
– Find individual words
● Lemmatization
– Find the base of words (stemming)
● Removing stop words
– “the”, “a”, “is”, “at”, etc
● Some other refinements are needed occasionally.
● Vectorization
– We take an array of floating point values
19. Aug 14, 2016
19 대 국회 회의록
● Featurization
– Text vectorization
● How can we compose a feature vector of numbers
representing a text document of words?
– Bag of Words
– TF-IDF
● Each word in a text would be a dimension.
● How big would that vector be?
– Would it be as big as vocabulary of an article?
– Would it be as big as vocabulary of “all” article?
– Why not all vocabulary in the language?
20. Aug 14, 2016
19 대 국회 회의록
● Featurization
– Word vectorization
● Vector representation of words in a latent
context
● Learn semantic relations of words in documents
● ex) “I ate ____ in Korea”
– with a context “eat” it can be DimSum, Spagetti, or
KimChi without considering the place Korea.
– with a context “Korea” it can be Seoul, K-pop, which
represents the place better without considering
context “eating”.
21. Aug 14, 2016
19 대 국회 회의록
● https://github.com/midnightradio/pycon-apac-2016