3
我已經創建了一個tf-idf矩陣,但現在我想檢索每個文檔的前兩個單詞。我想通過文件ID,它應該給我的前2個字。在scikit-learn tf-idf矩陣中獲取文檔名稱
現在,我有這樣的樣本數據:
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](http://pandas.pydata.org/)DataFrame。 – BrenBarn 2014-10-10 17:26:36
scikit-learn中沒有「文檔名稱」的概念。你必須自己儲存。 – 2014-10-10 19:22:22
我也這麼認爲。你們確認了它。感謝您的建議 – user1525721 2014-10-11 19:37:14