def initialize_user(self, u, delete_user=True): """add user to the EventActionAggregate""" self.users.append((u, delete_user)) for c in Category.objects.all(): try: #See if event action aggregate exists.If it does, reset it to default. eaa = EventActionAggregate.objects.get(user=u,category=c) except: #Else create one. eaa = EventActionAggregate(user=u, category=c) eaa.g, eaa.v, eaa.i, eaa.x = 0, 0, 0, 0 eaa.save()
def __init__(self, user=None, categories=None, num_categories=None): """ Initialize a new user and prepare to get recommendations from scratch. If given categories, use those as preferred, otherwise if given number of categories, create this many random ones """ if user: self.user = user self.delete_user = False else: # Create and assign a new user. # if necessary later delete the user as well. But this might not be # necessary since in Django we will be working with a fresh database # that will in any case be destructed at the end of testing. self.delete_user = True success = False count = 0 while not success: count += 1 try: self.user = User(username="******" + str(count), password='******' + str(count)) self.user.save() success = True except: success = False for c in Category.objects.all(): try: #See if event action aggregate exists.If it does, reset it to default. eaa = EventActionAggregate.objects.get(user=self.user, category=c) except: #Else create one. eaa = EventActionAggregate(user=self.user, category=c) eaa.g, eaa.v, eaa.i, eaa.x = 0, 0, 0, 0 eaa.save() if categories: categories = [self.get_category_id(c) for c in categories] self.preferred_categories = set([c for c in categories]) else: self.preferred_categories = set( [c.id for c in self.get_random_categories(num_categories)])
def __init__(self, user=None, categories=None, num_categories=None): """ Initialize a new user and prepare to get recommendations from scratch. If given categories, use those as preferred, otherwise if given number of categories, create this many random ones """ if user: self.user = user self.delete_user = False else: # Create and assign a new user. # if necessary later delete the user as well. But this might not be # necessary since in Django we will be working with a fresh database # that will in any case be destructed at the end of testing. self.delete_user = True success = False count = 0 while not success: count += 1 try: self.user = User(username="******"+str(count), password='******'+str(count)) self.user.save() success = True except: success = False for c in Category.objects.all(): try: #See if event action aggregate exists.If it does, reset it to default. eaa = EventActionAggregate.objects.get(user=self.user,category=c) except: #Else create one. eaa = EventActionAggregate(user=self.user, category=c) eaa.g, eaa.v, eaa.i, eaa.x = 0, 0, 0, 0 eaa.save() if categories: categories = [self.get_category_id(c) for c in categories] self.preferred_categories = set([c for c in categories]) else: self.preferred_categories = set([c.id for c in self.get_random_categories(num_categories)])
def test_multi_category_recall(self,user=None): if not user: user = self.user for c in Category.objects.all(): EventActionAggregate(user=user,category=c).save() # Each color corresponds to the number of categories (position in list + 1) selected during the iteration. colors = ["r","b","g","k","c","m","y"] # k is the number of categories selected. for k in range(1,8): categories = set(ml.recommend_categories(user)) picked_categories = set(random.sample(categories,k)) print "User picked categories: ", picked_categories # Generate mapping between categories and User's event action aggregate (GVIX store) picked_cat_aggregates = dict([(c,EventActionAggregate.objects.get(user=user, category=c)) for c in picked_categories]) trials = count() lst = [] while trials.next() < 50: print "Loop: ", trials mean_loop_count = count() recall = 0 iterations = 10 G = {} #temporary dictionary that store G counts for selected categories during iterations. while mean_loop_count.next() < iterations: cats = set(ml.recommend_categories(user)) correct_recommendations = cats.intersection(picked_categories) for c in correct_recommendations: try: G[c] += 1 except: G[c] = 1 #picked_cat_aggregates[x].save() cats.discard(picked_cat_aggregates[c]) #end of looping over correct_recommendations recall += len(correct_recommendations)*100.0/len(picked_categories) #end of looping over iterations to calculate means. ##### #print "Recall: ", recall #set and save the x values of discarded categories to the average number of times the category was G'd over iterations. for key,value in G.iteritems(): picked_cat_aggregates[key].g += min(round(value/iterations),2) picked_cat_aggregates[key].save() #end of looping over temporary dictionary. lst.append(recall*1.0/iterations) #Variant 1: All items in the last iterations that were not in the picked category have been X'd once. for c in cats: try: eaa = EventActionAggregate.objects.get(user=user, category=c) except EventActionAggregate.DoesNotExist: eaa = EventActionAggregate(user=user, category=c) eaa.x += 1 eaa.save() #end of adding X's #end print "Recall: ",lst plt.plot(lst,color=colors[k-1],label=k) for c in Category.objects.all(): #import pdb; pdb.set_trace() try: eaa = EventActionAggregate.objects.get(user=user, category=c) eaa.g = 0 eaa.v = 0 eaa.i = 0 eaa.x = 0 eaa.save() except: pass plt.title("Recall") plt.xlabel("Trials") plt.ylabel("% of User preferred categories") #plt.legend() plt.savefig("learning/test_results/recall.pdf") plt.cla() #import pdb; pdb.set_trace() self.assertTrue(True)