matutils – Math utils — gensim documentation

### Navigation

- index
- modules |
- next |
- previous |
- Gensim home|
- API reference|
- Tutorials »
- API Reference »

# `matutils` – Math utils

This module contains math helper functions.

class `gensim.matutils.` `Dense2Corpus`(dense, documents_columns=True)

  Treat dense numpy array as a sparse gensim corpus.

  No data copy is made (changes to the underlying matrix imply changes in the corpus).

class `gensim.matutils.` `MmReader`(input)

  Wrap a term-document matrix on disk (in matrix-market format), and present it as an object which supports iteration over the rows (~documents).

  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.

  Initialize the matrix reader.

  The input 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).

  input is either a string (file path) or a file-like object that supports seek(0) (e.g. gzip.GzipFile, bz2.BZ2File).

class `gensim.matutils.` `MmWriter`(fname)

  Store corpus in Matrix Market format.

  static `writeCorpus`(fname, corpus, progressCnt=1000)

    Save the vector space representation of an entire corpus to disk.

    Note that the documents are processed one at a time, so the whole corpus is allowed to be larger than the available RAM.

  `writeVector`(docNo, vector)

    Write a single sparse vector to the file.

    Sparse vector is any iterable yielding (field id, field value) pairs.

`gensim.matutils.` `corpus2csc`(corpus, num_terms, dtype=<type 'numpy.float64'>)
  Convert corpus into a sparse matrix, in scipy.sparse.csc_matrix format, with documents as columns.

`gensim.matutils.` `corpus2dense`(corpus, num_terms)
  Convert corpus into a dense numpy array (documents will be columns).

`gensim.matutils.` `full2sparse`(vec, eps=1.0000000000000001e-09)

  Convert a dense numpy array into the sparse corpus format (sequence of 2-tuples).

  Values of magnitude < eps are treated as zero (ignored).

`gensim.matutils.` `pad`(mat, padRow, padCol)
  Add additional rows/columns to a numpy.matrix mat. The new rows/columns will be initialized with zeros.

`gensim.matutils.` `sparse2full`(doc, length)
  Convert a document in sparse corpus format (sequence of 2-tuples) into a dense numpy array (of size length).

`gensim.matutils.` `unitVec`(vec)

  Scale a vector to unit length. The only exception is the zero vector, which is returned back unchanged.

  If the input is sparse (list of 2-tuples), output will also be sparse. Otherwise, output will be a numpy array.

### Navigation

- index
- modules |
- next |
- previous |
- Gensim home|
- API reference|
- Tutorials »
- API Reference »