gensim 实践篇

继上篇文章了解了一些模型的基本原理以后,这里来讲讲怎么用 gensim,主要参考官方网站的 gensim: Tutorials,这篇博文也只是简单记下一点笔记。

主要有三块内容,先讲怎么把文档表示成向量空间模型(VSM,vector space model)中的稀疏向量(sparse vector)形式,然后是怎么用模型(这里叫 topic and transformations)把词袋模型(BoW,Bag of Word)的表示转化成该模型的形式。最后是怎么把结果存下来做文档的相似度检索等。

顺便提一下,安装 gensim 特别简单,直接在终端里,

pip install --upgrade gensim

Corpora and Vector Spaces

这一节讨论怎么表示文档,特别是怎么表示成 BoW 形式,并用词典统计所有的词汇。

这里约定文档的表示有三种形式,

  • document,用一个字符串表示一篇文章
  • text,分词后(or tokenize)的表示,这里要去停词和低频词等
  • corpus,文档的 BoW 表示,写成词的 id 和对应的词频,词典记录了词汇和对应 id

我们用一组非常短的文档集示例,这里每篇文档只有一句话,

from gensim import corpora
from pprint import pprint

documents = ["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"]

print 'got', len(documents), 
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值