本文主要介紹兩個類的基本使用,CountVectorizer與TfidfVectorizer,這兩個類都是特征數值計算的常見方法。對於每一個訓練文本,CountVectorizer只考慮每種詞匯在該訓練文本中出現的頻率,而TfidfVectorizer除了考量某一詞匯在當前訓練文本中出現的頻率之外,同時關注包含這個詞匯的其它訓練文本數目的倒數。相比之下,訓練文本的數量越多,TfidfVectorizer這種特征量化方式就更有優勢。
CountVectorizer用來轉換詞頻矩陣的
#python2.7 sklearn version 0.18.1 from sklearn.feature_extraction.text import CountVectorizer X_test = ['I sed about sed the lack', 'of any Actually'] count_vec=CountVectorizer(stop_words=None) print count_vec.fit_transform(X_test).toarray() print '\nvocabulary list:\n\n',count_vec.vocabulary_ >> >> [[1 0 0 1 1 0 2 1] [0 1 1 0 0 1 0 0]] (0, 4) 1 (0, 7) 1 (0, 0) 1 (0, 6) 2 (0, 3) 1 (1, 1) 1 (1, 2) 1 (1, 5) 1 vocabulary list: {u'about': 0, u'i': 3, u'of': 5, u'lack': 4, u'actually': 1, u'sed': 6, u'the': 7, u'any': 2}
關於上面的代碼,有幾點說明:
(1)第6行代碼中,stop_words=None表示不去掉停用詞,若改為stop_words=’english’則去掉停用詞;
(2)第12,13行,分別是X_test中,兩段文本的詞頻統計結果;
(3)第15-22行,是稀疏矩陣的表示方式;
(4)CountVectorizer同樣適用於中文
# -*- coding: utf-8 -*- from sklearn.feature_extraction.text import CountVectorizer X_test = [u'沒有 你 的 地方 都是 他鄉',u'沒有 你 的 旅行 都是 流浪'] count_vec=CountVectorizer(token_pattern=r"(?u)\b\w\w+\b") print count_vec.fit_transform(X_test).toarray() print count_vec.fit_transform(X_test) print '\nvocabulary list:\n' for key,value in count_vec.vocabulary_.items(): print key,value >> >> [[1 1 0 1 0 1] [0 0 1 1 1 1]] (0, 0) 1 (0, 5) 1 (0, 1) 1 (0, 3) 1 (1, 4) 1 (1, 2) 1 (1, 5) 1 (1, 3) 1 vocabulary list: 他鄉 0 地方 1 旅行 2 沒有 3 都是 5 流浪 4
2.sklearn.feature_extraction.text.TfidfVectorizer
2.1 tf-idf
首先介紹一下如何計算tf-idf,並且需要明確的是tf-idf=tf*idf,也就是說tf與idf分別是兩個不同的東西。其中tf為謀個訓練文本中,某個詞的出現次數,即詞頻(Term Frequency);idf為逆文檔頻率(Inverse Document Frequency),對於詞頻的權重調整系數。
其中:
考慮到文章有長短之分,為了便於不同文章的比較,進行”詞頻”標准化。
如果一個詞越常見,那么分母就越大,逆文檔頻率就越小越接近0。分母之所以要加1,是為了避免分母為0(即所有文檔都不包含該詞)。log表示對得到的值取對數(此處為自然對數)。
假定某訓練文本長度為1000個詞,”中國”、”蜜蜂”、”養殖”各出現20次,則這三個詞的”詞頻”(TF)都為0.02。然后,搜索Google發現,包含”的”字的網頁共有250億張,假定這就是中文網頁總數(即總樣本數)。包含”中國”的網頁共有62.3億張,包含”蜜蜂”的網頁為0.484億張,包含”養殖”的網頁為0.973億張。則它們的逆文檔頻率(IDF)和TF-IDF如下:
NULL | 包含該詞的文檔數(億) | TF | IDF | TF-IDF |
---|---|---|---|---|
中國 | 62.3 | 0.02 | 0.603 | 0.0121 |
蜜蜂 | 0.484 | 0.02 | 2.713 | 0.0543 |
養殖 | 0.973 | 0.02 | 2.410 | 0.0482 |
如:
從上表可見,”蜜蜂”的TF-IDF值最高,”養殖”其次,”中國”最低。所以,如果只選擇一個詞,”蜜蜂”就是這篇文章的關鍵詞。
2.2 TfidfVectorizer
參數表 | 作用 |
---|---|
stop_words | 停用詞表;自定義停用詞表 |
token_pattern | 過濾規則; |
屬性表 | 作用 |
---|---|
vocabulary_ | 詞匯表;字典型 |
idf_ | 返回idf值 |
stop_words_ | 返回停用詞表 |
方法表 | 作用 |
---|---|
fit_transform(X) | 擬合模型,並返回文本矩陣 |
# -*- coding: utf-8 -*- from sklearn.feature_extraction.text import TfidfVectorizer X_test = ['沒有 你 的 地方 都是 他鄉','沒有 你 的 旅行 都是 流浪'] stopword = [u'都是'] #自定義一個停用詞表,如果不指定停用詞表,則默認將所有單個漢字視為停用詞; #但可以設token_pattern=r"(?u)\b\w+\b",即不考慮停用詞 tfidf=TfidfVectorizer(token_pattern=r"(?u)\b\w\w+\b",stop_words=stopword) weight=tfidf.fit_transform(X_test).toarray() word=tfidf.get_feature_names() print 'vocabulary list:\n' for key,value in tfidf.vocabulary_.items(): print key,value print 'IFIDF詞頻矩陣:\n' print weight for i in range(len(weight)): # 打印每類文本的tf-idf詞語權重,第一個for遍歷所有文本, #第二個for便利某一類文本下的詞語權重 print u"-------這里輸出第", i, u"類文本的詞語tf-idf權重------" for j in range(len(word)): print word[j], weight[i][j]#第i個文本中,第j個次的tfidf值 >> >> vocabulary list: 沒有 3 他鄉 0 地方 1 旅行 2 流浪 4 IFIDF詞頻矩陣: [[ 0.6316672 0.6316672 0. 0.44943642 0. ] [ 0. 0. 0.6316672 0.44943642 0.6316672 ]] -------第 0 類文本的詞語tf-idf權重------ 他鄉 0.631667201738 地方 0.631667201738 旅行 0.0 沒有 0.449436416524 流浪 0.0 -------第 1 類文本的詞語tf-idf權重------ 他鄉 0.0 地方 0.0 旅行 0.631667201738 沒有 0.449436416524 流浪 0.631667201738