gpt4 book ai didi

normalization - 标准化 TF-IDF 结果

转载 作者:行者123 更新时间:2023-12-04 03:48:29 24 4
gpt4 key购买 nike

我想标准化我从这个给定代码中得到的 tfidf 结果:

for (int docNum = 0; docNum < ir.numDocs(); docNum++) {
TermFreqVector tfv = ir.getTermFreqVector(docNum, "contents");
if (tfv == null) {
// ignore empty fields
continue;
}
String[] tterms = tfv.getTerms();
int termCount = tterms.length;
int[] freqs = tfv.getTermFrequencies();
for (int t = 0; t < termCount; t++) {
double idf = ir.numDocs() / ir.docFreq(new Term("contents", tterms[t]));
System.out.println(" " + tterms[t] + " " + freqs[t]*Math.log(idf));
}
}

这段代码的输出是:

area 0.0
areola 5.877735781779639
ari 3.9318256327243257
art 1.6094379124341003
artifici 1.0986122886681098
assign 2.1972245773362196
associ 3.295836866004329
assur 1.9459101490553132
averag 1.0986122886681098
avoid 0.6931471805599453
.
.
.

任何帮助将不胜感激。谢谢

最佳答案

一种常见的方法是按文档大小进行标准化。即不使用词条计数(或绝对频率),而是使用相对频率。

freqsum 成为频率数组的总和。然后使用

freqs[t]/(double)freqsum*Math.log(idf)

为避免此类混淆,我建议使用以下术语:

  • 术语计数用于“绝对频率”
  • 相对频率用于文档中单词的比率

而不是模棱两可的术语“词频”。

我知道,从历史上看,如果您查阅 Salton, Yang, On the specification of term values in automatic indexing (1973),它们指的是绝对计数。余弦相似度将消除比例,因此无论如何都无关紧要。像 Lucene 这样的现代系统会尝试更好地控制文档的影响。

关于normalization - 标准化 TF-IDF 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11281410/

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