- Gensim 教程
- Gensim - 首頁
- Gensim - 簡介
- Gensim - 開始
- Gensim - 文件與語料庫
- Gensim - 向量與模型
- Gensim - 建立字典
- 建立詞袋 (BoW) 語料庫
- Gensim - 變換
- Gensim - 建立 TF-IDF 矩陣
- Gensim - 主題建模
- Gensim - 建立 LDA 主題模型
- Gensim - 使用 LDA 主題模型
- Gensim - 建立 LDA Mallet 模型
- Gensim - 文件與 LDA 模型
- Gensim - 建立 LSI 和 HDP 主題模型
- Gensim - 開發詞嵌入
- Gensim - Doc2Vec 模型
- Gensim 有用資源
- Gensim - 快速指南
- Gensim - 有用資源
- Gensim - 討論
Gensim - 變換
本章將幫助您瞭解 Gensim 中的各種變換。讓我們從理解文件變換開始。
文件變換
文件變換意味著以一種可以進行數學運算的方式來表示文件。除了推斷語料庫的潛在結構外,文件變換還將實現以下目標:
它發現單詞之間的關係。
它揭示了語料庫中隱藏的結構。
它以一種新的、更具語義的方式描述文件。
它使文件的表示更加緊湊。
它提高了效率,因為新的表示消耗更少的資源。
它提高了功效,因為在新的表示中忽略了邊緣資料趨勢。
新的文件表示中也減少了噪聲。
讓我們看看將文件從一個向量空間表示轉換為另一個向量空間表示的實現步驟。
實現步驟
為了變換文件,我們必須遵循以下步驟:
步驟 1:建立語料庫
第一步也是最基本的一步是從文件中建立語料庫。我們已經在之前的示例中建立了語料庫。讓我們再建立一個,並進行一些改進(刪除常用詞和只出現一次的詞):
import gensim import pprint from collections import defaultdict from gensim import corpora
現在提供用於建立語料庫的文件:
t_corpus = ["CNTK formerly known as Computational Network Toolkit", "is a free easy-to-use open-source commercial-grade toolkit", "that enable us to train deep learning algorithms to learn like the human brain.", "You can find its free tutorial on tutorialspoint.com", "Tutorialspoint.com also provide best technical tutorials on technologies like AI deep learning machine learning for free"]
接下來,我們需要進行標記化,同時還要刪除常用詞:
stoplist = set('for a of the and to in'.split(' '))
processed_corpus = [
[
word for word in document.lower().split() if word not in stoplist
]
for document in t_corpus
]
以下指令碼將刪除只出現一次的單詞:
frequency = defaultdict(int)
for text in processed_corpus:
for token in text:
frequency[token] += 1
processed_corpus = [
[token for token in text if frequency[token] > 1]
for text in processed_corpus
]
pprint.pprint(processed_corpus)
輸出
[ ['toolkit'], ['free', 'toolkit'], ['deep', 'learning', 'like'], ['free', 'on', 'tutorialspoint.com'], ['tutorialspoint.com', 'on', 'like', 'deep', 'learning', 'learning', 'free'] ]
現在將其傳遞給`corpora.dictionary()`物件以獲取語料庫中的唯一物件:
dictionary = corpora.Dictionary(processed_corpus) print(dictionary)
輸出
Dictionary(7 unique tokens: ['toolkit', 'free', 'deep', 'learning', 'like']...)
接下來,以下程式碼行將為我們的語料庫建立詞袋模型:
BoW_corpus = [dictionary.doc2bow(text) for text in processed_corpus] pprint.pprint(BoW_corpus)
輸出
[ [(0, 1)], [(0, 1), (1, 1)], [(2, 1), (3, 1), (4, 1)], [(1, 1), (5, 1), (6, 1)], [(1, 1), (2, 1), (3, 2), (4, 1), (5, 1), (6, 1)] ]
步驟 2:建立變換
變換是一些標準的 Python 物件。我們可以使用訓練好的語料庫來初始化這些變換,即 Python 物件。在這裡,我們將使用`tf-idf`模型來建立我們訓練好的語料庫`BoW_corpus`的變換。
首先,我們需要從 gensim 中匯入 models 包。
from gensim import models
現在,我們需要按如下方式初始化模型:
tfidf = models.TfidfModel(BoW_corpus)
步驟 3:變換向量
現在,在最後一步中,向量將從舊錶示轉換為新表示。由於我們在上述步驟中已經初始化了 tfidf 模型,因此 tfidf 現在將被視為只讀物件。在這裡,我們將使用此 tfidf 物件將我們的向量從詞袋錶示(舊錶示)轉換為 Tfidf 實值權重(新表示)。
doc_BoW = [(1,1),(3,1)] print(tfidf[doc_BoW]
輸出
[(1, 0.4869354917707381), (3, 0.8734379353188121)]
我們將變換應用於語料庫的兩個值,但我們也可以將其應用於整個語料庫,如下所示:
corpus_tfidf = tfidf[BoW_corpus] for doc in corpus_tfidf: print(doc)
輸出
[(0, 1.0)] [(0, 0.8734379353188121), (1, 0.4869354917707381)] [(2, 0.5773502691896257), (3, 0.5773502691896257), (4, 0.5773502691896257)] [(1, 0.3667400603126873), (5, 0.657838022678017), (6, 0.657838022678017)] [ (1, 0.19338287240886842), (2, 0.34687949360312714), (3, 0.6937589872062543), (4, 0.34687949360312714), (5, 0.34687949360312714), (6, 0.34687949360312714) ]
完整的實現示例
import gensim
import pprint
from collections import defaultdict
from gensim import corpora
t_corpus = [
"CNTK formerly known as Computational Network Toolkit",
"is a free easy-to-use open-source commercial-grade toolkit",
"that enable us to train deep learning algorithms to learn like the human brain.",
"You can find its free tutorial on tutorialspoint.com",
"Tutorialspoint.com also provide best technical tutorials on
technologies like AI deep learning machine learning for free"
]
stoplist = set('for a of the and to in'.split(' '))
processed_corpus = [
[word for word in document.lower().split() if word not in stoplist]
for document in t_corpus
]
frequency = defaultdict(int)
for text in processed_corpus:
for token in text:
frequency[token] += 1
processed_corpus = [
[token for token in text if frequency[token] > 1]
for text in processed_corpus
]
pprint.pprint(processed_corpus)
dictionary = corpora.Dictionary(processed_corpus)
print(dictionary)
BoW_corpus = [dictionary.doc2bow(text) for text in processed_corpus]
pprint.pprint(BoW_corpus)
from gensim import models
tfidf = models.TfidfModel(BoW_corpus)
doc_BoW = [(1,1),(3,1)]
print(tfidf[doc_BoW])
corpus_tfidf = tfidf[BoW_corpus]
for doc in corpus_tfidf:
print(doc)
Gensim 中的各種變換
使用 Gensim,我們可以實現各種流行的變換,即向量空間模型演算法。其中一些如下:
Tf-Idf(詞頻-逆文件頻率)
在初始化期間,此 tf-idf 模型演算法需要一個具有整數值(例如詞袋模型)的訓練語料庫。然後,在變換時,它採用向量表示並返回另一個向量表示。
輸出向量將具有相同的維度,但稀有特徵(在訓練時)的值將增加。它基本上將整數值向量轉換為實數值向量。以下是 Tf-idf 變換的語法:
Model=models.TfidfModel(corpus, normalize=True)
LSI(潛在語義索引)
LSI 模型演算法可以將文件從整數值向量模型(例如詞袋模型)或 Tf-Idf 加權空間變換到潛在空間。輸出向量的維度將較低。以下是 LSI 變換的語法:
Model=models.LsiModel(tfidf_corpus, id2word=dictionary, num_topics=300)
LDA(潛在狄利克雷分配)
LDA 模型演算法是另一種將文件從詞袋模型空間轉換為主題空間的演算法。輸出向量的維度將較低。以下是 LSI 變換的語法:
Model=models.LdaModel(corpus, id2word=dictionary, num_topics=100)
隨機投影 (RP)
RP 是一種非常有效的方法,旨在降低向量空間的維度。這種方法基本上近似於文件之間的 Tf-Idf 距離。它是透過引入一些隨機性來實現的。
Model=models.RpModel(tfidf_corpus, num_topics=500)
層次狄利克雷過程 (HDP)
HDP 是一種非引數貝葉斯方法,它是 Gensim 的新增功能。使用它時需要注意。
Model=models.HdpModel(corpus, id2word=dictionary