gensim学习笔记之基本概念

来源:互联网 发布:汽车行业erp软件 编辑:程序博客网 时间:2024/05/17 07:54

Tutorials

首先看Tutorials,掌握一些关键的概念

  • python的logging模块, 要想看到Log,必须先导入logging包和配置相应的log格式
import logginglogging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
  • corpus包 : 使用空间向量表示的语料库,由一个个vec组成

  • transformationcorpus 表示的空间向量转换为模型

例如:

tfidf = models.TfidfModel(corpus)

一个转换transformation的作用是将一种空间向量矩阵转换为另一种空间向量表示形式
例如:

vec = [(0, 1), (4, 1)]
print(tfidf[vec])
[(0, 0.8075244), (4, 0.5898342)]

还有一些概念,例如相似度、稀疏矩阵等等。


语料库和空间向量

要点:词袋模型
由文本形式的语料库是由一篇篇文档组成的。
把每一篇文档,分词并去掉停用词
建立词袋字典,每篇文档按照词典构建自己的特征向量

dic=corpora.dictionary(iterable)方法构造词典

dictionary = corpora.Dictionary(texts)dictionary.save('/tmp/deerwester.dict') # store the dictionary, for future referenceprint(dictionary)Dictionary(12 unique tokens)

查看词典gensim.corpora.dictionary.Dictionary.token2id

print(dictionary.token2id){'minors': 11, 'graph': 10, 'system': 5, 'trees': 9, 'eps': 8, 'computer': 0,'survey': 4, 'user': 7, 'human': 1, 'time': 6, 'interface': 2, 'response': 3}

根据构造好的词典将文档转换为向量方法doc2bow(iterable)

new_doc = "Human computer interaction"new_vec = dictionary.doc2bow(new_doc.lower().split())print(new_vec) # the word "interaction" does not appear in the dictionary and is ignored[(0, 1), (1, 1)]

保存词袋模型构建出的语料的空间向量到文件

corpora.MmCorpus.serialize('/tmp/deerwester.mm', corpus)

基本概念掌握之后应该以解决问题为导向了。

……

0 0
原创粉丝点击