models.lsimodel – Latent Semantic Indexing — gensim documentation

### Navigation

- index
- modules |
- next |
- previous |
- gensim documentation »
- API Reference »

# `models.lsimodel` – Latent Semantic Indexing

Module for Latent Semantic Indexing.

class `gensim.models.lsimodel.` `LsiModel`(corpus, id2word=None, numTopics=200)

  Objects of this class allow building and maintaining a model for Latent Semantic Indexing (also known as Latent Semantic Analysis).

  The main methods are:

  1. constructor, which calculates the latent topics space, effectively initializing the model,
  2. the [] method, which returns representation of any input document in the computed latent space.

  Model persistency is achieved via its load/save methods.

  Find latent space based on the corpus provided.

  numTopics is the number of requested factors (latent dimensions).

  After the model has been initialized, you can estimate topics for an arbitrary, unseen document, using the `topics = self[document]` dictionary notation.

  Example:

      >>> lsi = LsiModel(corpus, numTopics = 10)
      >>> doc_lsi = lsi[doc_tfidf]

  `initialize`(corpus, chunks=100, keepDecomposition=False)

    Run SVD decomposition on the corpus. This will define the latent space into which terms and documents will be mapped.

    The SVD is created incrementally, in blocks of chunks documents. In the end, a self.projection matrix is constructed that can be used to transform documents into the latent space. The U, S, V decomposition itself is discarded, unless keepDecomposition is True, in which case it is stored in self.u, self.s and self.v.

    The algorithm is adapted from: M. Brand. 2006. Fast low-rank modifications of the thin singular value decomposition

  classmethod `load`(fname)
    Load a previously saved object from file (also see save).
  `printTopic`(topicNo, topN=10)

    Print a specified topic (0 <= topicNo < numTopics) in human readable format.

    Example:

        >>> lsimodel.printTopic(10, topN = 5)
        -0.340 * "category" + 0.298 * "$M$" + 0.183 * "algebra" + -0.174 * "functor" + -0.168 * "operator"

  `save`(fname)
    Save the object to file via pickling (also see load).
  `svdAddCols`(docs, decay=1.0, reorth=False)

    Update singular value decomposition factors to take into account new documents docs.

    This function corresponds to the general update of Brand (section 2), specialized for A = docs.T and B trivial (no update to matrix rows).

    The documents are assumed to be a list of full vectors (ie. not sparse 2-tuples).

    Compute new decomposition u’, s’, v’ so that if the current matrix X decomposes to u * s * v^T ~= X, then u’ * s’ * v’^T ~= [X docs^T]

    u, s, v and their new values u’, s’, v’ are stored within self (ie. as self.u, self.v etc.).

    self.v can be set to None, in which case it is completely ignored. This saves a bit of speed and a lot of memory, especially for huge corpora (size of v is linear in the number of added documents).

`gensim.models.lsimodel.` `iterSvd`(corpus, numTerms, numFactors, numIter=200, initRate=None, convergence=0.0001)

  Perform iterative Singular Value Decomposition on a streaming matrix (corpus), returning numFactors greatest factors (ie., not necessarily full spectrum).

  The parameters numIter (maximum number of iterations) and initRate (gradient descent step size) guide convergency of the algorithm.

  See Genevieve Gorrell: Generalized Hebbian Algorithm for Incremental Singular Value Decomposition in Natural Language Processing. EACL 2006.

  Use of this function is deprecated; although it works, it is several orders of magnitude slower than the direct (non-stochastic) version based on Brand. Use svdAddCols/svdUpdate to compute SVD iteratively. I keep this function here purely for backup reasons.

`gensim.models.lsimodel.` `svdUpdate`(U, S, V, a, b)

  Update SVD of an (m x n) matrix X = U * S * V^T so that [X + a * b^T] = U’ * S’ * V’^T and return U’, S’, V’.

  a and b are (m, 1) and (n, 1) rank-1 matrices, so that svdUpdate can simulate incremental addition of one new document and/or term to an already existing decomposition.

### Navigation

- index
- modules |
- next |
- previous |
- gensim documentation »
- API Reference »