def process(path): clicked, lines_evaluated, lines_total = 0, 0, 0 with open(path) as inf: for line in inf: lines_total += 1 # Parsing the log line. logline = line.strip().split() chosen = int(logline.pop(7)) user_action = int(logline.pop(7)) time = int(logline[0]) user_features = [float(x) for x in logline[1:7]] articles = [int(x) for x in logline[7:]] # Getting the recommended article. calculated = policy.reccomend(time, user_features, articles) if not calculated in articles: raise Exception("Article was not in the list.") # Updating the policy. if calculated == chosen: policy.update(user_action) clicked += user_action lines_evaluated += 1 else: policy.update(-1) print "Evaluated %d/%d lines." % ( lines_evaluated, lines_total) print "CTR=%f" % (float(clicked) / lines_evaluated)
def _run_once(self, verbose=True): policy = construct_policy(self.simu_params) iter_rewards = construct_iter_rewards(self.simu_params) # Pre-allocate to significantly increase performance rewards = [[]] * self.max_time policy_rewards = [0.0] * self.max_time reward_sums = [0.0] * policy.num_arms arm_choices = [] for t, rewards_t in enumerate(iter_rewards): if t >= self.max_time: break rewards[t] = rewards_t arm = policy.choose_arm() arm_choices.append(arm) reward = rewards_t[arm] policy_rewards[t] = reward policy.update(arm, reward) for i, r in enumerate(rewards_t): reward_sums[i] += r if verbose and t % 1000 == 0: print >>sys.stderr, "%s: distro %s ran %s time steps" % ( self.simu_params['name'], self.simu_params['distro_name'], t) return rewards, reward_sums, policy_rewards, arm_choices
def _run_once(self, verbose=True): policy = construct_policy(self.simu_params) iter_rewards = construct_iter_rewards(self.simu_params) # Pre-allocate to significantly increase performance rewards = [[]] * self.max_time policy_rewards = [0.0] * self.max_time reward_sums = [0.0] * policy.num_arms arm_choices = [] for t, rewards_t in enumerate(iter_rewards): if t >= self.max_time: break rewards[t] = rewards_t arm = policy.choose_arm() arm_choices.append(arm) reward = rewards_t[arm] policy_rewards[t] = reward policy.update(arm, reward) for i, r in enumerate(rewards_t): reward_sums[i] += r if verbose and t % 1000 == 0: print >> sys.stderr, "%s: distro %s ran %s time steps" % ( self.simu_params['name'], self.simu_params['distro_name'], t) return rewards, reward_sums, policy_rewards, arm_choices
def main(args): if (len(args) != 3): raise Exception("Usage: ./evaluator.py articles_file log_file") with file(args[1]) as inf: articles = [] for line in inf: features = line.strip().split(" ") article = [int(features[0])] article.extend(float(x) for x in features[1:]) articles.append(article) policy.set_articles(articles) score = 0 total_evaluated = 0 n_lines = 0 with file(args[2]) as inf: for line in inf: n_lines += 1 logline = line.strip().split() chosen = int(logline.pop(7)) reward = int(logline.pop(7)) time = int(logline[0]) user_features = [float(x) for x in logline[1:7]] articles = [int(x) for x in logline[7:]] calculated = policy.reccomend(time, user_features, articles) if calculated == chosen: policy.update(reward) score += reward total_evaluated += 1 else: policy.update(-1) print "Evaluated %d/%d lines." % (total_evaluated, n_lines) print "CTR=%f" % (float(score) / total_evaluated)
def process(path): clicked, lines_evaluated, lines_total = 0, 0, 0 with file(sys.argv[2]) as inf: for line in inf: lines_total += 1 # Parsing the log line. logline = line.strip().split() chosen = int(logline.pop(7)) user_action = int(logline.pop(7)) time = int(logline[0]) user_features = [float(x) for x in logline[1:7]] articles = [int(x) for x in logline[7:]] # Getting the recommended article. calculated = policy.reccomend(time, user_features, articles) if calculated not in articles: raise Exception("Article was not in the list.") # Updating the policy. if calculated == chosen: policy.update(user_action) clicked += user_action lines_evaluated += 1 else: policy.update(-1) if lines_total % 1000 == 0: print "Evaluated %d/%d lines." % (lines_evaluated, lines_total) print "CTR = %f" % (float(clicked) / lines_evaluated) print "Number of articles viewed %d" % len(policy.A.keys()) print "Evaluated %d/%d lines." % (lines_evaluated, lines_total) print "CTR = %f" % (float(clicked) / lines_evaluated)
score = 0 total_evaluated = 0 n_lines = 0 with file(sys.argv[2]) as inf: for line in inf: n_lines += 1 logline = line.strip().split() chosen = int(logline.pop(7)) reward = int(logline.pop(7)) time = int(logline[0]) user_features = [float(x) for x in logline[1:7]] articles = [int(x) for x in logline[7:]] calculated = policy.reccomend(time, user_features, articles) if calculated == chosen: policy.update(reward) score += reward total_evaluated += 1 else: policy.update(-1) print "Evaluated %d/%d lines." % (total_evaluated, n_lines) print "CTR=%f" % (float(score) / total_evaluated) pr.disable() ps = pstats.Stats(pr, stream=open("profile.txt", "w")) ps.sort_stats("cumtime") ps.print_stats()
values = [items[1:] for items in articles] dictionary = dict(zip(keys, values)) policy.set_articles(dictionary) score = 0 total_evaluated = 0 n_lines = 0 with file(sys.argv[2]) as inf: for line in inf: n_lines += 1 logline = line.strip().split() chosen = int(logline.pop(7)) reward = int(logline.pop(7)) time = int(logline[0]) user_features = [float(x) for x in logline[1:7]] articles = [int(x) for x in logline[7:]] calculated = policy.reccomend(time, user_features, articles) if calculated == chosen: policy.update(reward) score += reward total_evaluated += 1 else: policy.update(-1) print "Evaluated %d/%d lines." % (total_evaluated, n_lines) print "CTR=%f" % (float(score) / total_evaluated)