Vector hóa TF-IDF

Natural Language Processing (NLP) bằng Python

Fouad Trad

Machine Learning Engineer

Từ BoW đến TF-IDF

  • BoW coi mọi từ đều quan trọng như nhau
  • TF-IDF khắc phục bằng cách cho biết:
    • tần suất một từ xuất hiện trong tài liệu
    • mức độ có ý nghĩa của từ trên toàn bộ tập tài liệu

 

Hình minh họa biểu diễn BoW cho hai câu: 'I love this NLP course' và 'I enjoyed this project.'

Natural Language Processing (NLP) bằng Python

TF-IDF

Hình cho thấy TF-IDF là tích của TF và IDF.

Natural Language Processing (NLP) bằng Python

TF-IDF

Hình cho thấy TF-IDF là tích của TF và IDF.

  • TF: Term Frequency
    • Số lần một từ xuất hiện trong tài liệu
Natural Language Processing (NLP) bằng Python

TF-IDF

Hình cho thấy TF-IDF là tích của TF và IDF.

  • TF: Term Frequency
    • Số lần một từ xuất hiện trong tài liệu
  • IDF: Inverse Document Frequency
    • Mức độ hiếm của từ trên toàn bộ tập tài liệu

 

  • Từ xuất hiện trong một tài liệu, không có ở tài liệu khác → điểm cao
  • Từ xuất hiện ở mọi tài liệu → điểm thấp
Natural Language Processing (NLP) bằng Python

TF-IDF với mã

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']
Natural Language Processing (NLP) bằng Python

TF-IDF với mã

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)>
Natural Language Processing (NLP) bằng Python

Đầu ra TF-IDF

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']
Natural Language Processing (NLP) bằng Python

Trực quan hóa điểm bằng heatmap

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("TF-IDF Scores Across Reviews") plt.xlabel("Terms") plt.ylabel("Documents") plt.show()

 

Bản đồ nhiệt biểu diễn TF-IDF của tập dữ liệu.

Natural Language Processing (NLP) bằng Python

So sánh với BoW

 

Bản đồ nhiệt biểu diễn BoW của tập dữ liệu.

 

Bản đồ nhiệt biểu diễn TF-IDF của tập dữ liệu.

Natural Language Processing (NLP) bằng Python

Ayo berlatih!

Natural Language Processing (NLP) bằng Python

Preparing Video For Download...