TF-IDF वेक्टराइज़ेशन

Python में Natural Language Processing (NLP)

Fouad Trad

Machine Learning Engineer

BoW से TF-IDF तक

  • BoW सभी शब्दों को समान महत्व देता है
  • TF-IDF इसे ठीक करता है, बताकर:
    • किसी दस्तावेज़ में शब्द कितनी बार आता है
    • पूरे कलेक्शन में वह शब्द कितना अर्थपूर्ण है

 

दो वाक्यों का BoW निरूपण दिखाती छवि: 'I love this NLP course' और 'I enjoyed this project.'

Python में Natural Language Processing (NLP)

TF-IDF

छवि दिखाती है कि TF-IDF, TF और IDF का गुणनफल है.

Python में Natural Language Processing (NLP)

TF-IDF

छवि दिखाती है कि TF-IDF, TF और IDF का गुणनफल है.

  • TF: Term Frequency
    • किसी दस्तावेज़ में शब्द कितनी बार आता है
Python में Natural Language Processing (NLP)

TF-IDF

छवि दिखाती है कि TF-IDF, TF और IDF का गुणनफल है.

  • TF: Term Frequency
    • किसी दस्तावेज़ में शब्द कितनी बार आता है
  • IDF: Inverse Document Frequency
    • सभी दस्तावेज़ों में वह शब्द कितना दुर्लभ है

 

  • शब्द एक दस्तावेज़ में दिखे, बाकी में नहीं → high score
  • शब्द हर दस्तावेज़ में दिखे → low score
Python में Natural Language Processing (NLP)

कोड के साथ 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 में Natural Language Processing (NLP)

कोड के साथ 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 में Natural Language Processing (NLP)

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']
Python में Natural Language Processing (NLP)

स्कोर को हीटमैप के रूप में देखें

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()

 

डेटासेट के TF-IDF निरूपण का हीटमैप.

Python में Natural Language Processing (NLP)

BoW से तुलना

 

डेटासेट के BoW निरूपण का हीटमैप.

 

डेटासेट के TF-IDF निरूपण का हीटमैप.

Python में Natural Language Processing (NLP)

अभ्यास करते हैं!

Python में Natural Language Processing (NLP)

Preparing Video For Download...