字符串到向量
string = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]
cut_string = [item.split() for item in string]
#創(chuàng)建詞頻字典
from collections import defaultdict
dic = defaultdict(int)
for item in cut_string:
for cut in item:
dic[cut]+=1
#將原文本中頻數(shù)大于2的詞匯保存下來
result_string = [[cut for cut in item if dic[cut]>=2] for item in cut_string]
總結(jié):--基于python的總結(jié)
- 善于生成器的使用蛤奢,只要是對列表進(jìn)行操作,并且生成列表让腹,那么可以使用生成式远剩,可以替代其他語言中比較復(fù)雜的for循環(huán)的語法
- 對于dafualtdict的使用,只要結(jié)果是希望生成一個字典骇窍,則可以使用該方法瓜晤,dic = defaultdict(int)中的參數(shù)用來指定字典值的類型。
詞袋模型
詞袋模型->就是對于一篇文檔腹纳,將其整理成 “該詞在文檔中的出現(xiàn)次數(shù)痢掠?1 次〕盎校”的形式.
我們將詞用一個整型 ID 來表示足画,詞的 ID 和其詞頻之間的映射表即被稱作詞典
基礎(chǔ)模型:
接下來展示建立詞袋模型,并且建立詞向量的過程:
#步驟一:建立詞典
from gensim import corpora
dictionary = corpora.Dictionary(result_string)
……
Dictionary(12 unique tokens)
最后可以看到整個語料庫中只有 12 個單詞佃牛,這也就意味著淹辞,每篇文檔可以用一個 12 維的向量來表示。
#步驟二:查看詞典信息
#1.可以使用token2id來查看token-id
print(dictionary.token2id)
#2.可以使用dfs查看id-詞頻(frequency)
#步驟三:建立詞向量
new_vec = dictionary.doc2bow(“Hunman computer interaction”.split())
……
[(0, 1), (1, 1)]
返回結(jié)果[(0,1),(1,1)]表示:在文檔
“Hunman computer interaction”中俘侠,單詞“computer”(ID 為 0)出現(xiàn)了 1 次象缀,
單詞“human”(ID 為 1)出現(xiàn)了 1 次蔬将,詞典中的其余 10 個單詞出現(xiàn)次數(shù)均為
0。
改進(jìn)模型-建立向量:
以上建立模型存在的問題央星,每次處理的結(jié)果都保存在內(nèi)存里霞怀,當(dāng)數(shù)據(jù)量較大時難以應(yīng)對壓力,所以需要對其進(jìn)行改進(jìn)莉给,一個主要的方法就是對其使用生成器,生成器是python大數(shù)據(jù)處理的一個利器毙石。
生成器使用示例:
class mycorpora(object):
def __iter__(self):
with open(文件地址) as f:
for line in f:
yield corpora.doc2bow(line.split())
在__iter__函數(shù)中,對語料庫數(shù)據(jù)進(jìn)行解析颓遏,將每一篇文檔變成一個只包含單詞的list徐矩,然后通過建好的詞典將這個 list 轉(zhuǎn)換成一個能表示該文檔的稀疏向量。
corpus_memory_friendly = mycorpora()
for vector in corpus_memory_friendly:
print(vector)
……
[(0, 1), (1, 1), (2, 1)]
[(0, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
[(2, 1), (5, 1), (7, 1), (8, 1)]
[(1, 1), (5, 2), (8, 1)]
[(3, 1), (6, 1), (7, 1)]
[(9, 1)]
[(9, 1), (10, 1)]
[(9, 1), (10, 1), (11, 1)]
盡管輸出和此前 list 的輸出是一樣的州泊,但是這種方法卻是非常節(jié)省內(nèi)存的丧蘸,因為每次只有一個向量留在內(nèi)存中,如此一來遥皂,你可以處理任意大的語料庫力喷。
改進(jìn)模型--建立字典
from six import iteritems
#此處接收生成器
dictionary = corpora.Dictionary(line.lower().split() for line in open('mycorpus.txt
'))
#使用iteritems迭代字典效率較高
once_ids = [tokenid for tokenid, docfreq in iteritems(dictionary.dfs) if docfreq == 1]
#字典過濾掉id
dictionary.filter_tokens(stop_ids + once_ids)
#由于字典過濾掉id,所以使用以下的方法可以使其變得緊湊
dictionary.compactify()
print(dictionary)
……
Dictionary(12 unique tokens)
總結(jié):--基于gensim
- corpora.Dictionary(參數(shù))--->接收迭代器
- dictionary.dfs,dictionary.token2bow---->用來查看 詞語-id,和id->頻數(shù)
- dictionary.doc2bow(參數(shù))---->用來將文檔轉(zhuǎn)換為向量
- dictionary.filter_tokens(參數(shù))---->需要過濾掉的id的列表
- dictionary.compactify()----->使詞典變得緊湊
總結(jié):--基于生成器
生成器可以用來處理大數(shù)據(jù):
- 當(dāng)一個處理的方法是處理一條數(shù)據(jù)時,則可以通過for循環(huán)結(jié)合yield將每條數(shù)據(jù)傳遞給處理的方法演训,然后用for循環(huán)得到處理后的結(jié)果
- 也可以將生成器當(dāng)成一個整體傳遞給處理方法弟孟,前提是處理方法里有關(guān)于該整體的for循環(huán),這樣可以將生成器當(dāng)做和列表沒有區(qū)別样悟,對其進(jìn)行循環(huán)正常處理即可拂募。