Sid*_*rma 3 python information-retrieval matrix tf-idf python-3.x
我有100个文档(每个文档都是该文档中的一个简单的单词列表).现在我想创建一个TF-IDF矩阵,这样我就可以按等级创建一个小词搜索.我使用tfidfVectorizer尝试了它,但在语法中丢失了.任何帮助将非常感激.问候.
编辑:我将列表转换为字符串并将其添加到父列表中:
vectorizer = TfidfVectorizer(vocabulary=word_set)
matrix = vectorizer.fit_transform(doc_strings)
print(matrix)
Run Code Online (Sandbox Code Playgroud)
这里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
Run Code Online (Sandbox Code Playgroud)
这是正确的吗?如果是这样,我如何搜索特定文档中给定单词的等级.
Was*_*mad 16
你的代码工作正常.我举几个句子的例子.这里有一个句子相当于一个文件.希望这会对你有所帮助.
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = ["welcome to stackoverflow my friend",
"my friend, don't worry, you can get help from stackoverflow"]
vectorizer = TfidfVectorizer()
matrix = vectorizer.fit_transform(corpus)
print(matrix)
Run Code Online (Sandbox Code Playgroud)
我们知道fit_transform()返回一个tf-idf加权的文档术语矩阵.
该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
Run Code Online (Sandbox Code Playgroud)
那么,我们如何解释这个矩阵呢?您可以(x, y)在每一行中看到元组和值.这里元组代表,文件号.(在这种情况下,句号)和功能号.
为了更好地理解,让我们打印功能列表(在我们的例子中,功能是单词)及其索引.
for i, feature in enumerate(vectorizer.get_feature_names()):
print(i, feature)
Run Code Online (Sandbox Code Playgroud)
它输出:
0 can
1 don
2 friend
3 from
4 get
5 help
6 my
7 stackoverflow
8 to
9 welcome
10 worry
11 you
Run Code Online (Sandbox Code Playgroud)
因此,welcome to stackoverflow my friend句子转换为以下内容.
(0, 2) 0.379303492809
(0, 6) 0.379303492809
(0, 7) 0.379303492809
(0, 8) 0.533097824526
(0, 9) 0.533097824526
Run Code Online (Sandbox Code Playgroud)
例如,前两行值可以解释如下.
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
Run Code Online (Sandbox Code Playgroud)
从tf-idf值,你可以看到,单词welcome和to排名应该高于句子1中的其他单词.
您可以扩展此示例以搜索特定句子或文档中给定单词的排名以满足您的需要.
| 归档时间: |
|
| 查看次数: |
6861 次 |
| 最近记录: |