docstr: ItemKNN for rating prediction
  
  Its philosophy is as follows: in order to determine the rating of User u on item m, we can find other items
  that are similar to item m, and based on User u’s ratings on those similar items we infer his rating on
  item m.
  
  Usage::
  
      >> ItemKNN(train, test).compute()
      >> ItemKNN(train, test, ranking_file, as_similar_first=True).compute()
  
  :param train_file: File which contains the train set. This file needs to have at least 3 columns
  (user item feedback_value).
  :type train_file: str
  
  :param test_file: File which contains the test set. This file needs to have at least 3 columns
  (user item feedback_value).
  :type test_file: str, default None
  
  :param output_file: File with dir to write the final predictions
  :type output_file: str, default None
  
  :param similarity_metric: Pairwise metric to compute the similarity between the items. Reference about
  distances: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.pdist.html
  :type similarity_metric: str, default cosine
  
  :param k_neighbors: Number of neighbors to use. If None, k_neighbor = int(sqrt(n_users))
  :type k_neighbors: int, default None
  
  :param as_similar_first: If True, for each unknown item, which will be predicted, we first look for its k
  most similar users and then take the intersection with the users that
  seen that item.
  :type as_similar_first: bool, default False
  
  :param sep: Delimiter for input files
  :type sep: str, default '       '
  
  :param output_sep: Delimiter for output file
  :type output_sep: str, default '        '
docstr_tok: ["ItemKNN","for","rating","prediction","Its","philosophy","is","as","follows",":","in","order","to","determine","the","rating","of","User","u","on","item","m",",","we","can","find","other","items","that","are","similar","to","item","m",",","and","based","on","User","u","\u2019","s","ratings","on","those","similar","items","we","infer","his","rating","on","item","m.","Usage",":",":",">",">","ItemKNN","(","train",",","test",")",".compute","(",")",">",">","ItemKNN","(","train",",","test",",","ranking_file",",","as_similar_first=True",")",".compute","(",")",":","param","train_file",":","File","which","contains","the","train","set",".","This","file","needs","to","have","at","least","3","columns","(","user","item","feedback_value",")",".",":","type","train_file",":","str",":","param","test_file",":","File","which","contains","the","test","set",".","This","file","needs","to","have","at","least","3","columns","(","user","item","feedback_value",")",".",":","type","test_file",":","str",",","default","None",":","param","output_file",":","File","with","dir","to","write","the","final","predictions",":","type","output_file",":","str",",","default","None",":","param","similarity_metric",":","Pairwise","metric","to","compute","the","similarity","between","the","items",".","Reference","about","distances",":","http",":","\/\/docs.scipy.org\/doc\/scipy-0.14.0\/reference\/generated\/scipy.spatial.distance.pdist.html",":","type","similarity_metric",":","str",",","default","cosine",":","param","k_neighbors",":","Number","of","neighbors","to","use",".","If","None",",","k_neighbor","=","int","(","sqrt","(","n_users",")",")",":","type","k_neighbors",":","int",",","default","None",":","param","as_similar_first",":","If","True",",","for","each","unknown","item",",","which","will","be","predicted",",","we","first","look","for","its","k","most","similar","users","and","then","take","the","intersection","with","the","users","that","seen","that","item",".",":","type","as_similar_first",":","bool",",","default","False",":","param","sep",":","Delimiter","for","input","files",":","type","sep",":","str",",","default","'","'",":","param","output_sep",":","Delimiter","for","output","file",":","type","output_sep",":","str",",","default","'","'"]
code: def __init__(
      self,
      train_file=None,
      test_file=None,
      output_file=None,
      similarity_metric="cosine",
      k_neighbors=None,
      as_similar_first=False,
      sep="\t",
      output_sep="\t",
  ):
      """
      ItemKNN for rating prediction
  
      Its philosophy is as follows: in order to determine the rating of User u on item m, we can find other items
      that are similar to item m, and based on User u’s ratings on those similar items we infer his rating on
      item m.
  
      Usage::
  
          >> ItemKNN(train, test).compute()
          >> ItemKNN(train, test, ranking_file, as_similar_first=True).compute()
  
      :param train_file: File which contains the train set. This file needs to have at least 3 columns
      (user item feedback_value).
      :type train_file: str
  
      :param test_file: File which contains the test set. This file needs to have at least 3 columns
      (user item feedback_value).
      :type test_file: str, default None
  
      :param output_file: File with dir to write the final predictions
      :type output_file: str, default None
  
      :param similarity_metric: Pairwise metric to compute the similarity between the items. Reference about
      distances: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.pdist.html
      :type similarity_metric: str, default cosine
  
      :param k_neighbors: Number of neighbors to use. If None, k_neighbor = int(sqrt(n_users))
      :type k_neighbors: int, default None
  
      :param as_similar_first: If True, for each unknown item, which will be predicted, we first look for its k
      most similar users and then take the intersection with the users that
      seen that item.
      :type as_similar_first: bool, default False
  
      :param sep: Delimiter for input files
      :type sep: str, default '\t'
  
      :param output_sep: Delimiter for output file
      :type output_sep: str, default '\t'
  
      """
  
      super(ItemKNN, self).__init__(
          train_file=train_file,
          test_file=test_file,
          output_file=output_file,
          sep=sep,
          output_sep=output_sep,
          similarity_metric=similarity_metric,
      )
  
      self.recommender_name = "ItemKNN Algorithm"
  
      self.as_similar_first = as_similar_first
      self.k_neighbors = k_neighbors
  
      # internal vars
      self.si_matrix = None
      self.similar_items = None
  
file_path: CaseRecommender/caserec/recommenders/rating_prediction/itemknn.py
imports: from collections import defaultdict
  import numpy
  from caserec.utils.extra_functions import timed
  from caserec.recommenders.rating_prediction.base_knn import BaseKNN
label_desc: Use image node_id 1 to create a new ItemKNN object from inherited base classes: BaseKNN with example: obj = ItemKNN(train_file, test_file, output_file, similarity_metric, k_neighbors, as_similar_first, sep, output_sep)