全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 winbugs及其他软件专版
1153 0
2016-07-18

The tutorials are organized as a series of examples that highlight various features of gensim. It is assumed that the reader is familiar with thePython language, has installed gensim and read the introduction.

The examples are divided into parts on:


Preliminaries

All the examples can be directly copied to your Python interpreter shell. IPython‘s cpaste command is especially handy for copypasting code fragments, including the leading >>> characters.

Gensim uses Python’s standard logging module to log various stuff at various priority levels; to activate logging (this is optional), run

复制代码

Quick Example

First, let’s import gensim and create a small corpus of nine documents and twelve features [1]:

复制代码

In gensim a corpus is simply an object which, when iterated over, returns its documents represented as sparse vectors. In this case we’re using a list of list of tuples. If you’re not familiar with the vector space model, we’ll bridge the gap between raw strings, corpora and sparse vectorsin the next tutorial on Corpora and Vector Spaces.

If you’re familiar with the vector space model, you’ll probably know that the way you parse your documents and convert them to vectors has major impact on the quality of any subsequent applications.


Note

In this example, the whole corpus is stored in memory, as a Python list. However, the corpus interface only dictates that a corpus must support iteration over its constituent documents. For very large corpora, it is advantageous to keep the corpus on disk, and access its documents sequentially, one at a time. All the operations and transformations are implemented in such a way that makes them independent of the size of the corpus, memory-wise.


Next, let’s initialize a transformation:

复制代码

A transformation is used to convert documents from one vector representation into another:

复制代码

Here, we used Tf-Idf, a simple transformation which takes documents represented as bag-of-words counts and applies a weighting which discounts common terms (or, equivalently, promotes rare terms). It also scales the resulting vector to unit length (in the Euclidean norm).

Transformations are covered in detail in the tutorial on Topics and Transformations.

To transform the whole corpus via TfIdf and index it, in preparation for similarity queries:

复制代码

and to query the similarity of our query vector vec against every document in the corpus:

复制代码

How to read this output? Document number zero (the first document) has a similarity score of 0.466=46.6%, the second document has a similarity score of 19.1% etc.

Thus, according to TfIdf document representation and cosine similarity measure, the most similar to our query document vec is document no. 3, with a similarity score of 82.1%. Note that in the TfIdf representation, any documents which do not share any common features with vec at all (documents no. 4–8) get a similarity score of 0.0. See the Similarity Queries tutorial for more detail.

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群