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.similar_items = defaultdict(list)
  
      # Set the value for k
      if self.k_neighbors is None:
          self.k_neighbors = int(np.sqrt(len(self.items)))
  
      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.items), len(metadata["col_2"]))
          )
  
          meta_to_meta_id = {}
          for m, data in enumerate(metadata["col_2"]):
              meta_to_meta_id[data] = m
  
          for item in metadata["col_1"]:
              for m in metadata["dict"][item]:
                  self.matrix[
                      self.item_to_item_id[item], meta_to_meta_id[m]
                  ] = metadata["dict"][item][m]
  
          # 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 items 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. Transpose=False, because it is an
          # item x metadata matrix
          self.si_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.si_matrix = np.zeros((len(self.items), len(self.items)))
  
          # Fill similarity matrix
          for i in similarity["col_1"]:
              for i_j in similarity["dict"][i]:
                  self.si_matrix[
                      self.item_to_item_id[i],
                      self.item_to_item_id[int(i_j)],
                  ] = similarity["dict"][i][i_j]
  
          # Remove NaNs
          self.si_matrix[np.isnan(self.si_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()
  
      for i_id, item in enumerate(self.items):
          self.similar_items[i_id] = sorted(
              range(len(self.si_matrix[i_id])),
              key=lambda k: -self.si_matrix[i_id][k],
          )[1 : self.k_neighbors + 1]
  
file_path: CaseRecommender/caserec/recommenders/item_recommendation/item_attribute_knn.py
imports: from collections import defaultdict
  import numpy
  from caserec.recommenders.item_recommendation.itemknn import ItemKNN
  from caserec.utils.process_data import ReadFile
label_desc: Use image node_id 2 for calling the ItemAttributeKNN obj's underlying member method code with example usage: obj.init_model() without return types