class_docstr: Spark Ranking Evaluator
class_docstr_tok: ["Spark","Ranking","Evaluator"]
docstr: Initialization.
  This is the Spark version of ranking metrics evaluator.
  The methods of this class, calculate ranking metrics such as precision@k, recall@k, ndcg@k, and mean average
  precision.
  
  The implementations of precision@k, ndcg@k, and mean average precision are referenced from Spark MLlib, which
  can be found at `here <https://spark.apache.org/docs/2.3.0/mllib-evaluation-metrics.html#ranking-systems>`_.
  
  Args:
      rating_true (pyspark.sql.DataFrame): DataFrame of true rating data (in the
          format of customerID-itemID-rating tuple).
      rating_pred (pyspark.sql.DataFrame): DataFrame of predicted rating data (in
          the format of customerID-itemID-rating tuple).
      col_user (str): column name for user.
      col_item (str): column name for item.
      col_rating (str): column name for rating.
      col_prediction (str): column name for prediction.
      k (int): number of items to recommend to each user.
      relevancy_method (str): method for determining relevant items. Possible
          values are "top_k", "by_time_stamp", and "by_threshold".
      threshold (float): threshold for determining the relevant recommended items.
          This is used for the case that predicted ratings follow a known
          distribution. NOTE: this option is only activated if relevancy_method is
          set to "by_threshold".
docstr_tok: ["Initialization",".","This","is","the","Spark","version","of","ranking","metrics","evaluator",".","The","methods","of","this","class",",","calculate","ranking","metrics","such","as","precision","@","k",",","recall","@","k",",","ndcg","@","k",",","and","mean","average","precision",".","The","implementations","of","precision","@","k",",","ndcg","@","k",",","and","mean","average","precision","are","referenced","from","Spark","MLlib",",","which","can","be","found","at","`","here","<","https",":","\/\/spark.apache.org\/docs\/2.3.0\/mllib-evaluation-metrics.html","#","ranking-systems",">","`","_",".","Args",":","rating_true","(","pyspark.sql.DataFrame",")",":","DataFrame","of","true","rating","data","(","in","the","format","of","customerID-itemID-rating","tuple",")",".","rating_pred","(","pyspark.sql.DataFrame",")",":","DataFrame","of","predicted","rating","data","(","in","the","format","of","customerID-itemID-rating","tuple",")",".","col_user","(","str",")",":","column","name","for","user",".","col_item","(","str",")",":","column","name","for","item",".","col_rating","(","str",")",":","column","name","for","rating",".","col_prediction","(","str",")",":","column","name","for","prediction",".","k","(","int",")",":","number","of","items","to","recommend","to","each","user",".","relevancy_method","(","str",")",":","method","for","determining","relevant","items",".","Possible","values","are","``","top_k","''",",","``","by_time_stamp","''",",","and","``","by_threshold","''",".","threshold","(","float",")",":","threshold","for","determining","the","relevant","recommended","items",".","This","is","used","for","the","case","that","predicted","ratings","follow","a","known","distribution",".","NOTE",":","this","option","is","only","activated","if","relevancy_method","is","set","to","``","by_threshold","''","."]
code: def __init__(
      self,
      rating_true,
      rating_pred,
      k=DEFAULT_K,
      relevancy_method="top_k",
      col_user=DEFAULT_USER_COL,
      col_item=DEFAULT_ITEM_COL,
      col_rating=DEFAULT_RATING_COL,
      col_prediction=DEFAULT_PREDICTION_COL,
      threshold=DEFAULT_THRESHOLD,
  ):
      """Initialization.
      This is the Spark version of ranking metrics evaluator.
      The methods of this class, calculate ranking metrics such as precision@k, recall@k, ndcg@k, and mean average
      precision.
  
      The implementations of precision@k, ndcg@k, and mean average precision are referenced from Spark MLlib, which
      can be found at `here <https://spark.apache.org/docs/2.3.0/mllib-evaluation-metrics.html#ranking-systems>`_.
  
      Args:
          rating_true (pyspark.sql.DataFrame): DataFrame of true rating data (in the
              format of customerID-itemID-rating tuple).
          rating_pred (pyspark.sql.DataFrame): DataFrame of predicted rating data (in
              the format of customerID-itemID-rating tuple).
          col_user (str): column name for user.
          col_item (str): column name for item.
          col_rating (str): column name for rating.
          col_prediction (str): column name for prediction.
          k (int): number of items to recommend to each user.
          relevancy_method (str): method for determining relevant items. Possible
              values are "top_k", "by_time_stamp", and "by_threshold".
          threshold (float): threshold for determining the relevant recommended items.
              This is used for the case that predicted ratings follow a known
              distribution. NOTE: this option is only activated if relevancy_method is
              set to "by_threshold".
      """
      self.rating_true = rating_true
      self.rating_pred = rating_pred
      self.col_user = col_user
      self.col_item = col_item
      self.col_rating = col_rating
      self.col_prediction = col_prediction
      self.threshold = threshold
  
      # Check if inputs are Spark DataFrames.
      if not isinstance(self.rating_true, DataFrame):
          raise TypeError(
              "rating_true should be but is not a Spark DataFrame"
          )  # pragma : No Cover
  
      if not isinstance(self.rating_pred, DataFrame):
          raise TypeError(
              "rating_pred should be but is not a Spark DataFrame"
          )  # pragma : No Cover
  
      # Check if columns exist.
      true_columns = self.rating_true.columns
      pred_columns = self.rating_pred.columns
  
      if self.col_user not in true_columns:
          raise ValueError(
              "Schema of rating_true not valid. Missing User Col: "
              + str(true_columns)
          )
      if self.col_item not in true_columns:
          raise ValueError(
              "Schema of rating_true not valid. Missing Item Col"
          )
      if self.col_rating not in true_columns:
          raise ValueError(
              "Schema of rating_true not valid. Missing Rating Col"
          )
  
      if self.col_user not in pred_columns:
          raise ValueError(
              "Schema of rating_pred not valid. Missing User Col"
          )  # pragma : No Cover
      if self.col_item not in pred_columns:
          raise ValueError(
              "Schema of rating_pred not valid. Missing Item Col"
          )  # pragma : No Cover
      if self.col_prediction not in pred_columns:
          raise ValueError(
              "Schema of rating_pred not valid. Missing Prediction Col"
          )
  
      self.k = k
  
      relevant_func = {
          "top_k": _get_top_k_items,
          "by_time_stamp": _get_relevant_items_by_timestamp,
          "by_threshold": _get_relevant_items_by_threshold,
      }
  
      if relevancy_method not in relevant_func:
          raise ValueError(
              "relevancy_method should be one of {}".format(
                  list(relevant_func.keys())
              )
          )
  
      self.rating_pred = (
          relevant_func[relevancy_method](
              dataframe=self.rating_pred,
              col_user=self.col_user,
              col_item=self.col_item,
              col_rating=self.col_prediction,
              threshold=self.threshold,
          )
          if relevancy_method == "by_threshold"
          else relevant_func[relevancy_method](
              dataframe=self.rating_pred,
              col_user=self.col_user,
              col_item=self.col_item,
              col_rating=self.col_prediction,
              k=self.k,
          )
      )
  
      self._metrics = self._calculate_metrics()
  
imports: import numpy
  from recommenders.utils.constants import DEFAULT_PREDICTION_COL, DEFAULT_USER_COL, DEFAULT_ITEM_COL, DEFAULT_RATING_COL, DEFAULT_RELEVANCE_COL, DEFAULT_SIMILARITY_COL, DEFAULT_ITEM_FEATURES_COL, DEFAULT_ITEM_SIM_MEASURE, DEFAULT_TIMESTAMP_COL, DEFAULT_K, DEFAULT_THRESHOLD
label_desc: Use image node_id 1 to create a new SparkRankingEvaluation object  with example: obj = SparkRankingEvaluation(rating_true, rating_pred, k, relevancy_method, col_user, col_item, col_rating, col_prediction, threshold)