【发布时间】:2014-12-05 21:43:02
【问题描述】:
我创建了一个 tf-idf 矩阵,但现在我想检索每个文档的前 2 个单词。我想传递文档 ID,它应该给我前 2 个单词。
现在,我有这个示例数据:
from sklearn.feature_extraction.text import TfidfVectorizer
d = {'doc1':"this is the first document",'doc2':"it is a sunny day"} ### corpus
test_v = TfidfVectorizer(min_df=1) ### applied the model
t = test_v.fit_transform(d.values())
feature_names = test_v.get_feature_names() ### list of words/terms
>>> feature_names
['day', 'document', 'first', 'is', 'it', 'sunny', 'the', 'this']
>>> t.toarray()
array([[ 0. , 0.47107781, 0.47107781, 0.33517574, 0. ,
0. , 0.47107781, 0.47107781],
[ 0.53404633, 0. , 0. , 0.37997836, 0.53404633,
0.53404633, 0. , 0. ]])
我可以通过给出行号来访问矩阵,例如。
>>> t[0,1]
0.47107781233161794
有没有办法可以通过文档 ID 访问这个矩阵?在我的例子中是“doc1”和“doc2”。
谢谢
【问题讨论】:
-
不是直接的,但您可以将数据包装在 pandas DataFrame 中。
-
scikit-learn 中没有“文档名称”的概念。您必须自己存储这些。
-
我也是这么想的。你们证实了。感谢您的建议
标签: python matrix machine-learning scikit-learn tf-idf