如何从某个日期获取某个单词的TFIDF?

2024-06-08 02:48:09 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以使用python计算某个单词在某个日期的tf idf度量


我想使用日期2008-01-022008-01-05显示单词的tf idf度量

index  date         comment                                                  age
0      2008-01-02   I love dogs, I think dogs are the best.                  22
1      2008-01-03   I can't have cats because of my allergy. I like cats.    19
2      2008-01-04   This is awesome.                                         25
3      2008-01-05   I want a dog.                                            35

Tags: theagedateindex度量tfcomment单词
1条回答
网友
1楼 · 发布于 2024-06-08 02:48:09

您需要使用像gensim这样的NLP库。您可以在TF-IDF页面上遵循第一个示例

您基本上希望使用“评论”列作为语料库,然后可以计算该列中每一行的TF-IDF

from gensim.models import TfidfModel
from gensim.corpora import Dictionary

doc_list = []
for comment in df['comment']:
    doc_list.append(comment)
dct = Dictionary(doc_list)
corpus = [dct.doc2bow(line) for comment in df['comment']]
model = TfidfModel(corpus)

然后,您可以遍历语料库并找到TF-IDF值。这段代码中可能有一些不准确的地方,因为我暂时无法进行测试,但总体思路应该是可行的:)

相关问题 更多 >

    热门问题