class_docstr: Propensity-based Stratified Evaluation Method proposed by Jadidinejad et al. (2021)
  
  Parameters
  ----------
  data: array-like, required
      Raw preference data in the triplet format [(user_id, item_id, rating_value)].
  
  test_size: float, optional, default: 0.2
      The proportion of the test set, 
      if > 1 then it is treated as the size of the test set.
  
  val_size: float, optional, default: 0.0
      The proportion of the validation set,         if > 1 then it is treated as the size of the validation set.
  
  n_strata: int, optional, default: 2
      The number of strata for propensity-based stratification.
  
  rating_threshold: float, optional, default: 1.0
      Threshold used to binarize rating values into positive or negative feedback for
      model evaluation using ranking metrics (rating metrics are not affected).
  
  seed: int, optional, default: None
      Random seed for reproducibility.
  
  exclude_unknowns: bool, optional, default: True
      If `True`, unknown users and items will be ignored during model evaluation.
  
  verbose: bool, optional, default: False
      Output running log.
  
  References
  ----------
  Amir H. Jadidinejad, Craig Macdonald and Iadh Ounis, 
  The Simpson's Paradox in the Offline Evaluation of Recommendation Systems, 
  ACM Transactions on Information Systems (to appear)
  https://arxiv.org/abs/2104.08912
class_docstr_tok: ["Propensity-based","Stratified","Evaluation","Method","proposed","by","Jadidinejad","et","al",".","(","2021",")","Parameters","--","--","--","--","--","data",":","array-like",",","required","Raw","preference","data","in","the","triplet","format","[","(","user_id",",","item_id",",","rating_value",")","]",".","test_size",":","float",",","optional",",","default",":","0.2","The","proportion","of","the","test","set",",","if",">","1","then","it","is","treated","as","the","size","of","the","test","set",".","val_size",":","float",",","optional",",","default",":","0.0","The","proportion","of","the","validation","set",",","if",">","1","then","it","is","treated","as","the","size","of","the","validation","set",".","n_strata",":","int",",","optional",",","default",":","2","The","number","of","strata","for","propensity-based","stratification",".","rating_threshold",":","float",",","optional",",","default",":","1.0","Threshold","used","to","binarize","rating","values","into","positive","or","negative","feedback","for","model","evaluation","using","ranking","metrics","(","rating","metrics","are","not","affected",")",".","seed",":","int",",","optional",",","default",":","None","Random","seed","for","reproducibility",".","exclude_unknowns",":","bool",",","optional",",","default",":","True","If","`","True","`",",","unknown","users","and","items","will","be","ignored","during","model","evaluation",".","verbose",":","bool",",","optional",",","default",":","False","Output","running","log",".","References","--","--","--","--","--","Amir","H.","Jadidinejad",",","Craig","Macdonald","and","Iadh","Ounis",",","The","Simpson","'s","Paradox","in","the","Offline","Evaluation","of","Recommendation","Systems",",","ACM","Transactions","on","Information","Systems","(","to","appear",")","https",":","\/\/arxiv.org\/abs\/2104.08912"]
code: def _build_stratified_dataset(self, test_data):
      # build stratified datasets
      self.stratified_sets = {}
  
      # match the corresponding propensity score for each feedback
      test_props = np.array(
          [self.props[i] for u, i, r in test_data], dtype="float"
      )
  
      # stratify
      minp = min(test_props) - 0.01 * min(test_props)
      maxp = max(test_props) + 0.01 * max(test_props)
      slice = (maxp - minp) / self.n_strata
      strata = [
          f"Q{idx}"
          for idx in np.digitize(
              x=test_props, bins=np.arange(minp, maxp, slice)
          )
      ]
  
      for stratum in sorted(np.unique(strata)):
          # sample the corresponding sub-population
          qtest_data = []
          for (u, i, r), q in zip(test_data, strata):
              if q == stratum:
                  qtest_data.append((u, i, r))
  
          # build a dataset
          qtest_set = Dataset.build(
              data=qtest_data,
              fmt=self.fmt,
              global_uid_map=self.global_uid_map,
              global_iid_map=self.global_iid_map,
              seed=self.seed,
              exclude_unknowns=self.exclude_unknowns,
          )
          if self.verbose:
              print("---")
              print("Test data ({}):".format(stratum))
              print(
                  "Number of users = {}".format(len(qtest_set.uid_map))
              )
              print(
                  "Number of items = {}".format(len(qtest_set.iid_map))
              )
              print(
                  "Number of ratings = {}".format(qtest_set.num_ratings)
              )
              print("Max rating = {:.1f}".format(qtest_set.max_rating))
              print("Min rating = {:.1f}".format(qtest_set.min_rating))
              print(
                  "Global mean = {:.1f}".format(qtest_set.global_mean)
              )
              print(
                  "Number of unknown users = {}".format(
                      qtest_set.num_users - self.train_set.num_users
                  )
              )
              print(
                  "Number of unknown items = {}".format(
                      self.test_set.num_items - self.train_set.num_items
                  )
              )
  
          self.stratified_sets[stratum] = qtest_set
  
file_path: cornac/cornac/eval_methods/propensity_stratified_evaluation.py
imports: import time
  from collections import defaultdict
  from collections import OrderedDict
  import powerlaw
  import numpy
  import tqdm.auto
  from ..utils.common import safe_indexing
  from ..data import Dataset
  from .base_method import BaseMethod, rating_eval
  from .ratio_split import RatioSplit
  from ..experiment.result import Result, PSTResult
label_desc: Use image node_id 5 for calling the PropensityStratifiedEvaluation obj's underlying member method code with example usage: obj._build_stratified_dataset(test_data) without return types