TF-IDF vektörleştirme

Python ile Natural Language Processing (NLP)

Fouad Trad

Machine Learning Engineer

BoW'dan TF-IDF'ye

  • BoW tüm sözcükleri eşit önemli sayar
  • TF-IDF bunu şunları belirterek düzeltir:
    • bir sözcüğün belgede kaç kez geçtiği
    • o sözcüğün derleme genelinde ne kadar anlamlı olduğu

 

İki cümlenin BoW temsili: 'I love this NLP course' ve 'I enjoyed this project.'

Python ile Natural Language Processing (NLP)

TF-IDF

TF-IDF'nin TF ve IDF'nin çarpımı olduğunu gösteren görsel.

Python ile Natural Language Processing (NLP)

TF-IDF

TF-IDF'nin TF ve IDF'nin çarpımı olduğunu gösteren görsel.

  • TF: Terim Frekansı
    • Bir sözcüğün bir belgede kaç kez geçtiği
Python ile Natural Language Processing (NLP)

TF-IDF

TF-IDF'nin TF ve IDF'nin çarpımı olduğunu gösteren görsel.

  • TF: Terim Frekansı
    • Bir sözcüğün bir belgede kaç kez geçtiği
  • IDF: Ters Belge Frekansı
    • O sözcüğün tüm belgelerde ne kadar nadir olduğu

 

  • Sözcük bir belgede var, diğerlerinde yok → yüksek skor
  • Sözcük her belgede var → düşük skor
Python ile Natural Language Processing (NLP)

Kod ile TF-IDF

reviews = [
    "I loved the movie. It was amazing!",
    "The movie was okay.",
    "I hated the movie. It was boring."
]

cleaned_reviews = [preprocess(review) for review in reviews] print(cleaned_reviews)
['i loved the movie it was amazing', 
 'the movie was okay', 
 'i hated the movie it was boring']
Python ile Natural Language Processing (NLP)

Kod ile TF-IDF

from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer()
tfidf_matrix = vectorizer.fit_transform(cleaned_reviews)
print(tfidf_matrix)
<Compressed Sparse Row sparse matrix of dtype 'float64'
    with 16 stored elements and shape (3, 9)>
Python ile Natural Language Processing (NLP)

TF-IDF çıktısı

print(tfidf_matrix.toarray())
[[0.52523431 0.         0.         0.39945423 0.52523431 0.31021184   0.         0.31021184 0.31021184]
 [0.         0.         0.         0.         0.         0.41285857   0.69903033 0.41285857 0.41285857]
 [0.         0.52523431 0.52523431 0.39945423 0.         0.31021184   0.         0.31021184 0.31021184]]
vectorizer.get_feature_names_out()
['amazing' 'boring' 'hated' 'it' 'loved' 'movie' 'okay' 'the' 'was']
Python ile Natural Language Processing (NLP)

Skorları ısı haritası ile görselleştirme

import pandas as pd

df_tfidf = pd.DataFrame(

tfidf_matrix.toarray(),
columns=vectorizer.get_feature_names_out() )
import seaborn as sns
import matplotlib.pyplot as plt

sns.heatmap(df_tfidf, annot=True)
plt.title("İncelemeler arasında TF-IDF skorları") plt.xlabel("Terimler") plt.ylabel("Belgeler") plt.show()

 

Veri kümesinin TF-IDF temsiline ait ısı haritası.

Python ile Natural Language Processing (NLP)

BoW ile karşılaştırma

 

Veri kümesinin BoW temsiline ait ısı haritası.

 

Veri kümesinin TF-IDF temsiline ait ısı haritası.

Python ile Natural Language Processing (NLP)

Hadi pratik yapalım!

Python ile Natural Language Processing (NLP)

Preparing Video For Download...