创建 TF-IDF 矩阵 Python 3.6

Creating a TF-IDF Matrix Python 3.6

我有 100 个文档(每个文档都是该文档中单词的简单列表)。现在我想创建一个 TF-IDF 矩阵,以便我可以按排名创建一个小词搜索。我使用 tfidfVectorizer 尝试了它,但在语法中迷路了。任何帮助将非常感激。问候。

编辑:我将列表转换为字符串并将它们添加到父列表中:

vectorizer = TfidfVectorizer(vocabulary=word_set)
matrix = vectorizer.fit_transform(doc_strings)
print(matrix)

这里 word_set 是一组可能的不同单词,doc_strings 是一个包含每个文档作为字符串的列表;但是,当我打印矩阵时,我得到如下输出:

  (0, 839)  0.299458532286
  (0, 710)  0.420878518454
  (0, 666)  0.210439259227
  (0, 646)  0.149729266143
  (0, 550)  0.210439259227
  (0, 549)  0.210439259227
  (0, 508)  0.210439259227
  (0, 492)  0.149729266143
  (0, 479)  0.149729266143
  (0, 425)  0.149729266143
  (0, 401)  0.210439259227
  (0, 332)  0.210439259227
  (0, 310)  0.210439259227
  (0, 253)  0.149729266143
  (0, 216)  0.210439259227
  (0, 176)  0.149729266143
  (0, 122)  0.149729266143
  (0, 119)  0.210439259227
  (0, 111)  0.149729266143
  (0, 46)   0.210439259227
  (0, 26)   0.210439259227
  (0, 11)   0.149729266143
  (0, 0)    0.210439259227
  (1, 843)  0.0144007295367
  (1, 842)  0.0288014590734
  (1, 25)   0.0144007295367
  (1, 24)   0.0144007295367
  (1, 23)   0.0432021886101
  (1, 22)   0.0144007295367
  (1, 21)   0.0288014590734
  (1, 20)   0.0288014590734
  (1, 19)   0.0288014590734
  (1, 18)   0.0432021886101
  (1, 17)   0.0288014590734
  (1, 16)   0.0144007295367
  (1, 15)   0.0144007295367
  (1, 14)   0.0432021886101
  (1, 13)   0.0288014590734
  (1, 12)   0.0144007295367
  (1, 11)   0.0102462376715
  (1, 10)   0.0144007295367
  (1, 9)    0.0288014590734
  (1, 8)    0.0288014590734
  (1, 7)    0.0144007295367
  (1, 6)    0.0144007295367
  (1, 5)    0.0144007295367
  (1, 4)    0.0144007295367
  (1, 3)    0.0144007295367
  (1, 2)    0.0288014590734
  (1, 1)    0.0144007295367

这是否正确?如果正确,我如何在特定文档中搜索给定单词的排名。

您的代码运行良好。我举了几个句子的例子。这里一个句子相当于一个文档。希望这对你有所帮助。

from sklearn.feature_extraction.text import TfidfVectorizer

corpus = ["welcome to Whosebug my friend", 
          "my friend, don't worry, you can get help from Whosebug"]
vectorizer = TfidfVectorizer()
matrix = vectorizer.fit_transform(corpus)
print(matrix)

As we know that fit_transform() returns a tf-idf-weighted document-term matrix.

print()语句输出如下:

  (0, 2)    0.379303492809
  (0, 6)    0.379303492809
  (0, 7)    0.379303492809
  (0, 8)    0.533097824526
  (0, 9)    0.533097824526
  (1, 3)    0.342619853089
  (1, 5)    0.342619853089
  (1, 4)    0.342619853089
  (1, 0)    0.342619853089
  (1, 11)   0.342619853089
  (1, 10)   0.342619853089
  (1, 1)    0.342619853089
  (1, 2)    0.243776847332
  (1, 6)    0.243776847332
  (1, 7)    0.243776847332

那么,我们如何解释这个矩阵呢?您可以在每一行中看到一个元组 (x, y) 和一个值。这里的元组代表,文件号。 (在这种情况下,句子编号)和功能编号

为了更好地理解,让我们打印特征列表(在我们的例子中,特征是单词)及其索引。

for i, feature in enumerate(vectorizer.get_feature_names()):
    print(i, feature)

它输出:

0 can
1 don
2 friend
3 from
4 get
5 help
6 my
7 Whosebug
8 to
9 welcome
10 worry
11 you

所以,welcome to Whosebug my friend句子就变成了下面的样子。

(0, 2)  0.379303492809
(0, 6)  0.379303492809
(0, 7)  0.379303492809
(0, 8)  0.533097824526
(0, 9)  0.533097824526

例如,前两行值可以解释如下。

0 = sentence no.
2 = word index (index of the word `friend`)
0.379303492809 = tf-idf weight

0 = sentence no.
6 = word index (index of the word `my`)
0.379303492809 = tf-idf weight

从 tf-idf 值可以看出,单词 welcometo 在句子 1 中的排名应该高于其他单词。

您可以扩展此示例以搜索给定单词在特定句子或文档中的排名以满足您的需要。