n-gram 모델 구축

Python으로 배우는 NLP 피처 엔지니어링

Rounak Banik

Data Scientist

BoW의 한계

review label
'The movie was good and not boring' positive
'The movie was not good and boring' negative

 

  • BoW 표현이 완전히 동일!
  • 단어의 문맥이 사라짐.
  • 감정은 'not'의 위치에 좌우됨.
Python으로 배우는 NLP 피처 엔지니어링

n-gram

  • 주어진 문서에서 연속된 n개 요소(단어) 시퀀스.
  • n = 1 → bag-of-words
    'for you a thousand times over'
    
  • n = 2, n-gram:
    [
    'for you',
    'you a',
    'a thousand',
    'thousand times',
    'times over'
    ]
    
Python으로 배우는 NLP 피처 엔지니어링

n-gram

'for you a thousand times over'
  • n = 3, n-gram:
    [
    'for you a',
    'you a thousand',
    'a thousand times',
    'thousand times over'
    ]
    
  • 더 많은 문맥을 포착.
Python으로 배우는 NLP 피처 엔지니어링

활용 분야

  • 문장 완성
  • 철자 교정
  • 기계번역 후교정
Python으로 배우는 NLP 피처 엔지니어링

scikit-learn으로 n-gram 모델 구축

바이그램만 생성.

bigrams = CountVectorizer(ngram_range=(2,2))

유니그램, 바이그램, 트라이그램 생성.

ngrams = CountVectorizer(ngram_range=(1,3))
Python으로 배우는 NLP 피처 엔지니어링

한계점

  • 차원의 저주
  • 높은 차수 n-gram은 드묾
  • n은 작게 유지
Python으로 배우는 NLP 피처 엔지니어링

Vamos praticar!

Python으로 배우는 NLP 피처 엔지니어링

Preparing Video For Download...