简体   繁体   中英

use scikit learn tfidf vectorizer starting from counts data frame

I have a pandas data frame with counts of words for a series of documents. Can I apply sklearn.feature_extraction.text.TfidfVectorizer to it to return a term-document matrix?

import pandas as pd

a = [1,2,3,4]
b = [1,3,4,6]
c = [3,4,6,1]

df = pd.DataFrame([a,b,c])

How can I get tfidf version of counts in df?

like this:

from sklearn.feature_extraction.text import TfidfTransformer
tfidf =TfidfTransformer(norm=u'l2', use_idf=True, smooth_idf=True, sublinear_tf=False)
data =tfidf.fit_transform(df.values)

This returns a sparse matrix of the tfidf values. You can turn them into a dense and put them back into a data frame like this:

pd.DataFrame(data.todense())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM