def main(): users = db_connector.getAllWardrobeData() #start_time = time.time() matrix = getMatrix( users ) #print users[2] #u = input("Enter user (0-19): ") projectedRatings = {} for u in range( len(users) ): #print "- Getting kNN for User #" + str(u) NN = kNN( np.array( matrix[u] ), 4 )[1:] weights = assignWeights( u, NN, matrix ) #print weights #print "- Recommendations for User #" + str(u) #print #print "User #" + str(u) + "'s Nearest Neighbors and Weights:" projectedRatings[u] = getRecItems(u, weights, users) #print (time.time() - start_time) print projectedRatings """call = input("Enter user number: ") print projectedRatings[call]""" #print users """print "Angelica and Billie Jean: "
def main(): conn = db_connector.getDBConnection() cur = conn.cursor() wardrobes = db_connector.getAllWardrobeData() catalogue = db_connector.getCatalogueData() users = sorted(wardrobes.keys()) for user in users: # comment when not updating db #query = "DELETE FROM modifit_user_recommendations WHERE user_id = {0}" #cur.execute(query.format(user)) print "user: "******"category: " + str(category) # compute cosine similarities under the current category cosineSimilarities = {} for user2 in users: if user2 != user and category in wardrobes[user2]: a = getOrderedValues( wardrobes[user][category], wardrobes[user2][category] ) b = getOrderedValues( wardrobes[user2][category], wardrobes[user][category] ) cosineSimilarities[user2] = getCosineSimilarity( np.array( a ), np.array( b ) ) #print str(user2) + ": " + str(cosineSimilarities[user2]) #print kNN( cosineSimilarities, 5 ) # compute weighted ratings of items from nearest neighbors weightedRatings = getWeightedRatings( user, category, cosineSimilarities, wardrobes ) #print weightedRatings # pick the top 3 weighted ratings sortedRatings = sorted(weightedRatings.items(), key=lambda x: x[1], reverse=True) if len(sortedRatings) > 3: sortedRatings = sortedRatings[:3] # assign the top 3 as candidate items candidateItems = {} for rating in sortedRatings: candidateItems[rating[0]] = rating[1] #print candidateItems # compute projected ratings of candidate items using jaccard index # if projected rating >= 3, then item will be recommended to user projectedRatings = {} for item1 in candidateItems: for item2 in wardrobes[user][category]: score = getJaccardIndex(catalogue[category - 1][item1], catalogue[category - 1][item2]) rating = score * wardrobes[user][category][item2] if (item1 not in projectedRatings or score > projectedRatings[item1]) and rating >= 3: projectedRatings[item1] = rating #print projectedRatings # print recommendations and projected ratings for item in sorted(projectedRatings.keys()): print str(item) + ": " + str(projectedRatings[item]) # comment when not updating db """for item in catalogue[category - 1]: if item in projectedRatings: query = "INSERT INTO modifit_user_recommendations " \ + "(user_id, item_id, projected_rating, user_rating) " \ + "VALUES ({0}, {1}, {2}, 0);" try: cur.execute(query.format(user, item, projectedRatings[item])) except: print "Can't execute query""" print