class_docstr: Spark Evaluator for diversity, coverage, novelty, serendipity
class_docstr_tok: ["Spark","Evaluator","for","diversity",",","coverage",",","novelty",",","serendipity"]
docstr: Cosine similarity metric from
  
  :Citation:
  
      Y.C. Zhang, D.Ó. Séaghdha, D. Quercia and T. Jambor, Auralist:
      introducing serendipity into music recommendation, WSDM 2012
  
  The item indexes in the result are such that i1 <= i2.
docstr_tok: ["Cosine","similarity","metric","from",":","Citation",":","Y.C",".","Zhang",",","D.\u00d3",".","S\u00e9aghdha",",","D.","Quercia","and","T.","Jambor",",","Auralist",":","introducing","serendipity","into","music","recommendation",",","WSDM","2012","The","item","indexes","in","the","result","are","such","that","i1","<","=","i2","."]
code: def _get_cooccurrence_similarity(self, n_partitions):
      """Cosine similarity metric from
  
      :Citation:
  
          Y.C. Zhang, D.Ó. Séaghdha, D. Quercia and T. Jambor, Auralist:
          introducing serendipity into music recommendation, WSDM 2012
  
      The item indexes in the result are such that i1 <= i2.
      """
      if self.df_cosine_similarity is None:
          pairs = self._get_pairwise_items(df=self.train_df)
          item_count = self.train_df.groupBy(self.col_item).count()
  
          self.df_cosine_similarity = (
              pairs.groupBy("i1", "i2")
              .count()
              .join(
                  item_count.select(
                      F.col(self.col_item).alias("i1"),
                      F.pow(F.col("count"), 0.5).alias("i1_sqrt_count"),
                  ),
                  on="i1",
              )
              .join(
                  item_count.select(
                      F.col(self.col_item).alias("i2"),
                      F.pow(F.col("count"), 0.5).alias("i2_sqrt_count"),
                  ),
                  on="i2",
              )
              .select(
                  "i1",
                  "i2",
                  (
                      F.col("count")
                      / (
                          F.col("i1_sqrt_count")
                          * F.col("i2_sqrt_count")
                      )
                  ).alias(self.sim_col),
              )
              .repartition(n_partitions, "i1", "i2")
          )
      return self.df_cosine_similarity
  
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 4 for calling the SparkDiversityEvaluation obj's underlying member method code with example usage: obj._get_cooccurrence_similarity(n_partitions) and returns: self