forked from piskvorky/gensim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatutils.html
More file actions
304 lines (256 loc) · 15.7 KB
/
Copy pathmatutils.html
File metadata and controls
304 lines (256 loc) · 15.7 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!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>matutils – Math utils — 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.6',
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/jquery.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="up" title="API Reference" href="apiref.html" />
<link rel="next" title="corpora.bleicorpus – Corpus in Blei’s LDA-C format" href="corpora/bleicorpus.html" />
<link rel="prev" title="utils – Various utility functions" href="utils.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="corpora/bleicorpus.html" title="corpora.bleicorpus – Corpus in Blei’s LDA-C format"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="utils.html" title="utils – Various utility functions"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Gensim home</a>| </li>
<li><a href="tutorial.html">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>
<li><a href="apiref.html" accesskey="U">API Reference</a> »</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="utils.html"
title="previous chapter"><tt class="docutils literal docutils literal"><span class="pre">utils</span></tt> – Various utility functions</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="corpora/bleicorpus.html"
title="next chapter"><tt class="docutils literal docutils literal docutils literal"><span class="pre">corpora.bleicorpus</span></tt> – Corpus in Blei’s LDA-C format</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="module-gensim.matutils">
<span id="matutils-math-utils"></span><h1><tt class="xref py py-mod docutils literal"><span class="pre">matutils</span></tt> – Math utils<a class="headerlink" href="#module-gensim.matutils" title="Permalink to this headline">¶</a></h1>
<p>This module contains math helper functions.</p>
<dl class="class">
<dt id="gensim.matutils.Dense2Corpus">
<em class="property">class </em><tt class="descclassname">gensim.matutils.</tt><tt class="descname">Dense2Corpus</tt><big>(</big><em>dense</em>, <em>documents_columns=True</em><big>)</big><a class="headerlink" href="#gensim.matutils.Dense2Corpus" title="Permalink to this definition">¶</a></dt>
<dd><p>Treat dense numpy array as a sparse gensim corpus.</p>
<p>No data copy is made (changes to the underlying matrix imply changes in the
corpus).</p>
</dd></dl>
<dl class="class">
<dt id="gensim.matutils.MmReader">
<em class="property">class </em><tt class="descclassname">gensim.matutils.</tt><tt class="descname">MmReader</tt><big>(</big><em>input</em>, <em>transposed=True</em><big>)</big><a class="headerlink" href="#gensim.matutils.MmReader" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrap a term-document matrix on disk (in matrix-market format), and present it
as an object which supports iteration over the rows (~documents).</p>
<p>Note that the file is read into memory one document at a time, not the whole
matrix at once (unlike scipy.io.mmread). This allows us to process corpora
which are larger than the available RAM.</p>
<p>Initialize the matrix reader.</p>
<p>The <cite>input</cite> refers to a file on local filesystem, which is expected to
be in the sparse (coordinate) Matrix Market format. Documents are assumed
to be rows of the matrix (and document features are columns).</p>
<p><cite>input</cite> is either a string (file path) or a file-like object that supports
<cite>seek()</cite> (e.g. gzip.GzipFile, bz2.BZ2File).</p>
<dl class="method">
<dt id="gensim.matutils.MmReader.docbyoffset">
<tt class="descname">docbyoffset</tt><big>(</big><em>offset</em><big>)</big><a class="headerlink" href="#gensim.matutils.MmReader.docbyoffset" title="Permalink to this definition">¶</a></dt>
<dd><p>Return document at file offset <cite>offset</cite> (in bytes)</p>
</dd></dl>
<dl class="method">
<dt id="gensim.matutils.MmReader.skip_headers">
<tt class="descname">skip_headers</tt><big>(</big><em>input_file</em><big>)</big><a class="headerlink" href="#gensim.matutils.MmReader.skip_headers" title="Permalink to this definition">¶</a></dt>
<dd><p>Skip file headers that appear before the first document.</p>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="gensim.matutils.MmWriter">
<em class="property">class </em><tt class="descclassname">gensim.matutils.</tt><tt class="descname">MmWriter</tt><big>(</big><em>fname</em><big>)</big><a class="headerlink" href="#gensim.matutils.MmWriter" title="Permalink to this definition">¶</a></dt>
<dd><p>Store a corpus in Matrix Market format.</p>
<p>Note that the output is written one document at a time, not the whole
matrix at once (unlike scipy.io.mmread). This allows us to process corpora
which are larger than the available RAM.</p>
<p>NOTE: the output file is created in a single pass through the input corpus, so
that the input can be a once-only stream (iterator).
To achieve this, a fake MM header is written first, statistics are collected
during the pass (shape of the matrix, number of non-zeroes), followed by a seek
back to the beginning of the file, rewriting the fake header with proper values.</p>
<dl class="staticmethod">
<dt id="gensim.matutils.MmWriter.write_corpus">
<em class="property">static </em><tt class="descname">write_corpus</tt><big>(</big><em>fname</em>, <em>corpus</em>, <em>progress_cnt=1000</em>, <em>index=False</em><big>)</big><a class="headerlink" href="#gensim.matutils.MmWriter.write_corpus" title="Permalink to this definition">¶</a></dt>
<dd><p>Save the vector space representation of an entire corpus to disk.</p>
<p>Note that the documents are processed one at a time, so the whole corpus
is allowed to be larger than the available RAM.</p>
</dd></dl>
<dl class="method">
<dt id="gensim.matutils.MmWriter.write_vector">
<tt class="descname">write_vector</tt><big>(</big><em>docno</em>, <em>vector</em><big>)</big><a class="headerlink" href="#gensim.matutils.MmWriter.write_vector" title="Permalink to this definition">¶</a></dt>
<dd><p>Write a single sparse vector to the file.</p>
<p>Sparse vector is any iterable yielding (field id, field value) pairs.</p>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="gensim.matutils.Sparse2Corpus">
<em class="property">class </em><tt class="descclassname">gensim.matutils.</tt><tt class="descname">Sparse2Corpus</tt><big>(</big><em>sparse</em>, <em>documents_columns=True</em><big>)</big><a class="headerlink" href="#gensim.matutils.Sparse2Corpus" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a matrix in scipy.sparse format into a streaming gensim corpus.</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.corpus2csc">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">corpus2csc</tt><big>(</big><em>corpus</em>, <em>num_terms=None</em>, <em>dtype=<type 'numpy.float64'></em>, <em>num_docs=None</em>, <em>num_nnz=None</em>, <em>printprogress=0</em><big>)</big><a class="headerlink" href="#gensim.matutils.corpus2csc" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert corpus into a sparse matrix, in scipy.sparse.csc_matrix format,
with documents as columns.</p>
<p>If the number of terms, documents and non-zero elements is known, you can pass
them here as parameters and a more memory efficient code path will be taken.</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.corpus2dense">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">corpus2dense</tt><big>(</big><em>corpus</em>, <em>num_terms</em><big>)</big><a class="headerlink" href="#gensim.matutils.corpus2dense" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert corpus into a dense numpy array (documents will be columns).</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.dense2vec">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">dense2vec</tt><big>(</big><em>vec</em>, <em>eps=1e-09</em><big>)</big><a class="headerlink" href="#gensim.matutils.dense2vec" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a dense numpy array into the sparse corpus format (sequence of 2-tuples).</p>
<p>Values of magnitude < <cite>eps</cite> are treated as zero (ignored).</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.full2sparse">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">full2sparse</tt><big>(</big><em>vec</em>, <em>eps=1e-09</em><big>)</big><a class="headerlink" href="#gensim.matutils.full2sparse" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a dense numpy array into the sparse corpus format (sequence of 2-tuples).</p>
<p>Values of magnitude < <cite>eps</cite> are treated as zero (ignored).</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.full2sparse_clipped">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">full2sparse_clipped</tt><big>(</big><em>vec</em>, <em>topn</em>, <em>eps=1e-09</em><big>)</big><a class="headerlink" href="#gensim.matutils.full2sparse_clipped" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <cite>full2sparse</cite>, but only return the <cite>topn</cite> greatest elements (not all).</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.pad">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">pad</tt><big>(</big><em>mat</em>, <em>padrow</em>, <em>padcol</em><big>)</big><a class="headerlink" href="#gensim.matutils.pad" title="Permalink to this definition">¶</a></dt>
<dd><p>Add additional rows/columns to a numpy.matrix <cite>mat</cite>. The new rows/columns
will be initialized with zeros.</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.qr_destroy">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">qr_destroy</tt><big>(</big><em>la</em><big>)</big><a class="headerlink" href="#gensim.matutils.qr_destroy" title="Permalink to this definition">¶</a></dt>
<dd><p>Return QR decomposition of <cite>la[0]</cite>. Content of <cite>la</cite> gets destroyed in the process.</p>
<p>Using this function should be less memory intense than calling <cite>scipy.linalg.qr(la[0])</cite>,
because the memory used in <cite>la[0]</cite> is reclaimed earlier.</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.scipy2sparse">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">scipy2sparse</tt><big>(</big><em>vec</em><big>)</big><a class="headerlink" href="#gensim.matutils.scipy2sparse" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a scipy.sparse vector to gensim format (list of 2-tuples).</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.sparse2full">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">sparse2full</tt><big>(</big><em>doc</em>, <em>length</em><big>)</big><a class="headerlink" href="#gensim.matutils.sparse2full" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a document in sparse corpus format (sequence of 2-tuples) into a dense
numpy array (of size <cite>length</cite>).</p>
</dd></dl>
<dl class="function">
<dt id="gensim.matutils.unitvec">
<tt class="descclassname">gensim.matutils.</tt><tt class="descname">unitvec</tt><big>(</big><em>vec</em><big>)</big><a class="headerlink" href="#gensim.matutils.unitvec" title="Permalink to this definition">¶</a></dt>
<dd><p>Scale a vector to unit length. The only exception is the zero vector, which
is returned back unchanged.</p>
<p>Output will be in the same format as input (i.e., gensim vector=>gensim vector,
or numpy array=>numpy array, scipy.sparse=>scipy.sparse).</p>
</dd></dl>
</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="corpora/bleicorpus.html" title="corpora.bleicorpus – Corpus in Blei’s LDA-C format"
>next</a> |</li>
<li class="right" >
<a href="utils.html" title="utils – Various utility functions"
>previous</a> |</li>
<li><a href="index.html">Gensim home</a>| </li>
<li><a href="tutorial.html">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>
<li><a href="apiref.html" >API Reference</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2009-2012, Radim Řehůřek <radimrehurek(at)seznam.cz>.
Last updated on Sep 16, 2012.
</div>
</body>
</html>