docstr: Method to fit the model. Create and calculate a similarity matrix by metadata file or a pre-computed similarity
  matrix
docstr_tok: ["Method","to","fit","the","model",".","Create","and","calculate","a","similarity","matrix","by","metadata","file","or","a","pre-computed","similarity","matrix"]
code: def init_model(self):
      """
      Method to fit the model. Create and calculate a similarity matrix by metadata file or a pre-computed similarity
      matrix
  
      """
      self.users_id_viewed_item = {}
  
      # Set the value for k
      if self.k_neighbors is None:
          self.k_neighbors = int(np.sqrt(len(self.users)))
  
      for item in self.items:
          for user in self.train_set["users_viewed_item"].get(item, []):
              self.users_id_viewed_item.setdefault(item, []).append(
                  self.user_to_user_id[user]
              )
  
      if self.metadata_file is not None:
          metadata = ReadFile(
              self.metadata_file,
              sep=self.metadata_similarity_sep,
              as_binary=self.metadata_as_binary,
          ).read_metadata_or_similarity()
  
          self.matrix = np.zeros(
              (len(self.users), len(metadata["col_2"]))
          )
  
          meta_to_meta_id = {}
          for m, data in enumerate(metadata["col_2"]):
              meta_to_meta_id[data] = m
  
          for user_m in metadata["col_1"]:
              for m1 in metadata["dict"][user_m]:
                  try:
                      self.matrix[
                          self.user_to_user_id[user_m],
                          meta_to_meta_id[m1],
                      ] = metadata["dict"][user_m][m1]
                  except KeyError:
                      pass
  
          # create header info for metadata
          sparsity = (
              1
              - (
                  metadata["number_interactions"]
                  / (len(metadata["col_1"]) * len(metadata["col_2"]))
              )
          ) * 100
  
          self.extra_info_header = (
              ">> metadata:: %d users and %d metadata (%d interactions) | sparsity:: %.2f%%"
              % (
                  len(metadata["col_1"]),
                  len(metadata["col_2"]),
                  metadata["number_interactions"],
                  sparsity,
              )
          )
  
          # Create similarity matrix based on metadata or similarity file
          self.su_matrix = self.compute_similarity(transpose=False)
  
      elif self.similarity_file is not None:
          similarity = ReadFile(
              self.similarity_file,
              sep=self.metadata_similarity_sep,
              as_binary=False,
          ).read_metadata_or_similarity()
  
          self.su_matrix = np.zeros((len(self.users), len(self.users)))
  
          # Fill similarity matrix
          for u in similarity["col_1"]:
              for u_j in similarity["dict"][u]:
                  self.su_matrix[
                      self.user_to_user_id[u],
                      self.user_to_user_id[int(u_j)],
                  ] = similarity["dict"][u][u_j]
  
          # Remove NaNs
          self.su_matrix[np.isnan(self.su_matrix)] = 0.0
  
      else:
          raise ValueError(
              "This algorithm needs a similarity matrix or a metadata file!"
          )
  
      # Create original matrix user x item for prediction process
      self.create_matrix()
  
imports: import numpy
  from caserec.recommenders.rating_prediction.userknn import UserKNN
  from caserec.utils.process_data import ReadFile
label_desc: Use image node_id 2 for calling the UserAttributeKNN obj's underlying member method code with example usage: obj.init_model() without return types