docstr: User KNN for Item Recommendation
  
  This algorithm predicts a rank for each user based on the similar items that his neighbors
  (similar users) consumed.
  
  Usage::
  
      >> UserKNN(train, test, as_similar_first=True).compute()
      >> UserKNN(train, test, ranking_file, as_binary=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 users. 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 rank_length: Size of the rank that must be generated by the predictions of the recommender algorithm
  :type rank_length: int, default 10
  
  :param as_binary: If True, the explicit feedback will be transform to binary
  :type as_binary: bool, default False
  
  :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 True
  
  :param sep: Delimiter for input files
  :type sep: str, default '       '
  
  :param output_sep: Delimiter for output file
  :type output_sep: str, default '        '
docstr_tok: ["User","KNN","for","Item","Recommendation","This","algorithm","predicts","a","rank","for","each","user","based","on","the","similar","items","that","his","neighbors","(","similar","users",")","consumed",".","Usage",":",":",">",">","UserKNN","(","train",",","test",",","as_similar_first=True",")",".compute","(",")",">",">","UserKNN","(","train",",","test",",","ranking_file",",","as_binary=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","users",".","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","rank_length",":","Size","of","the","rank","that","must","be","generated","by","the","predictions","of","the","recommender","algorithm",":","type","rank_length",":","int",",","default","10",":","param","as_binary",":","If","True",",","the","explicit","feedback","will","be","transform","to","binary",":","type","as_binary",":","bool",",","default","False",":","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","True",":","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,
      rank_length=10,
      as_binary=False,
      as_similar_first=True,
      sep="\t",
      output_sep="\t",
  ):
      """
      User KNN for Item Recommendation
  
      This algorithm predicts a rank for each user based on the similar items that his neighbors
      (similar users) consumed.
  
      Usage::
  
          >> UserKNN(train, test, as_similar_first=True).compute()
          >> UserKNN(train, test, ranking_file, as_binary=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 users. 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 rank_length: Size of the rank that must be generated by the predictions of the recommender algorithm
      :type rank_length: int, default 10
  
      :param as_binary: If True, the explicit feedback will be transform to binary
      :type as_binary: bool, default False
  
      :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 True
  
      :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(UserKNN, self).__init__(
          train_file=train_file,
          test_file=test_file,
          output_file=output_file,
          as_binary=as_binary,
          rank_length=rank_length,
          similarity_metric=similarity_metric,
          sep=sep,
          output_sep=output_sep,
      )
  
      self.recommender_name = "UserKNN Algorithm"
  
      self.as_similar_first = as_similar_first
      self.k_neighbors = k_neighbors
  
      # internal vars
      self.su_matrix = None
      self.users_id_viewed_item = None
  
file_path: CaseRecommender/caserec/recommenders/item_recommendation/userknn.py
imports: import numpy
  from caserec.recommenders.item_recommendation.base_item_recommendation import BaseItemRecommendation
  from caserec.utils.extra_functions import timed
label_desc: Use image node_id 1 to create a new UserKNN object from inherited base classes: BaseItemRecommendation with example: obj = UserKNN(train_file, test_file, output_file, similarity_metric, k_neighbors, rank_length, as_binary, as_similar_first, sep, output_sep)