【问题标题】:TF-IDF by string line rather than whole text documentTF-IDF 按字符串行而不是整个文本文档
【发布时间】:2023-03-08 08:36:01
【问题描述】:

我已经在一个简单的程序中实现了 TF-IDF,但我想计算每行而不是整个文件的 TF-IDF。

我使用了from sklearn.feature_extraction.text import TfidfVectorizer,并以tf-idf feature weights using sklearn.feature_extraction.text.TfidfVectorizer为例查看了以下链接

这是我的代码:

from sklearn.feature_extraction.text import TfidfVectorizer

f1 = open('testDB.txt','r')
a = []  
for line in f1:
    a.append(line.strip())
f1.close()

f2 = open('testDB1.txt','r')
b = []
for line in f2:
    b.append(line.strip())
f2.close()

for i in range(min(len(a), len(b))):
    vectorizer = TfidfVectorizer(min_df=1)
    X = vectorizer.fit_transform(a, b)
    idf = vectorizer.idf_
    print dict(zip(vectorizer.get_feature_names(), idf))

文本文件包括:

testDB.txt =
hello my name is tom
epping is based just outside of london football
epping football club is really bad

testDB1.txt = 
hello my name is tom
i live in chelmsford and i play football
chelmsford is a lovely city

输出:

{u'based': 1.6931471805599454, u'name': 1.6931471805599454, u'just': 1.6931471805599454, u'outside': 1.6931471805599454, u'club': 1.6931471805599454, u'of': 1.6931471805599454, u'is': 1.0, u'football': 1.2876820724517808, u'epping': 1.2876820724517808, u'bad': 1.6931471805599454, u'london': 1.6931471805599454, u'tom': 1.6931471805599454, u'my': 1.6931471805599454, u'hello': 1.6931471805599454, u'really': 1.6931471805599454}
{u'based': 1.6931471805599454, u'name': 1.6931471805599454, u'just': 1.6931471805599454, u'outside': 1.6931471805599454, u'club': 1.6931471805599454, u'of': 1.6931471805599454, u'is': 1.0, u'football': 1.2876820724517808, u'epping': 1.2876820724517808, u'bad': 1.6931471805599454, u'london': 1.6931471805599454, u'zain': 1.6931471805599454, u'my': 1.6931471805599454, u'hello': 1.6931471805599454, u'really': 1.6931471805599454}
{u'based': 1.6931471805599454, u'name': 1.6931471805599454, u'just': 1.6931471805599454, u'outside': 1.6931471805599454, u'club': 1.6931471805599454, u'of': 1.6931471805599454, u'is': 1.0, u'football': 1.2876820724517808, u'epping': 1.2876820724517808, u'bad': 1.6931471805599454, u'london': 1.6931471805599454, u'tom': 1.6931471805599454, u'my': 1.6931471805599454, u'hello': 1.6931471805599454, u'really': 1.6931471805599454}

如您所见,它为两个文本文件而不是每行的整个文档执行 TF-IDF。我尝试使用 for 循环实现每行,但我无法找出问题所在。

理想情况下,输出将打印每行的 TF-IDF。例如

u'hello': 0.23123, u'my': 0.3123123, u'name': '0.2313213, u'is': 0.3213132, u'tom': 0.3214344

等等

如果有人可以帮助我或提供任何建议,那就太好了。

【问题讨论】:

  • 您似乎没有“以下链接”(您似乎粘贴了import 声明的第二份副本,我已将其删除);您能否编辑问题以包含您要链接到的 URL?
  • 传递一行而不是一组行应该是不费吹灰之力的。但不清楚为什么你有两个文件或它们中的行如何相互关联。程序最终应该将两个文件中的行配对,还是从两个文件中读取所有行并将其用作计算 IDF 的数据库?
  • 这两个文件要相互比较。所以每个文件的第一行将被配对,每个文件中的第二行将被配对等等。程序应该将两个文件中的行配对并计算每个配对行的 IDF。例如。 testDB.txt 中的 'hello my name is tom' 和 testDB1.txt 中的 'hello my name is tom' 将是第一对。如果这有意义?

标签: python scikit-learn tf-idf


【解决方案1】:

嗯...在这里你传递了 a 和 b:

for i in range(min(len(a), len(b))):
    vectorizer = TfidfVectorizer(min_df=1)
    X = vectorizer.fit_transform(a, b)
    idf = vectorizer.idf_
    print dict(zip(vectorizer.get_feature_names(), idf))

当 a 和 b 是数组时...(字符串列表)。你可以这样做:

for i in range(min(len(a), len(b))):
    vectorizer = TfidfVectorizer(min_df=1)
    X = vectorizer.fit_transform(a[i], b[i])
    idf = vectorizer.idf_
    print dict(zip(vectorizer.get_feature_names(), idf))

但是正如 cmets 中提到的,不清楚你在做什么......

【讨论】:

  • 这看起来像 OP 想要的,但实际上没有任何意义。
  • 抱歉,我似乎误解了 tf-idf。
猜你喜欢
  • 2014-07-29
  • 2017-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 2019-10-18
  • 1970-01-01
相关资源
最近更新 更多