Search code examples
pythontf-idf

Get individual sentences from TF-IDF


so I used tf-idf on a bunch of sentence, but now I would like to get the TF-IDF vector for an individual sentence back. This is my code. How do I now get the first item in the text matrix?

tfidf = TfidfVectorizer(
min_df = 5,
max_df = 0.95,
max_features = 8000
)

tfidf.fit(df_m_a['text_final'])
text = tfidf.transform(df_m_a['text_final'])

Solution

  • You have already called fit with tfidf.fit(df_m_a['text_final']). Hence the variable tfidf contains the TF-IDF vectorizer, with the last part of code text = tfidf.transform(df_m_a['text_final']) you will get the transformed vector, you should get what you want with text[0]