2014-12-02 58 views
1

我有一個NLP任務,我正在使用scikit-learn。閱讀tutorials我發現必須對文本進行矢量化,以及如何使用此矢量化模型來提供分類算法。假設我有一些文字,我想如下向量化它:scipy中的這個稀疏矩陣是什麼意思?

from sklearn.feature_extraction.text import CountVectorizer 

corpus =['''Computer science is the scientific and 
practical approach to computation and its applications.''' 
#this is another opinion 
'''It is the systematic study of the feasibility, structure, 
expression, and mechanization of the methodical 
procedures that underlie the acquisition, 
representation, processing, storage, communication of, 
and access to information, whether such information is encoded 
as bits in a computer memory or transcribed in genes and 
protein structures in a biological cell.''' 
     #anotherone 
'''A computer scientist specializes in the theory of 
computation and the design of computational systems'''] 

vectorizer = CountVectorizer(analyzer='word') 

X = vectorizer.fit_transform(corpus) 

print X 

的問題是,我不明白輸出的意思,我沒有看到有文字和返回的矩陣任何關係通過向量化:

(0, 12) 3 
    (0, 33) 1 
    (0, 20) 3 
    (0, 45) 7 
    (0, 34) 1 
    (0, 2) 6 
    (0, 28) 1 
    (0, 4) 1 
    (0, 47) 2 
    (0, 10) 2 
    (0, 22) 1 
    (0, 3) 1 
    (0, 21) 1 
    (0, 42) 1 
    (0, 40) 1 
    (0, 26) 5 
    (0, 16) 1 
    (0, 38) 1 
    (0, 15) 1 
    (0, 23) 1 
    (0, 25) 1 
    (0, 29) 1 
    (0, 44) 1 
    (0, 49) 1 
    (0, 1) 1 
    : : 
    (0, 30) 1 
    (0, 37) 1 
    (0, 9) 1 
    (0, 0) 1 
    (0, 19) 2 
    (0, 50) 1 
    (0, 41) 1 
    (0, 14) 1 
    (0, 5) 1 
    (0, 7) 1 
    (0, 18) 4 
    (0, 24) 1 
    (0, 27) 1 
    (0, 48) 1 
    (0, 17) 1 
    (0, 31) 1 
    (0, 39) 1 
    (0, 6) 1 
    (0, 8) 1 
    (0, 35) 1 
    (0, 36) 1 
    (0, 46) 1 
    (0, 13) 1 
    (0, 11) 1 
    (0, 43) 1 

而且我不明白什麼是與輸出發生的事情時,我使用toarray()方法:

print X.toarray() 

究竟手段輸出什麼關係與胼?:

[[1 1 6 1 1 1 1 1 1 1 2 1 3 1 1 1 1 1 4 2 3 1 1 1 1 1 5 1 1 1 1 1 1 1 1 1 1 
    1 1 1 1 1 1 1 1 7 1 2 1 1 1]] 
+0

您可能想了解Manning&Schuetze書中的向量空間模型:http://nlp.stanford.edu/IR-book/pdf/06vect.pdf – mbatchkarov 2014-12-02 13:51:05

回答

5

CountVectorizer產生文檔項矩陣。舉個簡單的例子,讓我們來看看下面的簡化代碼:

from sklearn.feature_extraction.text import CountVectorizer 

corpus =['''computer hardware''', 
'''computer data and software data'''] 

vectorizer = CountVectorizer(analyzer='word') 

X = vectorizer.fit_transform(corpus) 

print X 

print X.toarray() 

你有兩個文件,語料庫的元素,和五個方面的話。

 | and computer data hardware software 
     +------------------------------------- 
doc 0 |   1    1 
doc 1 | 1  1 2     1 

和所述X表示在關聯的方式在上述矩陣,即,從(行,列)的地圖術語的頻率和X.toarray()節目X爲:和可以如下計算在文檔中的術語列表清單。以下是執行結果:

(1, 0) 1 
    (0, 1) 1 
    (1, 1) 1 
    (1, 2) 2 
    (0, 3) 1 
    (1, 4) 1 
[[0 1 0 1 0] 
[1 1 2 0 1]] 

正如@dmcc指出,你省略這使得corpus只有一個文件的逗號。

+0

感謝您的反饋意見。關於scikit-learn的其他矢量工具有哪些? (例如FeatureHasher,Tf-idf等),這種矢量化算法是否返回文檔矩陣或返回的矩陣取決於所選擇的矢量化算法?。 – tumbleweed 2014-12-03 05:44:57

+1

@ml_guy是的,它取決於向量化器和參數。請看一下[功能提取頁面](http://scikit-learn.org/stable/modules/feature_extraction.html)。 – 2014-12-03 23:53:38

3

我認爲缺少的環節是vectorizer.get_feature_names()docs)。此方法可以在矩陣映射回計數到其原始字:

>>> vectorizer.get_feature_names() 
[u'access', u'acquisition', u'and', u'applications', u'approach', u'as', u'biological', u'bits', u'cell', u'communication', u'computation', u'computational', u'computer', u'design', u'encoded', u'expression', u'feasibility', u'genes', u'in', u'information', u'is', u'it', u'its', u'mechanization', u'memory', u'methodical', u'of', u'or', u'practical', u'procedures', u'processing', u'protein', u'representation', u'science', u'scientific', u'scientist', u'specializes', u'storage', u'structure', u'structures', u'study', u'such', u'systematic', u'systems', u'that', u'the', u'theory', u'to', u'transcribed', u'underlie', u'whether'] 

因此,在X.toarray()第一元件意味着該語料庫包含單詞access的1個實例和第三元件是指有6個實例的單詞and

順便說一句,混淆的一點可能是#anotherone附近缺失的逗號 - 這會導致兩個字符串被連接,因此corpus只是一個列表,其中只有一個字符串。