Über n-Gramme hinaus: Word Embeddings

Feature Engineering für NLP in Python

Rounak Banik

Data Scientist

Das Problem mit BoW und tf-idf

'I am happy'
'I am joyous'
'I am sad'
Feature Engineering für NLP in Python

Word Embeddings

  • Abbildung von Wörtern in einen n-dimensionalen Vektorraum
  • Erzeugt mit Deep Learning und riesigen Datenmengen
  • Erkennt, wie ähnlich zwei Wörter sind
  • Nutzt zur Erkennung von Synonymen und Antonymen
  • Erfasst komplexe Beziehungen
    • King-QueenMan-Woman
    • France-ParisRussia-Moscow
  • Abhängig vom spaCy-Modell; unabhängig vom verwendeten Datensatz
Feature Engineering für NLP in Python

Word Embeddings mit spaCy

import spacy

# Modell laden und Doc-Objekt erstellen
nlp = spacy.load('en_core_web_lg')
doc = nlp('I am happy')
# Wortvektoren für jedes Token erzeugen
for token in doc:
  print(token.vector)
[-1.0747459e+00  4.8677087e-02  5.6630421e+00  1.6680446e+00
 -1.3194644e+00 -1.5142369e+00  1.1940931e+00 -3.0168812e+00
 ...
Feature Engineering für NLP in Python

Wortähnlichkeiten

doc = nlp("happy joyous sad")
for token1 in doc:
  for token2 in doc:
    print(token1.text, token2.text, token1.similarity(token2))
happy happy 1.0
happy joyous 0.63244456
happy sad 0.37338886
joyous happy 0.63244456
joyous joyous 1.0
joyous sad 0.5340932
...
Feature Engineering für NLP in Python

Dokumentähnlichkeiten

# Doc-Objekte erzeugen
sent1 = nlp("I am happy")
sent2 = nlp("I am sad")
sent3 = nlp("I am joyous")
# Ähnlichkeit zwischen sent1 und sent2 berechnen
sent1.similarity(sent2)
0.9273363837282105
# Ähnlichkeit zwischen sent1 und sent3 berechnen
sent1.similarity(sent3)
0.9403554938594568
Feature Engineering für NLP in Python

Lass uns üben!

Feature Engineering für NLP in Python

Preparing Video For Download...