利用sklearn生成詞條矩陣

from sklearn.feature_extraction.text import CountVectorizer countvec = CountVectorizer(min_df=1,max_df=1)

定義詞條矩陣函數;

min_df :默認1,當為0-1的數時,表示小於min_df不納入關鍵詞;當大於1時,表示小於min_df不納入關鍵詞;

max_df :默認1,當為0-1的數時,表示大於max_df不納入關鍵詞;當大於1時,表示大於max_df不納入關鍵詞;

analyze = countvec.build_analyzer()
analyze('郭靖 和 哀牢山 三十六 劍')

輸出分詞結果;

x = countvec.fit_transform(['郭靖 和 哀牢山 三十六 劍 哈哈 鏡子 哈哈 哈哈 哈哈','哈哈 哈哈 哈哈 噢 這樣啊 郭靖 三十六 哀牢山'])

訓練並轉換詞句成詞袋模型

x.todense() # 輸出詞條矩陣 countvec.get_feature_names() # 輸出詞條標籤名 countvec.vocabulary_ # 輸出詞條對應的位置


分享到:


相關文章: