短文本分類,首先對文本做預處理,包括分詞,去停頓詞,文本向量化
1.分詞:使用jieba分詞,使用比較簡單,jieba分詞有三種模式,
-
精確模式:將句子最精確的分開,適合文本分析
-
全模式:句子中所有可以成詞的詞語都掃描出來,速度快,不能解決歧義
-
搜索引擎模式:在精確的基礎上,對長詞再次切分,提高召回
import jieba
#全模式
text = "我來到北京清華大學"
seg_list = jieba.cut(text, cut_all=True)
print u"[全模式]: ", "/ ".join(seg_list)
#精確模式
seg_list = jieba.cut(text, cut_all=False)
print u"[精確模式]: ", "/ ".join(seg_list)
#默認是精確模式
seg_list = jieba.cut(text)
print u"[默認模式]: ", "/ ".join(seg_list)
#新詞識別 “杭研”並沒有在詞典中,但是也被Viterbi算法識別出來了
seg_list = jieba.cut("他來到了網易杭研大廈")
print u"[新詞識別]: ", "/ ".join(seg_list)
#搜索引擎模式
seg_list = jieba.cut_for_search(text)
print u"[搜索引擎模式]: ", "/ ".join(seg_list)

2.去停頓詞:去停頓詞實際就是導入自己定義好的一個字典表,然后判斷數據中有無這些關鍵字
3.詞袋模型+TF-IDF算法
https://blog.csdn.net/ACM_hades/article/details/93085783
(1)詞袋模型:它是一種用機器學習算法對文本進行建模時表示文本數據的方法, 機器學習算法不能直接處理原始文本,文本必須 轉換成數字。具體來說,是數字的向量。
(2)詞袋模型能夠把一段文字或一個文檔轉化為向量表示,它不考慮句子中單詞的順序,只考慮詞表(vocabulary)中單詞在這個句子中的出現次數。具體的說,詞袋模型將每段文字或文檔都轉化為一個與詞匯表一樣長的向量,向量的每個元素存儲該位置對於的詞出現的次數。
(3)由於每個文檔中一般只會出現的詞匯表中的很少一部分詞,因此會有很多的單詞沒有出現過,這些詞被標記為0。所以,向量中大多數的元素就會為0。
from sklearn.feature_extraction.text import CountVectorizer
#一個語料庫
corpus = [
'This is the first document.',
'This document is the second document.',
'And this is the third one.',
'Is this the first document?',
]
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(corpus)
print("詞匯:索引",vectorizer.vocabulary_)
print("句子的向量:")
print(X.toarray())#元素為每個詞出現的次數
輸出:
詞匯:索引
{'this': 8, 'is': 3, 'the': 6, 'first': 2, 'document': 1, 'second': 5, 'and': 0, 'third': 7, 'one': 4}
句子的向量:
[[0 1 1 1 0 0 1 0 1]
[0 2 0 1 0 1 1 0 1]
[1 0 0 1 1 0 1 1 1]
[0 1 1 1 0 0 1 0 1]]
可以設置CountVecorizer中的ngram_range參數來構建不同的n元組模型,默認ngram_range=(1,1)
1元組:“the”、“weather”、“is”、“sweet”。
2元組:“the weather”、“weather is”、“is sweet”
TF-IDF算法:
詞頻-逆文檔頻率(TF-IDF,term frequency-inverse document frequency):是一種用於信息檢索與數據挖掘的常用加權技術,常用於挖掘文章中的關鍵詞。算法簡單高效,常被工業用於最開始的文本數據清洗。
TF-IDF有兩層意思:詞頻(TF)表示在某個文檔中每個單詞出現的頻率;逆文檔頻率(IDF): 是一個詞語普遍重要性的度量,它的大小與一個詞的常見程度成反比,計算方法是語料庫的文檔總數除以語料庫中包含該詞語的文檔數量,再將得到的商取對數。
TF-IDF=TF*IDF


4.詞袋與TF-IDF結合使用
BOW模型有很多缺點:
1、沒有考慮單詞之間的順序,
2、無法反應出一個句子的關鍵詞,
比如這個句子:“John likes to play football, Mary likes too”:
若他的詞匯表為:[‘football’, ‘john’, ‘likes’, ‘mary’, ‘play’, ‘to’, ‘too’]
則詞向量表示為:[1 1 2 1 1 1 1]
若根據BOW模型提取這個句子的關鍵詞,則為 “like”,因為其值最大,但是顯然這個句子的關鍵詞應該為“football”
TF-IDF則可以解決例子中描述的BOW模型的這個問題,我們用每個詞的TF-IDF分數來代替其頻率,就能解決這個問題。
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
corpus = [
'This is the first document.',
'This document is the second document.',
'And this is the third one.',
'Is this the first document?',
]
vectorizer = TfidfVectorizer()
#設置小數點的位數為2
np.set_printoptions(2)
X = vectorizer.fit_transform(corpus)
#詞匯表
print(vectorizer.vocabulary_)
print(X.toarray())
輸出:
{'this': 8, 'is': 3, 'the': 6, 'first': 2, 'document': 1, 'second': 5, 'and': 0, 'third': 7, 'one': 4}
[[0. 0.47 0.58 0.38 0. 0. 0.38 0. 0.38]
[0. 0.69 0. 0.28 0. 0.54 0.28 0. 0.28]
[0.51 0. 0. 0.27 0.51 0. 0.27 0.51 0.27]
[0. 0.47 0.58 0.38 0. 0. 0.38 0. 0.38]]
原文鏈接:https://blog.csdn.net/ACM_hades/article/details/93085783
5.SVM模型