class_docstr: Hierarchical Poisson Factorization.
  
  Parameters
  ----------
  k: int, optional, default: 5
      The dimension of the latent factors.
  
  max_iter: int, optional, default: 100
      Maximum number of iterations.
  
  name: string, optional, default: 'HPF'
      The name of the recommender model.
  
  trainable: boolean, optional, default: True
      When False, the model is not trained and Cornac assumes that the model is already         pre-trained (Theta and Beta are not None). 
      
  verbose: boolean, optional, default: False
      When True, some running logs are displayed.
      
  hierarchical: boolean, optional, default: True
      When False, PF is used instead of HPF.
      
  seed: int, optional, default: None
      Random seed for parameters initialization.
  
  init_params: dict, optional, default: None
      Initial parameters of the model.
      
      Theta: ndarray, shape (n_users, k)
          The expected user latent factors.
      
      Beta: ndarray, shape (n_items, k)
          The expected item latent factors.
  
      G_s: ndarray, shape (n_users, k)
          This represents "shape" parameters of Gamma distribution over Theta.
  
      G_r: ndarray, shape (n_users, k)
          This represents "rate" parameters of Gamma distribution over Theta.
          
      L_s: ndarray, shape (n_items, k)
          This represents "shape" parameters of Gamma distribution over Beta.
      
      L_r: ndarray, shape (n_items, k)
          This represents "rate" parameters of Gamma distribution over Beta.
      
  References
  ----------
  * Gopalan, Prem, Jake M. Hofman, and David M. Blei. Scalable Recommendation with     Hierarchical Poisson Factorization. In UAI, pp. 326-335. 2015.
class_docstr_tok: ["Hierarchical","Poisson","Factorization",".","Parameters","--","--","--","--","--","k",":","int",",","optional",",","default",":","5","The","dimension","of","the","latent","factors",".","max_iter",":","int",",","optional",",","default",":","100","Maximum","number","of","iterations",".","name",":","string",",","optional",",","default",":","'HPF'","The","name","of","the","recommender","model",".","trainable",":","boolean",",","optional",",","default",":","True","When","False",",","the","model","is","not","trained","and","Cornac","assumes","that","the","model","is","already","pre-trained","(","Theta","and","Beta","are","not","None",")",".","verbose",":","boolean",",","optional",",","default",":","False","When","True",",","some","running","logs","are","displayed",".","hierarchical",":","boolean",",","optional",",","default",":","True","When","False",",","PF","is","used","instead","of","HPF",".","seed",":","int",",","optional",",","default",":","None","Random","seed","for","parameters","initialization",".","init_params",":","dict",",","optional",",","default",":","None","Initial","parameters","of","the","model",".","Theta",":","ndarray",",","shape","(","n_users",",","k",")","The","expected","user","latent","factors",".","Beta",":","ndarray",",","shape","(","n_items",",","k",")","The","expected","item","latent","factors",".","G_s",":","ndarray",",","shape","(","n_users",",","k",")","This","represents","``","shape","''","parameters","of","Gamma","distribution","over","Theta",".","G_r",":","ndarray",",","shape","(","n_users",",","k",")","This","represents","``","rate","''","parameters","of","Gamma","distribution","over","Theta",".","L_s",":","ndarray",",","shape","(","n_items",",","k",")","This","represents","``","shape","''","parameters","of","Gamma","distribution","over","Beta",".","L_r",":","ndarray",",","shape","(","n_items",",","k",")","This","represents","``","rate","''","parameters","of","Gamma","distribution","over","Beta",".","References","--","--","--","--","--","*","Gopalan",",","Prem",",","Jake","M.","Hofman",",","and","David","M.","Blei",".","Scalable","Recommendation","with","Hierarchical","Poisson","Factorization",".","In","UAI",",","pp",".","326-335",".","2015","."]
docstr: Fit the model to observations.
  
  Parameters
  ----------
  train_set: :obj:`cornac.data.Dataset`, required
      User-Item preference data as well as additional modalities.
  
  val_set: :obj:`cornac.data.Dataset`, optional, default: None
      User-Item preference data for model selection purposes (e.g., early stopping).
  
  Returns
  -------
  self : object
docstr_tok: ["Fit","the","model","to","observations",".","Parameters","--","--","--","--","--","train_set",":",":","obj",":","`","cornac.data.Dataset","`",",","required","User-Item","preference","data","as","well","as","additional","modalities",".","val_set",":",":","obj",":","`","cornac.data.Dataset","`",",","optional",",","default",":","None","User-Item","preference","data","for","model","selection","purposes","(","e.g.",",","early","stopping",")",".","Returns","--","--","--","-","self",":","object"]
code: def fit(self, train_set, val_set=None):
      """Fit the model to observations.
  
      Parameters
      ----------
      train_set: :obj:`cornac.data.Dataset`, required
          User-Item preference data as well as additional modalities.
  
      val_set: :obj:`cornac.data.Dataset`, optional, default: None
          User-Item preference data for model selection purposes (e.g., early stopping).
  
      Returns
      -------
      self : object
      """
      Recommender.fit(self, train_set, val_set)
  
      if self.trainable:
          # use pre-trained params if exists, otherwise from constructor
          init_params = {
              "G_s": self.Gs,
              "G_r": self.Gr,
              "L_s": self.Ls,
              "L_r": self.Lr,
          }
  
          X = train_set.csc_matrix
          # recover the striplet sparse format from csc sparse matrix X (needed to feed c++)
          (rid, cid, val) = sp.find(X)
          val = np.array(val, dtype="float32")
          rid = np.array(rid, dtype="int32")
          cid = np.array(cid, dtype="int32")
          tX = np.concatenate(
              (
                  np.concatenate(([rid], [cid]), axis=0).T,
                  val.reshape((len(val), 1)),
              ),
              axis=1,
          )
          del rid, cid, val
  
          if self.hierarchical:
              res = hpf.hpf(
                  tX,
                  X.shape[0],
                  X.shape[1],
                  self.k,
                  self.max_iter,
                  self.seed,
                  init_params,
              )
          else:
              res = hpf.pf(
                  tX,
                  X.shape[0],
                  X.shape[1],
                  self.k,
                  self.max_iter,
                  self.seed,
                  init_params,
              )
          self.Theta = np.asarray(res["Z"])
          self.Beta = np.asarray(res["W"])
  
          # overwrite init_params for future fine-tuning
          self.Gs = np.asarray(res["G_s"])
          self.Gr = np.asarray(res["G_r"])
          self.Ls = np.asarray(res["L_s"])
          self.Lr = np.asarray(res["L_r"])
  
      elif self.verbose:
          print(
              "%s is trained already (trainable = False)" % (self.name)
          )
  
      return self
  
file_path: cornac/cornac/models/hpf/recom_hpf.py
imports: import numpy
  import scipy.sparse
  from cornac.models.hpf import hpf
  from ..recommender import Recommender
  from ..recommender import ANNMixin, MEASURE_DOT
  from ...exception import ScoreException
label_desc: Use image node_id 2 for calling the HPF obj's underlying member method code with example usage: obj.fit(train_set, val_set) and returns: self