def main_page(request): import recommendations as rec # recommended posts # get user data user = request.user handler = ratings.get_handler(BlogPost) # others = User.objects.filter().exclude(username=user) # for test all users users = User.objects.all() critics = {} for other in users: scores = {} for vote in ratings.get_votes_by(other): scores[vote.content_object.title] = vote.score # print "%s -> %s" % (vote.content_object.title, vote.score) if scores != {}: critics[other] = scores print rec.getRecommendations(critics, user) # latest avtivities from following people following_people = [followingship.followers for followingship in user.following_set.all()] following_people_blogposts = BlogPost.objects.filter( user__in=following_people, status=BlogPost.LIVE_STATUS, ).order_by('-id') MAX_WEIGHT = 5 tags = Tag.objects.order_by('name') # calculate tag, min and max counts. min_count = max_count = tags[0].blogposts.count() for tag in tags: tag.count = tag.blogposts.count() if tag.count < min_count: min_count = tag.count if max_count < tag.count: max_count = tag.count #calculate count range. Avoid dividing by zero. range = float(max_count - min_count) if range == 0.0: range = 1.0 #calculate tag weights. for tag in tags: tag.weight = int( MAX_WEIGHT * (tag.count - min_count) / range ) variables = RequestContext(request, { # 'username': user, 'following_people': following_people, 'blogposts': following_people_blogposts, 'show_tags': True, 'show_user': True, 'show_body': True, 'tags': tags, }) # return render_to_response('tag_cloud_page.html', variables) return render_to_response('main_page.html', variables)
def run(): database = db.connect() cursor = db.getCursor(database) t1 = time.time() results = recommendations.getRecommendations(cursor, 3) t2 = time.time() prettyPrint(cursor, 3, results) t3 = time.time() print "REQUIRED TIME FOR RECOMENDATIONS: %0.3f ms, for querying and printing: %0.3f ms" % ( (t2 - t1) * 1000.0, (t3 - t2) * 1000.0) results = recommendations.getRecommendations(cursor, 4) prettyPrint(cursor, 4, results) db.disconnect(cursor)
def recommend(): rownames1, colnames1, data1, trainingGameExamples = rec.readfile( 'TrainingGameExamples.csv') rownames2, colnames2, data2, todaysGameExamples = rec.readfile( 'TodaysGameExamples.csv') trainingGameExamples.update(todaysGameExamples) print("ClearGameRecommendations") requests.get( "http://localhost/guerillalogisticsapi/MachineLearning/ClearGameRecommendations" ) for key, dict in todaysGameExamples.items(): predictions = rec.getRecommendations(trainingGameExamples, key) payload = {} payload["key"] = key for pair in predictions: payload[pair[1]] = pair[0] r = requests.get( "http://localhost/guerillalogisticsapi/MachineLearning/SaveGameRecommendation", params=payload) print(key) print("AdjustGameRecommendations") requests.get( "http://localhost/guerillalogisticsapi/MachineLearning/AdjustGameRecommendations" )
def formrec(): """ crates recommendations for user """ collection = db['user_ratings'] linkcollection = db['song_link'] newcollection = db['user_recommendations'] master = {} rec_list = list(collection.distinct('uID')) #when empty ie no similar user or recommendations if not rec_list: defcollection = db['song_default'] master['songs'] = "" return master for user in rec_list: tempdict = {} tempdict.setdefault(user, []) udata = reco.getRecommendations(reco.master, user) for data in udata: try: tempdict[user].append( (list(linkcollection.find({'_id': data[1]}))[-1])) except IndexError: pass #print user master['songs'] = tempdict #pp.pprint(master) newcollection.save({'_id': user, 'data': master})
def main(): prefs = r.loadMovieLens( os.getcwd() + "/ml-100k") # Calculates the preferences using recommendations.py movie_recommendations = r.getRecommendations( prefs, str(sub_id)) # Calculates all recommendations for sub user. print("\nThe most recommended movies for " + get_user(sub_id) + " (which is the substitute) are:") for i in range( 5): # This section prints out the recommendations for sub user. u = movie_recommendations[i][1] print(u) print("\nThe least recommended movies for the substitute are: ") for i in range(5): u = movie_recommendations[len(movie_recommendations) - (i + 1)][1] print(u) similiar_item_data = r.calculateSimilarItems( prefs) # Calculate all similar movie data. print("\nFive similar movies to my favorite movie " + favorite_movie + " are:") fav_mov_sim = similiar_item_data[favorite_movie][ 0:5] # For real me, some recommended movies for my preferences. for mov in fav_mov_sim: print(mov[1] + " " + str(mov[0])) print("\nFive similar movies to my least favorite movie " + least_favorite_movie + " are:") lea_fav_mov_sim = similiar_item_data[least_favorite_movie][0:5] for mov in lea_fav_mov_sim: print(mov[1] + " " + str(mov[0]))
def testSanity(self): expectList = recommendations.getRecommendations(recommendations.critics, "Toby") actualList = myRec.getRecommendations(myRec.critics, "Toby") for i, j in zip(expectList, actualList): if abs(i[0] - j[0]) > 0.0000001: self.fail("%f %f" % (i[0], j[0])) #self.assertAlmostEqual(i[0], j[0], 0.001) expectList = recommendations.getRecommendations(recommendations.critics, "Toby", recommendations.sim_distance) actualList = myRec.getRecommendations(myRec.critics, "Toby", myRec.sim_distance) for i, j in zip(expectList, actualList): if abs(i[0] - j[0]) > 0.0000001: self.fail("%f %f" % (i[0], j[0]))
def run_assignment_2_3(): print "-"*50, "\n", "assignment 2.3: User based collaborative filtering (UCF)\n", "-"*50 recommendations = reco.getRecommendations(reco.critics,'Toby',reco.sim_pearson) for i in recommendations: print i print "-"*50
def get_recommendations(): user_data = json.loads(request.data) items = getRecommendations(user_data["user_id"]) resp = make_response( json.dumps(list(items), sort_keys=True, indent=4, default=json_util.default)) return resp
def testBasics(self): r = recommendations.getRecommendations(self.data, 'Nico', similarity=recommendations.sim_distance) #print recommendations.sim_distance(self.data, 'Nico', 'Yann') #print recommendations.sim_distance(self.data, 'Nico', 'Josh') #print recommendations.sim_distance(self.data, 'Nico', 'Kerstin') #print r self.assertEquals(['Mathematica', 'Chocolate', 'Patterns'], [n for s,n in r])
def test_getRecommendations(self): actual = rec.getRecommendations(rec.critics,'Toby') expected = [(3.3477895267131013, 'The Night Listener'), (2.8325499182641614, 'Lady in the Water'), (2.5309807037655645, 'Just My Luck')] self.assertIsInstance(actual, list) self.assertEqual( round_sequence(actual), round_sequence(expected))
def _suggestions(user): """ The function gives the suggestions for a given user, by calling the recommendation algorithm. Args: user (dict): An anonym or real user existing in the data base. Return: The suggestions for the user. Raise: An Excpetion if no suggestions are available. """ users = handler.get_documents('users_info') + handler.get_documents( 'frontend_users') prefs = {} for u in users: prefs.update(_transform_for_suggestions(u)) reco_ids = recommendations.getRecommendations(prefs, user['_id']) method = "pearson" # if no recommendation found with pearson sim # distance we use the euclidean instead if not reco_ids: method = "euclidean" reco_ids = recommendations.getRecommendations( prefs, user['_id'], recommendations.sim_distance) suggestions = [] for score, place_id in reco_ids: place = handler.get_documents('places', one=True, query={'_id': place_id}) place['score'] = score suggestions.append(place) if not suggestions: raise Exception("No suggestions available") return (suggestions, method)
def testMovieLens(): import recommendations prefs = loadMovieLens() userId = '87' print prefs[userId] start = time.clock() print recommendations.getRecommendations(prefs, userId)[0:30] print "Total Time:" + str(time.clock() - start) print print "Preprocessing..." itemMatches = recommendations.calculateSimilarItems(prefs, n=50) print start = time.clock() print recommendations.getRecommendedItems(prefs, itemMatches, userId)[0:30] print "Total Time:" + str(time.clock() - start)
def recomienda(id_usuario): usuarios_libros = {} try: usuarios_libros = pickle.load(open("diccionario.p", "rb")) if not id_usuario in usuarios_libros.keys(): calcula_valoraciones() usuarios_libros = pickle.load(open("diccionario.p", "rb")) except IOError: calcula_valoraciones() usuarios_libros = pickle.load(open("diccionario.p", "rb")) return recommendations.getRecommendations(usuarios_libros, id_usuario)
def recomsummary(): apikey = request.args.get('apikey') data_test_id = request.args.get('data') k = request.args.get('k') if apikey == 'abcd': if data_test_id == 'none': abort(404) else: vectorizer = joblib.load("vector-summary.pkl") ids_data = joblib.load("ids.pkl") recom = recommend.getRecommendations(ids_data, vectorizer, data_test_id, int(k)) return jsonify(recom)
def recommendations(): apikey = request.args.get('apikey') data_test_id = request.args.get('data') k = request.args.get('k') #data_test_id es el id a analizar if apikey == 'abcd': #obtener datos de entrenamiento de archivo vector-cover train_data = joblib.load("vector-cover.pkl") #obtener ids de archivo ids ids_data = joblib.load("ids.pkl") recom = recommend.getRecommendations(ids_data, train_data, data_test_id, int(k)) return jsonify(recom)
def recomienda(id_usuario): usuarios_libros={} try: usuarios_libros=pickle.load( open( "diccionario.p", "rb" ) ) if not id_usuario in usuarios_libros.keys(): calcula_valoraciones() usuarios_libros=pickle.load( open( "diccionario.p", "rb" ) ) except IOError: calcula_valoraciones() usuarios_libros=pickle.load( open( "diccionario.p", "rb" ) ) return recommendations.getRecommendations(usuarios_libros, id_usuario)
def testDelicious(): import recommendations delusers = initializeUserDict('programming') fillItems(delusers) user = delusers.keys()[random.randint(0, len(delusers) - 1)] print user print recommendations.topMatches(delusers, user, similarity=recommendations.simEuclidean) recUrls = recommendations.getRecommendations(delusers, user, similarity=recommendations.simEuclidean)[0:10] print recUrls url = recUrls[0][1] print url print recommendations.topMatches(recommendations.transformPrefs(delusers), url)
def main(): print 'Audioscrobbler/Last.fm Recommendations by Chris Coykendall ' print '------------------------------------------------------------' if network: # Get user to recommend to user=raw_input('Enter user to recommend to: ') # DEBUG user='******' numFriends=raw_input('Enter # of friends to collect listen data from: ') print 'Fetching Last.fm API data for ' + user + '...' # Get the fan's friends friends=getFanFriends(user,int(numFriends)) if friends==None: return print 'Retrieved ' + str(len(friends)-1) + ' friends from ' + user + '...' # Get the fan and friends top artists based on relative number of artist plays print 'Fetching most listened artists from user and their friends...' fanArtistData=getArtistPlaysDataSet(friends) # Use similarity to determine top 5 similar users based on artists played closestMatches= recommendations.topMatches(fanArtistData,user,5) print '>>>>> Top 5 Similar Users out of ' + str(len(closestMatches)) + ' friends:' for match in closestMatches[:5]: print match[1] + ' (' + str(round(((match[0]+1)/2)*100,1)) + '%)' print '<<<<<' # Use Pearson score to determine top 20 recommended artists for user from friends listens recArtists= recommendations.getRecommendations(fanArtistData,user) print '>>>>> Top 20 Recommended Artists out of ' + str(len(recArtists)) sumIt=0 for match in recArtists[:20]: print match[1] + ' (' + str(round(match[0]*20,1)) + '%)' print '<<<<<' # Ask whether or not to display the data set if raw_input('Would you like to display the data dictionary (Y/N)?: ').upper()=='Y': for user in fanArtistData: print user for artist in fanArtistData[user]: print '\t' + artist + ': ' + str(round(fanArtistData[user][artist],1))
def get_recommendations(self, type='user_similarity', similarity='pearson'): from recommendations import getRecommendations, calculateSimilarItems, get_similarity, getRecommendedItems if type == 'user_similarity': users_prefs = User.get_users_ratings_dict(lazy_evaluation=False) sim = get_similarity(similarity) return [ (star, Product.get_by_id(productid)) for star, productid in getRecommendations(users_prefs, self.id, similarity=sim) ] elif type == 'item_similarity': users_prefs = User.get_users_ratings_dict(lazy_evaluation=True) similar_items = ItemSimilarity.get_similarity_dict() return [ (star, Product.get_by_id(productid)) for star, productid in getRecommendedItems(users_prefs, similar_items, self.id) ] else: raise ValueError('Unknown recommendation type %s' % type)
def index(): if request.method == "POST": results = {} # find imdb url url = request.form.get("ratings-url") # parse ratings ratings = get_ratings(url) critics["user"] = ratings # run getRecommendations results = getRecommendations(critics, "user") results = {score: imdbID for score, imdbID in results if score > 3} return render_template("index.html", results=results, movie_names=movie_names) else: return render_template("index.html")
import recommendations print('-------') movies = recommendations.transformPrefs(recommendations.critics) result = recommendations.topMatches(movies, 'Supermen Returns') print(result) print('-------') result = recommendations.getRecommendations(movies, 'Just My Luck') print(result)
'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 'Just My Luck': 2.0, 'Superman Returns': 3.0,\ 'You, Me and Dupree': 2.0, 'The Night Listener': 3.0}, \ 'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 'Superman Returns': 5.0, \ 'You, Me and Dupree': 3.5, 'The Night Listener':3.0}, \ 'Toby': {'Snakes on a Plane': 4.5, 'Superman Returns': 4.0, 'You, Me and Dupree': 1.0}} list_of_recommendations = recommendations.topMatches(critics, 'Toby', n=3) print('when algorithm of similarity is sim_distance:') print(list_of_recommendations) list_of_recommendations = recommendations.topMatches( critics, 'Toby', n=3, similarity=recommendations.sim_pearson) print('when algorithm of similarity is sim_pearson:') print(list_of_recommendations) person_getRecommendations = recommendations.getRecommendations(critics, 'Toby') print('when algorithm of similarity is sim_distance:') print(person_getRecommendations) person_getRecommendations = recommendations.getRecommendations( critics, 'Toby', similarity=recommendations.sim_pearson) print('when algorithm of similarity is sim_pearson:') print(person_getRecommendations) print(end='\n') movies = recommendations.transformPrefs(critics) for item in movies: print(item, movies[item]) print(end='\n') movie_recommendations = recommendations.topMatches(movies, 'Superman Returns')
print sqrt(pow(4.5 - 4, 2) + pow(1 - 2, 2)) reload(recommendations) print '欧几里得距离:' print recommendations.sim_distance(recommendations.critics, 'Lisa Rose', 'Gene Seymour') print '皮尔逊相关系数:' print recommendations.sim_pearson(recommendations.critics, 'Lisa Rose', 'Gene Seymour') print '获得最匹配者:' print recommendations.topMatches(recommendations.critics, 'Toby', n=3) print '获取对Toby的推荐:' print recommendations.getRecommendations(recommendations.critics, 'Toby') print recommendations.getRecommendations(recommendations.critics, 'Toby', recommendations.sim_distance) movies = recommendations.transformPrefs(recommendations.critics) print '调换人与物品:' print movies print '打印相似类型的电影:' print recommendations.topMatches(movies, 'Superman Returns') print recommendations.getRecommendations(movies, 'Just My Luck') # import pydelicious # print pydelicious.get_popular(tag='programming') # print '开始填充数据:'
from recommendations import critics from recommendations2 import moviesgrade from recommendations import sim_distance from recommendations import sim_pearson from recommendations import topMatches from recommendations import pearsonTopMatches from recommendations import distanceTopMatches from recommendations import getRecommendations from recommendations2 import transformPrefs #print(distanceTopMatches(critics, 'Toby', n=3)) # transformPrefs()는 Map 객체의 key값과 value값을 교환함 movies = transformPrefs(critics) # Superman Returns과 유사한 영화 top 3 print(topMatches(movies, 'Superman Returns', 2)) # 평론가 추천 print(getRecommendations(movies, 'Just My Luck'))
import recommendations as rec # for person in rec.critics: # print ("Euklid Top Match: \t" + person + "\t" + str(rec.topMatches(rec.critics, person, rec.sim_euclid)[0])) # print ("Pearson Top Match: \t" + person + "\t" + str(rec.topMatches(rec.critics, person, rec.sim_pearson)[0])) print rec.getRecommendations(rec.critics, 'Toby Segaran', rec.sim_pearson)
def get_recommendations(): user_data = json.loads(request.data) items = getRecommendations(user_data["user_id"]) resp = make_response(json.dumps(list(items), sort_keys=True, indent=4, default=json_util.default)) return resp
if key == '': continue user_dict[key] = eval(r.hget(user_dict_key,key)) return user_dict if __name__ == '__main__': #user_dict = initializeUserDict('programming') #fillItems(user_dict) #save_user_dict(user_dict) user_dict = get_user_dict('user_dict') # Find users most like a given user user = user_dict.keys()[random.randint(0, len(user_dict)-1)] top_matches = recommendations.topMatches(user_dict, user) print "Top matches for", user, ":" print "{0:6} {1}".format( "Score", "Name") for match in top_matches: print "{0:.4f} {1:6}".format(match[0], match[1]) print "\nRecommended URLs for", user, ":" recs = recommendations.getRecommendations(user_dict, user)[0:10] print "{0:6} {1}".format( "Score", "URL") for item in recs: print "{0:.4f} {1}".format(item[0], item[1]) print "placeholder for breakpoint"
print "distance" print recommendations.sim_distance(recommendations.critics, 'ming', 'lin') print recommendations.sim_distance(recommendations.critics, 'ming', 'michael') print recommendations.sim_distance(recommendations.critics, 'ming', 'mick') print "pearson" print recommendations.sim_pearson(recommendations.critics, 'ming', 'lin') print recommendations.sim_pearson(recommendations.critics, 'ming', 'michael') print recommendations.sim_pearson(recommendations.critics, 'ming', 'mick') print "top match" print recommendations.topMatches(recommendations.critics, 'ming', n = 3) print "recommendation" print recommendations.getRecommendations(recommendations.critics, 'ming') print "recommend items" items = recommendations.transformPrefs(recommendations.critics) print recommendations.topMatches(items, 'lady in the water', n =3) print recommendations.sim_pearson(items, 'lady in the water', 'snake on a plane') print "item similarity use distance" print recommendations.calculateSimilarItems(recommendations.critics) print 'item similarity use pearson' print recommendations.calculateSimilarItems(recommendations.critics, similarity = recommendations.sim_pearson) print 'recommendation base on items' itemsim = recommendations.calculateSimilarItems(recommendations.critics) print recommendations.getRecommendedItems(recommendations.critics, itemsim, 'ming')
def recommendByUser(self, event): if not self.isValid(): # 保证输入框存在有效值 return self.resultList = recommendations.getRecommendations( self.prefs, self.userId)[0:self.maxNum] self.showResult()
import recommendations print('-------') result = recommendations.getRecommendations(recommendations.critics, 'Toby') print(result) print('-------') result = recommendations.getRecommendations(recommendations.critics, 'Toby', similarity=recommendations.sim_distance) print(result)
# recommendationTest.py import recommendations itemsim = recommendations.calculateSimilarItems(recommendations.critics) ''' recommendedList = recommendations.getRecommendedItems(recommendations.critics, itemsim, 'Toby') print(recommendedList) ''' prefs = recommendations.loadMovieLens() # print prefs['87'] print 'user-based recommendations : ' recommendeduser = recommendations.getRecommendations(prefs, '87')[0:30] print recommendeduser print 'item-based recommendations : ' itemsim = recommendations.calculateSimilarItems(prefs, n=50) recommendeditems = recommendations.getRecommendedItems(prefs, itemsim, '87')[0:30] print recommendeditems
movie_titles.append(title) return movie_titles,ratings if __name__=='__main__': moviefile = open("u.data","r") pref_dic = {} rec=[] for line in moviefile: (user_id, item_id, rating, timestamp ) = line.split('\t') if user_id in pref_dic: pref_dic[user_id][item_id] = rating else: pref_dic[user_id] = {} result = recommendations.getRecommendations(pref_dic, '33') rec.append(result) bottom5= result[-5:] top5 = result[:5] imdbfile=open("u.item","r") top_titles,rate=getTitles(imdbfile, top5) print "Top Movies are" for i in range(0,len(top_titles)):
#fillIteams(delusers) import recommendations #print recommendations.critics['Lisa Rose']['Lady in the Water'] print recommendations.critics['Lisa Rose'] print recommendations.critics['Gene Seymour'] print recommendations.sim_distance(recommendations.critics,'Lisa Rose','Gene Seymour') print recommendations.sim_pearson(recommendations.critics,'Lisa Rose','Gene Seymour') print recommendations.topMatches(recommendations.critics,'Toby',n=3) print recommendations.topMatches(recommendations.critics,'Toby',n=3,similarity=recommendations.sim_distance) recommendations.getRecommendations(recommendations.critics,'Toby') recommendations.getRecommendations(recommendations.critics,'Toby',similarity=recommendations.sim_distance) movies=recommendations.transformPrefs(recommendations.critics) #print movies recommendations.topMatches(movies,'Superman Returns') recommendations.getRecommendations(movies,'Just My Luck') recommendations.getRecommendations(movies,'Lady in the Water') itemsim=recommendations.calculateSimilarItems(recommendations.critics,n=8) itemsim
# instantiate network network = pl.get_lastfm_network() # get Band band = network.get_artist("J.B.O") topfans = band.get_top_fans(10) group = [a.item for a in topfans] #print band #print topfans #print group userDict = recom.createLastfmUserDict(group) selectedUser = group[1].get_name() print "Selected User: "******"====================================" topMatches = recom.topMatches(userDict, group[1].get_name(), recom.sim_euclid) print "Top Matches: " pp.pprint(topMatches) print "====================================" recommendations = recom.getRecommendations(userDict, group[1].get_name(), recom.sim_euclid) print "Recommendations: " pp.pprint(recommendations) print "====================================" #print result[0][1].key()
if cache_dict and exists(items_cache_file): print "Reading the items from cache file", items_cache_file cache_file = open(items_cache_file, "rb") user_url_tuple = load(cache_file) cache_file.close() else: user_url_tuple = sorted([(user, post["url"], ) for user in user_dict for post in get_userpost_details(user)]) if cache_dict: print "Saving to cache file" cache_file = open(items_cache_file, "wb") dump(user_url_tuple, cache_file) cache_file.close() #Very Imperative grouped_by_values = groupby(user_url_tuple, lambda (user, _): user) distinct_url_sequence = map(lambda (_, url) : (url, 0), user_url_tuple) for user in user_dict: user_dict[user] = dict(distinct_url_sequence) for user, grouped_values in grouped_by_values: for _, url in grouped_values: user_dict[user][url] = 1 user_dict = initializeUserDict('technology', count=10) fillItems(user_dict) user = user_dict.keys()[random.randint(0, len(user_dict) - 1)] print "Top matches for ", user, " are ", topMatches(user_dict, user) print "Recommendations for user ", user, " are ", getRecommendations(user_dict, user)
# Commands used to demonstrate / use functions created in Collective Intelligence import recommendations from recommendations import critics # pg 9: critics['Lisa Rose']['Lady in the Water'] critics['Toby']['Snakes on a Plane'] = 4.5 critics['Toby'] # pg 11 recommendations.sim_distance( recommendations.critics, 'Lisa Rose', 'Gene Seymour' ) # pg 14 recommendations.sim_pearson( recommendations.critics, 'Lisa Rose', 'Gene Seymour' ) # pg 15 recommendations.topMatches( recommendations.critics, 'Toby', n = 3 ) # pg 17 recommendations.getRecommendations(recommendations.critics, 'Toby') recommendations.getRecommendations(recommendations.critics, 'Toby', similarity = recommendations.sim_distance) movies = recommendations.transformPrefs( recommendations.critics ) recommendations.topMatches(movies, 'Superman Returns') recommendations.getRecommendations(movies, 'Just My Luck')
def recommended_for_you(self, you): prefs = self.prefs recs = recommendations.getRecommendations(prefs, str(you)) total = len(recs) return (recs[0:5], recs[total-5:total])
def get_worst_recomendations(id, data): new_data = recommendations.getRecommendations(data, id) new_data.reverse() print('\n Worst Recommendation\n',new_data[0:5])
from recommendations import critics from recommendations import sim_distance from recommendations import sim_pearson from recommendations import pearsonTopMatches from recommendations import distanceTopMatches from recommendations import getRecommendations print(critics['Lisa Rose']['Lady in the Water']) critics['Toby']['Snakes on a Plane'] = 5.0 print(critics['Toby']) result = { 'euclidean distance socre': sim_distance(critics, 'Lisa Rose', 'Gene Seymour') } print(result) result = { 'Pearson correlation score': sim_pearson(critics, 'Lisa Rose', 'Gene Seymour') } print(result) print(distanceTopMatches(critics, 'Toby', n=3)) print(pearsonTopMatches(critics, 'Toby', n=3)) print('-------------------------------------') print(getRecommendations(critics, 'Toby')) print('-------------------------------------') print('-------------------------------------') print(getRecommendations(critics, 'Toby', similarity=sim_distance))
def get_top_recomendations(id,data): output = recommendations.getRecommendations(data, id) print('\n Top Recommendation\n',output[0:5])
try: posts = get_userposts(user) break except: print "Failed user " + user + ",retrying" time.sleep(4) for post in posts: url = post['url'] user_dict[user][url] = 1.0 all_items[url] = 1 # Fill in missing items with 0 for ratings in user_dict.values(): for item in all_items: if item not in ratings: ratings[item] = 0.0 if __name__ == "__main__": delusers = initializeUserDict('programming') delusers['onerhao'] = {} fillItems(delusers) print delusers user = delusers.keys()[random.randint(0, len(delusers) - 1)] print user print recommendations.topMatches(delusers, user) print recommendations.getRecommendations(delusers, user) # url = recommendations.getRecommendations(delusers, user)[0][1] # print recommendations.topMatches( # recommendations.transformPrefs(delusers), url)
###################################################### delusers = deliciousrec.initializeUserDict('gratis') delusers['lpagliari'] = {} # Add yourself to the dictionary if you use delicious deliciousrec.fillItems(delusers) delusers ########################### # pick random user and find users similar to him/her: import recommendations reload(recommendations) import random user = delusers.keys()[random.randint(0, len(delusers) - 1)] user recommendations.topMatches(delusers, user) ########################### # get recommendations for a user: recommendations.getRecommendations(delusers, user)[0: 10] reload(recommendations) itemsim = recommendations.calculateSimilarItems(recommendations.critics) itemsim recommendations.getRecommendedItems(recommendations.critics, itemsim, 'Toby') prefs = recommendations.loadMovieLens() prefs['87'] recommendations.getRecommendations(prefs,'87')[0: 30]
if choice == '1': user = raw_input('enter the user ID:\t') print '\n' print 'Books till now read by the user are:\n' print critics[user], '\n' dischoice = raw_input( 'enter your choice\n1.euclidian distance\n2.pearson corelation\nchoice:\t' ) if dischoice == '1': print 'top matched user according to the euclidian distance\n' print recommendations.topMatches(critics, user, 3, recommendations.sim_distance), '\n' print 'Recommended books according to the euclidian distance\n' print recommendations.getRecommendations( critics, user, similarity=recommendations.sim_distance)[0:3], '\n' else: if dischoice == '2': print 'top matched user according to the pearson corelation\n' print recommendations.topMatches(critics, user, 3), '\n' print 'Recommended books according to the pearson corelaton\n' print recommendations.getRecommendations(critics, user)[0:3], '\n' else: print 'invalid choice... try again' else: if choice == '2': critic = recommendations.transformPrefs(critics) book = raw_input('enter the book name:\t') print '\n' print 'Users who have read the book till now are:\n' print critic[book], '\n'
#!/usr/bin/python import recommendations list=[] prefs=recommendations.loadMovieLens() list=recommendations.getRecommendations(prefs,"xxx") for i in range(xxx): print list[i]
def recommendByUser(self, event): if not self.isValid(): # 保证输入框存在有效值 return self.resultList = recommendations.getRecommendations(self.prefs, self.userId)[0:self.maxNum] self.showResult()
import recommendations as rec prefs = rec.loadMovieLens("../data/ml-100k") table = open("../docs/q3_table.tex", "w") table.write("\\begin{longtable}{| c | c |}\n") table.write("\\hline\n") table.write("Movie & Ranking\\\\\n") recs = rec.getRecommendations(prefs, '929', best=True) for r in recs: table.write("\\hline\n {} & {:.3f}\\\\\n".format(r[1].replace('&', '\&'), r[0])) table.write("\\hline\n") table.write("\\caption{Ranking for unseen movies}\n") table.write("\\label{tab:myfirstlongtable}\n") table.write("\\end{longtable}\n") table.write("Top 5 recommendations:\n") table.write("\\begin{enumerate}\n") for r in recs[:5]: table.write("\\item {}\n".format(r[1])) table.write("\\end{enumerate}\n") recs = rec.getRecommendations(prefs, '929', best=False) table.write("Bottom 5 recommendations:\n") table.write("\\begin{enumerate}\n") for r in recs[:5]: table.write("\\item {}\n".format(r[1])) table.write("\\end{enumerate}\n") table.close()
'Gene Seymour')) # 0.14814814814814814 (different from book???) pprint( recommendations.sim_pearson(recommendations.critics, 'Lisa Rose', 'Gene Seymour')) # 0.39605901719066977 (the same as book) # 为Toby寻找品味相同的用户 pprint(recommendations.topMatches(recommendations.critics, 'Toby', n=3)) # [(0.9912407071619299, 'Lisa Rose'), # (0.9244734516419049, 'Mick LaSalle'), # (0.8934051474415647, 'Claudia Puig')] # 为Toby寻找他可能感兴趣的电影(基于用户的协作型过滤,用欧几里德距离作为相似度评估) pprint(recommendations.getRecommendations(recommendations.critics, 'Toby')) # [(3.3477895267131017, 'The Night Listener'), # (2.8325499182641614, 'Lady in the Water'), # (2.530980703765565, 'Just My Luck')] # 为Toby寻找他可能感兴趣的电影(基于用户的协作型过滤,用皮尔逊系数作为相似度评估) pprint( recommendations.getRecommendations( recommendations.critics, 'Toby', similarity=recommendations.sim_distance)) # [(3.5002478401415877, 'The Night Listener'), # (2.7561242939959363, 'Lady in the Water'), # (2.461988486074374, 'Just My Luck')] # 为Superman Returns寻找相似的电影
if __name__ == '__main__': # stupid little demo import recommendations print 'getting users...' users = initializeUserDict('programming') users['thakis'] = {} users['ytamshg'] = {} users['tubanator'] = {} print 'getting their posts (takes a while) ...' fillItems(users) print 'Distances:' print recommendations.sim_distance(users, 'thakis', 'ytamshg') print recommendations.sim_distance(users, 'thakis', 'tubanator') print recommendations.sim_distance(users, 'tubanator', 'ytamshg') print 'Pearsons:' print recommendations.sim_pearson(users, 'thakis', 'ytamshg') print recommendations.sim_pearson(users, 'thakis', 'tubanator') print recommendations.sim_pearson(users, 'tubanator', 'ytamshg') print 'Recommendations:' print recommendations.getRecommendations(users, 'thakis')[0:10] print recommendations.getRecommendations(users, 'ytamshg')[0:10] print recommendations.getRecommendations(users, 'tubanator')[0:10]
user_url_tuple = load(cache_file) cache_file.close() else: user_url_tuple = sorted([( user, post["url"], ) for user in user_dict for post in get_userpost_details(user)]) if cache_dict: print "Saving to cache file" cache_file = open(items_cache_file, "wb") dump(user_url_tuple, cache_file) cache_file.close() #Very Imperative grouped_by_values = groupby(user_url_tuple, lambda (user, _): user) distinct_url_sequence = map(lambda (_, url): (url, 0), user_url_tuple) for user in user_dict: user_dict[user] = dict(distinct_url_sequence) for user, grouped_values in grouped_by_values: for _, url in grouped_values: user_dict[user][url] = 1 user_dict = initializeUserDict('technology', count=10) fillItems(user_dict) user = user_dict.keys()[random.randint(0, len(user_dict) - 1)] print "Top matches for ", user, " are ", topMatches(user_dict, user) print "Recommendations for user ", user, " are ", getRecommendations( user_dict, user)
def print_UBRecommendations(critics, userId='60255'): werec = recommendations.getRecommendations(critics, userId)[0:30] print "\n---------User-based Recommendations using 60255----------\n" # Print out n recommendations convertId_to_title(werec, 5)
# Find similarity between two users Euclidian distance print( recommendations.sim_distance(recommendations.critics, 'Lisa Rose', 'Gene Seymour')) # Find similarity between two users Pearson distance print( recommendations.sim_pearson(recommendations.critics, 'Lisa Rose', 'Gene Seymour')) #Find similar users print(recommendations.topMatches(recommendations.critics, 'Toby', n=3)) #Get recommendations of movies default=Pearson print(recommendations.getRecommendations(recommendations.critics, 'Toby')) #Get recommendations of movies using Euclidian print( recommendations.getRecommendations( recommendations.critics, 'Toby', similarity=recommendations.sim_distance)) # Transpose the matrix to movies vs user movies = recommendations.transformPrefs(recommendations.critics) #Print similar movies print(recommendations.topMatches(movies, 'Superman Returns')) #Similarity matirx of items
def userRec(user): """ Regresa las recomendaciones para "user", basándose en la información de MovieLens guardada en prefs """ return recommendations.getRecommendations(prefs, user)[0:20]
# instantiate network network = pl.get_lastfm_network() # get Band band = network.get_artist("J.B.O") topfans = band.get_top_fans(10) group=[a.item for a in topfans] #print band #print topfans #print group userDict = recom.createLastfmUserDict(group) selectedUser = group[1].get_name() print "Selected User: "******"====================================" topMatches = recom.topMatches(userDict, group[1].get_name(), recom.sim_euclid) print "Top Matches: " pp.pprint(topMatches) print "====================================" recommendations = recom.getRecommendations(userDict, group[1].get_name(), recom.sim_euclid) print "Recommendations: " pp.pprint(recommendations) print "====================================" #print result[0][1].key()
__author__ = 'ponomarevandrew' from recommendations import critics import recommendations print critics['Lisa Rose']['Lady in the Water'] critics['Toby']['Snakes on a Plane'] = 4.5 print critics['Toby'] print critics print "-----------" print recommendations.getRecommendations(recommendations.critics, 'Toby') print "-----------" movies = recommendations.transformPrefs(recommendations.critics) print recommendations.topMatches(movies, 'Superman Returns') print "-----------" itemsim = recommendations.calculateSimilarItems(recommendations.critics) print itemsim print "-----------" print recommendations.getRecommendedItems(recommendations.critics, itemsim, 'Toby') print "-----------" prefs = recommendations.loadMovieLens() print prefs['1'] print "-----------" print recommendations.getRecommendations(prefs,'42')[31:60] print "-----------" ##itemsim = recommendations.calculateSimilarItems(prefs, n =50) ##print itemsim print "-----------" ##print recommendations.getRecommendedItems(prefs,itemsim, '87')[0:30] print "-----------"
for i in range(3): try: posts = get_userposts(user) break except: print "Failed user " + user + ",retrying" time.sleep(4) for post in posts: url = post['url'] user_dict[user][url] = 1.0 all_items[url] = 1 # Fill in missing items with 0 for ratings in user_dict.values(): for item in all_items: if item not in ratings: ratings[item] = 0.0 if __name__ == "__main__": delusers = initializeUserDict('programming') delusers['onerhao'] = {} fillItems(delusers) print delusers user = delusers.keys()[random.randint(0, len(delusers) - 1)] print user print recommendations.topMatches(delusers, user) print recommendations.getRecommendations(delusers, user) # url = recommendations.getRecommendations(delusers, user)[0][1] # print recommendations.topMatches( # recommendations.transformPrefs(delusers), url)
return tag_map if __name__ == '__main__': # stupid little demo import recommendations print 'getting users...' users = initializeUserDict('programming') users['thakis'] = {} users['ytamshg'] = {} users['tubanator'] = {} print 'getting their posts (takes a while) ...' fillItems(users) print 'Distances:' print recommendations.sim_distance(users, 'thakis', 'ytamshg') print recommendations.sim_distance(users, 'thakis', 'tubanator') print recommendations.sim_distance(users, 'tubanator', 'ytamshg') print 'Pearsons:' print recommendations.sim_pearson(users, 'thakis', 'ytamshg') print recommendations.sim_pearson(users, 'thakis', 'tubanator') print recommendations.sim_pearson(users, 'tubanator', 'ytamshg') print 'Recommendations:' print recommendations.getRecommendations(users, 'thakis')[0:10] print recommendations.getRecommendations(users, 'ytamshg')[0:10] print recommendations.getRecommendations(users, 'tubanator')[0:10]
def test_getRecommendations(self): self.assertEqual( recommendations.getRecommendations(recommendations.critics, 'Toby')[0], (3.3477895267131013, 'The Night Listener'))
def test_getRecommendations(self): self.assertEqual(recommendations.getRecommendations(recommendations.critics,'Toby')[0],(3.3477895267131013, 'The Night Listener'))