class_docstr: Speeds up inference calls to MatrixFactorization models by using
  `NMSLib <https://github.com/nmslib/nmslib>`_ to create approximate nearest neighbours
  indices of the latent factors.
  
  Parameters
  ----------
  model : MatrixFactorizationBase
      A matrix factorization model to use for the factors
  method : str, optional
      The NMSLib method to use
  index_params: dict, optional
      Optional params to send to the createIndex call in NMSLib
  query_params: dict, optional
      Optional query time params for the NMSLib 'setQueryTimeParams' call
  approximate_similar_items : bool, optional
      whether or not to build an NMSLIB index for computing similar_items
  approximate_recommend : bool, optional
      whether or not to build an NMSLIB index for the recommend call
  
  Attributes
  ----------
  similar_items_index : nmslib.FloatIndex
      NMSLib index for looking up similar items in the cosine space formed by the latent
      item_factors
  
  recommend_index : nmslib.FloatIndex
      NMSLib index for looking up similar items in the inner product space formed by the latent
      item_factors
class_docstr_tok: ["Speeds","up","inference","calls","to","MatrixFactorization","models","by","using","`","NMSLib","<","https",":","\/\/github.com\/nmslib\/nmslib",">","`","_","to","create","approximate","nearest","neighbours","indices","of","the","latent","factors",".","Parameters","--","--","--","--","--","model",":","MatrixFactorizationBase","A","matrix","factorization","model","to","use","for","the","factors","method",":","str",",","optional","The","NMSLib","method","to","use","index_params",":","dict",",","optional","Optional","params","to","send","to","the","createIndex","call","in","NMSLib","query_params",":","dict",",","optional","Optional","query","time","params","for","the","NMSLib","'setQueryTimeParams","'","call","approximate_similar_items",":","bool",",","optional","whether","or","not","to","build","an","NMSLIB","index","for","computing","similar_items","approximate_recommend",":","bool",",","optional","whether","or","not","to","build","an","NMSLIB","index","for","the","recommend","call","Attributes","--","--","--","--","--","similar_items_index",":","nmslib.FloatIndex","NMSLib","index","for","looking","up","similar","items","in","the","cosine","space","formed","by","the","latent","item_factors","recommend_index",":","nmslib.FloatIndex","NMSLib","index","for","looking","up","similar","items","in","the","inner","product","space","formed","by","the","latent","item_factors"]
code: def fit(self, Cui, show_progress=True, callback=None):
      # nmslib can be a little chatty when first imported, disable some of
      # the logging
      logging.getLogger("nmslib").setLevel(logging.WARNING)
  
      # train the model
      self.model.fit(Cui, show_progress, callback=callback)
      item_factors = self.model.item_factors
      if implicit.gpu.HAS_CUDA and isinstance(
          item_factors, implicit.gpu.Matrix
      ):
          item_factors = item_factors.to_numpy()
  
      # create index for similar_items
      if self.approximate_similar_items:
          log.debug("Building nmslib similar items index")
          self.similar_items_index = nmslib.init(
              method=self.method, space="cosinesimil"
          )
  
          # there are some numerical instability issues here with
          # building a cosine index with vectors with 0 norms, hack around this
          # by just not indexing them
          norms = np.linalg.norm(item_factors, axis=1)
          ids = np.arange(item_factors.shape[0])
  
          # delete zero valued rows from the matrix
          nonzero_item_factors = np.delete(
              item_factors, ids[norms == 0], axis=0
          )
          ids = ids[norms != 0]
  
          self.similar_items_index.addDataPointBatch(
              nonzero_item_factors, ids=ids
          )
          self.similar_items_index.createIndex(
              self.index_params, print_progress=show_progress
          )
          self.similar_items_index.setQueryTimeParams(self.query_params)
  
      # build up a separate index for the inner product (for recommend
      # methods)
      if self.approximate_recommend:
          log.debug("Building nmslib recommendation index")
          self.max_norm, extra = augment_inner_product_matrix(
              item_factors
          )
          self.recommend_index = nmslib.init(
              method="hnsw", space="cosinesimil"
          )
          self.recommend_index.addDataPointBatch(extra)
          self.recommend_index.createIndex(
              self.index_params, print_progress=show_progress
          )
          self.recommend_index.setQueryTimeParams(self.query_params)
  
file_path: implicit/implicit/ann/nmslib.py
imports: import logging
  import nmslib
  import numpy
  from scipy.sparse import csr_matrix
  import implicit.gpu
  from implicit.recommender_base import RecommenderBase
  from implicit.utils import _batch_call, _filter_items_from_results, augment_inner_product_matrix
label_desc: Use image node_id 2 for calling the NMSLibModel obj's underlying member method code with example usage: obj.fit(Cui, show_progress, callback) without return types