gpt4 book ai didi

r - 试图让 tf-idf 加权在 R 中工作

转载 作者:行者123 更新时间:2023-12-04 11:14:41 25 4
gpt4 key购买 nike

我正在尝试使用 tm 包进行一些非常基本的文本分析并获得一些 tf-idf 分数;我正在运行 OS X(虽然我已经在 Debian Squeeze 上尝试过,但结果相同);我有一个目录(这是我的工作目录),里面有几个文本文件(第一个包含 Ulysses 的前三集,第二个包含后三集,如果你必须知道的话)。

R 版本:2.15.1
SessionInfo() 报告关于 tm 的这个:[1] tm_0.5-8.3

相关代码:

library('tm')
corpus <- Corpus(DirSource('.'))
dtm <- DocumentTermMatrix(corpus,control=list(weight=weightTfIdf))

str(dtm)
List of 6
$ i : int [1:12456] 1 1 1 1 1 1 1 1 1 1 ...
$ j : int [1:12456] 2 10 12 17 20 24 29 30 32 34 ...
$ v : num [1:12456] 1 1 1 1 1 1 1 1 1 1 ...
$ nrow : int 2
$ ncol : int 10646
$ dimnames:List of 2
..$ Docs : chr [1:2] "bloom.txt" "telemachiad.txt"
..$ Terms: chr [1:10646] "_--c'est" "_--et" "_--for" "_--goodbye," ...
- attr(*, "class")= chr [1:2] "DocumentTermMatrix" "simple_triplet_matrix"
- attr(*, "Weighting")= chr [1:2] "term frequency" "tf"

您会注意到,权重似乎仍然是默认的词频 (tf),而不是我想要的加权 tf-idf 分数。

抱歉,如果我遗漏了一些明显的内容,但根据我阅读的文档,这应该可行。毫无疑问,错不在星星上……

最佳答案

如果你看 DocumentTermMatrix帮助页面,在示例中,您将看到 control参数是这样指定的:

data(crude)
dtm <- DocumentTermMatrix(crude,
control = list(weighting = function(x) weightTfIdf(x, normalize = FALSE),
stopwords = TRUE))

因此,权重由名为 weighting 的列表元素指定。 ,不是 weight .您可以通过传递函数名称或自定义函数来指定此权重,如示例中所示。但以下也有效:
data(crude)
dtm <- DocumentTermMatrix(crude, control = list(weighting = weightTfIdf))

关于r - 试图让 tf-idf 加权在 R 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14820590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com