n-gram-Modelle erstellen

Feature Engineering für NLP in Python

Rounak Banik

Data Scientist

BoW: Schwächen

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

 

  • Exakt dieselbe BoW-Darstellung!
  • Kontext der Wörter geht verloren.
  • Stimmung hängt von der Position von 'not' ab.
Feature Engineering für NLP in Python

n-Gramme

  • Zusammenhängende Folge von n Elementen (Wörtern) in einem Dokument.
  • n = 1 → bag-of-words
    'for you a thousand times over'
    
  • n = 2, n-grams:
    [
    'for you',
    'you a',
    'a thousand',
    'thousand times',
    'times over'
    ]
    
Feature Engineering für NLP in Python

n-Gramme

'for you a thousand times over'
  • n = 3, n-grams:
    [
    'for you a',
    'you a thousand',
    'a thousand times',
    'thousand times over'
    ]
    
  • Erfasst mehr Kontext.
Feature Engineering für NLP in Python

Anwendungen

  • Satzergänzung
  • Rechtschreibkorrektur
  • Korrektur in der maschinellen Übersetzung
Feature Engineering für NLP in Python

n-gram-Modelle mit scikit-learn erstellen

Erzeugt nur Bigramme.

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

Erzeugt Unigramme, Bigramme und Trigramme.

ngrams = CountVectorizer(ngram_range=(1,3))
Feature Engineering für NLP in Python

Schwächen

  • Fluch der Dimensionalität
  • Höhergradige n-Gramme sind selten
  • n klein halten
Feature Engineering für NLP in Python

Lass uns üben!

Feature Engineering für NLP in Python

Preparing Video For Download...