📜  Gensim-转换

📅  最后修改于: 2020-10-16 02:26:32             🧑  作者: Mango


本章将帮助您学习Gensim中的各种转换。让我们首先了解转换文档。

转换文件

转换文档意味着以一种可以对数学进行操作的方式来表示文档。除了推导语料库的潜在结构外,转换文档还可以实现以下目标:

  • 它发现单词之间的关系。

  • 它带出了语料库中的隐藏结构。

  • 它以一种新的,更具语义的方式描述了文档。

  • 它使文档的表示更加紧凑。

  • 因为新的表示消耗更少的资源,所以它提高了效率。

  • 由于在新的表示形式中忽略了边际数据趋势,因此提高了功效。

  • 在新的文档表示中也减少了噪音。

让我们看看将文档从一种向量空间表示形式转换为另一种向量空间表示形式的实现步骤。

实施步骤

为了转换文档,我们必须遵循以下步骤-

步骤1:创建语料库

最基本的第一步是根据文档创建语料库。在前面的示例中,我们已经创建了语料库。让我们创建另一个具有一些增强功能(删除常见单词和仅出现一次的单词)的方法-

import gensim
import pprint
from collections import defaultdict
from gensim import corpora

现在提供用于创建语料库的文档-

t_corpus = [“ CNTK以前称为Computational Network Toolkit,”是一个免费的易于使用的开源商业级工具包,“”使我们能够训练深度学习算法来像人脑一样学习。“,”您可以在tutorialspoint.com上找到其免费教程”,““ Tutorialspoint.com还免费提供有关AI深度学习机器学习等技术的最佳技术教程”]

接下来,我们需要进行标记化,并随之删除常用词-

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']...)

接下来,以下代码行将为我们的语料库创建Word of Bag模型-

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导入模型包。

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