【发布时间】:2019-01-10 03:01:52
【问题描述】:
我想从下面的文档中分别计算 tf 和 idf。我正在使用 python 和 pandas。
import pandas as pd
df = pd.DataFrame({'docId': [1,2,3],
'sent': ['This is the first sentence','This is the second sentence', 'This is the third sentence']})
我想使用不使用 Sklearn 库的 Tf-Idf 公式计算。
标记化后,我已将其用于 TF 计算:
tf = df.sent.apply(pd.value_counts).fillna(0)
但这给了我一个计数,但我想要(count/total number of words)的比率。
对于 IDF:
df[df['sent'] > 0] / (1 + len(df['sent'])
但它似乎不起作用。 我希望 Tf 和 Idf 都为 pandas 系列格式。
编辑
对于标记化,我使用了df['sent'] = df['sent'].apply(word_tokenize)
我的 idf 分数为:
tfidf = TfidfVectorizer()
feature_array = tfidf.fit_transform(df['sent'])
d=(dict(zip(tfidf.get_feature_names(), tfidf.idf_)))
如何分别获得 tf 分数?
【问题讨论】:
-
请解释您是如何标记化的。另外,您是否认为每个句子都有自己的文档?
-
@T.Ray 检查我编辑的问题。我计算了 idf 分数。我只想要每个标记化单词的 tf 分数。
-
我很困惑。我以为你不想使用
sklearn?
标签: python python-3.x pandas tf-idf tfidfvectorizer