我已经创建了一个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'。
谢谢