forked from piskvorky/gensim
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tutorial.html
288 lines (257 loc) · 20 KB
/
tutorial.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tutorials — gensim</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.8.4',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="author" title="About these documents" href="about.html" />
<link rel="top" title="gensim" href="index.html" />
<link rel="next" title="Corpora and Vector Spaces" href="tut1.html" />
<link rel="prev" title="Installation" href="install.html" />
<!-- twitter search widget
<script type="text/javascript" src="_static/widget.js"></script>
-->
<meta property="og:title" content="#gensim" />
<meta property="og:description" content="Efficient topic modelling in Python" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24066335-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="tut1.html" title="Corpora and Vector Spaces"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="install.html" title="Installation"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Gensim home</a>| </li>
<li><a href="#">Tutorials</a>| </li>
<li><a href="http://groups.google.com/group/gensim">Support</a>| </li>
<li><a href="https://github.com/piskvorky/gensim/wiki">Contribute</a>| </li>
<li><a href="apiref.html">API reference</a>»</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Tutorials</a><ul>
<li><a class="reference internal" href="#preliminaries">Preliminaries</a></li>
<li><a class="reference internal" href="#quick-example">Quick Example</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="install.html"
title="previous chapter">Installation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="tut1.html"
title="next chapter">Corpora and Vector Spaces</a></p>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="24" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="tutorials">
<span id="tutorial"></span><h1>Tutorials<a class="headerlink" href="#tutorials" title="Permalink to this headline">¶</a></h1>
<p>The tutorials are organized as a series of examples that highlight various features
of <cite>gensim</cite>. It is assumed that the reader is familiar with the Python language
and has read the <a class="reference internal" href="intro.html"><em>Introduction</em></a>.</p>
<p>The examples are divided into parts on:</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="tut1.html">Corpora and Vector Spaces</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tut1.html#from-strings-to-vectors">From Strings to Vectors</a></li>
<li class="toctree-l2"><a class="reference internal" href="tut1.html#corpus-streaming-one-document-at-a-time">Corpus Streaming – One Document at a Time</a></li>
<li class="toctree-l2"><a class="reference internal" href="tut1.html#corpus-formats">Corpus Formats</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="tut2.html">Topics and Transformations</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tut2.html#transformation-interface">Transformation interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="tut2.html#available-transformations">Available transformations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="tut3.html">Similarity Queries</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tut3.html#similarity-interface">Similarity interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="tut3.html#where-next">Where next?</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="wiki.html">Experiments on the English Wikipedia</a><ul>
<li class="toctree-l2"><a class="reference internal" href="wiki.html#preparing-the-corpus">Preparing the corpus</a></li>
<li class="toctree-l2"><a class="reference internal" href="wiki.html#latent-sematic-analysis">Latent Sematic Analysis</a></li>
<li class="toctree-l2"><a class="reference internal" href="wiki.html#latent-dirichlet-allocation">Latent Dirichlet Allocation</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="distributed.html">Distributed Computing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="distributed.html#why-distributed-computing">Why distributed computing?</a></li>
<li class="toctree-l2"><a class="reference internal" href="distributed.html#prerequisites">Prerequisites</a></li>
<li class="toctree-l2"><a class="reference internal" href="distributed.html#core-concepts">Core concepts</a></li>
<li class="toctree-l2"><a class="reference internal" href="distributed.html#available-distributed-algorithms">Available distributed algorithms</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="preliminaries">
<h2>Preliminaries<a class="headerlink" href="#preliminaries" title="Permalink to this headline">¶</a></h2>
<p>All the examples can be directly copied to your Python interpreter shell (assuming
you have <a class="reference internal" href="install.html"><em>gensim installed</em></a>, of course).
<a class="reference external" href="http://ipython.scipy.org">IPython</a>‘s <tt class="docutils literal"><span class="pre">cpaste</span></tt> command is especially handy for copypasting code fragments which include superfluous
characters, such as the leading <tt class="docutils literal"><span class="pre">>>></span></tt>.</p>
<p>Gensim uses Python’s standard <tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt> module to log various stuff at various
priority levels; to activate logging (this is optional), run</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">logging</span>
<span class="gp">>>> </span><span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">format</span><span class="o">=</span><span class="s">'</span><span class="si">%(asctime)s</span><span class="s"> : </span><span class="si">%(levelname)s</span><span class="s"> : </span><span class="si">%(message)s</span><span class="s">'</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="quick-example">
<span id="first-example"></span><h2>Quick Example<a class="headerlink" href="#quick-example" title="Permalink to this headline">¶</a></h2>
<p>First, let’s import gensim and create a small corpus of nine documents <a class="footnote-reference" href="#id2" id="id1">[1]</a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">gensim</span> <span class="kn">import</span> <span class="n">corpora</span><span class="p">,</span> <span class="n">models</span><span class="p">,</span> <span class="n">similarities</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="n">corpus</span> <span class="o">=</span> <span class="p">[[(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)],</span>
<span class="gp">>>> </span> <span class="p">[(</span><span class="mi">2</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)],</span>
<span class="gp">>>> </span> <span class="p">[(</span><span class="mi">1</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)],</span>
<span class="gp">>>> </span> <span class="p">[(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)],</span>
<span class="gp">>>> </span> <span class="p">[(</span><span class="mi">3</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)],</span>
<span class="gp">>>> </span> <span class="p">[(</span><span class="mi">9</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)],</span>
<span class="gp">>>> </span> <span class="p">[(</span><span class="mi">9</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)],</span>
<span class="gp">>>> </span> <span class="p">[(</span><span class="mi">9</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)],</span>
<span class="gp">>>> </span> <span class="p">[(</span><span class="mi">8</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">),</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)]]</span>
</pre></div>
</div>
<p><em class="dfn">Corpus</em> is simply an object which, when iterated over, returns its documents represented
as sparse vectors.</p>
<p>If you’re familiar with the <a class="reference external" href="http://en.wikipedia.org/wiki/Vector_space_model">Vector Space Model</a>,
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. If you’re not familiar
with <abbr title="Vector Space Model">VSM</abbr>, we’ll bridge the gap between <strong>raw strings</strong>
and <strong>sparse vectors</strong> in the next tutorial
on <a class="reference internal" href="tut1.html"><em>Corpora and Vector Spaces</em></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">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.</p>
</div>
<p>Next, let’s initialize a <em class="dfn">transformation</em>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">tfidf</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TfidfModel</span><span class="p">(</span><span class="n">corpus</span><span class="p">)</span>
</pre></div>
</div>
<p>A transformation is used to convert documents from one vector representation into another:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">vec</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">)]</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">tfidf</span><span class="p">[</span><span class="n">vec</span><span class="p">]</span>
<span class="go">[(0, 0.8075244), (4, 0.5898342)]</span>
</pre></div>
</div>
<p>Here, we used <a class="reference external" href="http://en.wikipedia.org/wiki/Tf%E2%80%93idf">Tf-Idf</a>, 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 <a class="reference external" href="http://en.wikipedia.org/wiki/Norm_%28mathematics%29#Euclidean_norm">Euclidean norm</a>).</p>
<p>Transformations are covered in detail in the tutorial on <a class="reference internal" href="tut2.html"><em>Topics and Transformations</em></a>.</p>
<p>To transform the whole corpus via TfIdf and index it, in preparation for similarity queries:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">index</span> <span class="o">=</span> <span class="n">similarities</span><span class="o">.</span><span class="n">SparseMatrixSimilarity</span><span class="p">(</span><span class="n">tfidf</span><span class="p">[</span><span class="n">corpus</span><span class="p">])</span>
</pre></div>
</div>
<p>and to query the similarity of our query vector <tt class="docutils literal"><span class="pre">vec</span></tt> against every document in the corpus:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">sims</span> <span class="o">=</span> <span class="n">index</span><span class="p">[</span><span class="n">tfidf</span><span class="p">[</span><span class="n">vec</span><span class="p">]]</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="nb">list</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">sims</span><span class="p">))</span>
<span class="go">[(0, 0.4662244), (1, 0.19139354), (2, 0.24600551), (3, 0.82094586), (4, 0.0), (5, 0.0), (6, 0.0), (7, 0.0), (8, 0.0)]</span>
</pre></div>
</div>
<p>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.</p>
<p>Thus, according to TfIdf document representation and cosine similarity measure,
the most similar to our query document <cite>vec</cite> 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 <tt class="docutils literal"><span class="pre">vec</span></tt> at all (documents no. 4–8) get a similarity score of 0.0. See the <a class="reference internal" href="tut3.html"><em>Similarity Queries</em></a> tutorial for more detail.</p>
<hr class="docutils" />
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>This is the same corpus as used in
<a class="reference external" href="http://www.cs.bham.ac.uk/~pxt/IDA/lsa_ind.pdf">Deerwester et al. (1990): Indexing by Latent Semantic Analysis</a>, Table 2.</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="tut1.html" title="Corpora and Vector Spaces"
>next</a> |</li>
<li class="right" >
<a href="install.html" title="Installation"
>previous</a> |</li>
<li><a href="index.html">Gensim home</a>| </li>
<li><a href="#">Tutorials</a>| </li>
<li><a href="http://groups.google.com/group/gensim">Support</a>| </li>
<li><a href="https://github.com/piskvorky/gensim/wiki">Contribute</a>| </li>
<li><a href="apiref.html">API reference</a>»</li>
</ul>
</div>
<div class="footer">
© Copyright 2012, Radim Řehůřek <radimrehurek(at)seznam.cz>.
Last updated on Mar 09, 2012.
</div>
</body>
</html>